Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^6: Perl is dying (better sorting than the ST)

by diotalevi (Canon)
on Feb 11, 2007 at 06:04 UTC ( #599433=note: print w/ replies, xml ) Need Help??


in reply to Re^5: Perl is dying (better sorting than the ST)
in thread Perl is dying

How can perl know the extent of of the arrays? I can see from looking at a sample optree that the size of the input array can be known at compile time. I can also see sometimes sort's function can be known to be "simple" and won't increase the extent of the array. Lastly, I can also see that the expression used by the outer, unpacking map is simple. If I thought this were an interesting thing to chase down I'd probably go write some prolog to match stuff in our optree and if we were lisp instead of perl I'd write a macro to automatically optimize the code. We're not and we won't get that goodness til Perl 6 so it'd be a waste to do this to Perl 5.

perl -MO=Concise -e 'print map $_->[1],sort{$a->[3]<=>$b->[3]}map[lc,$ +_],1,2,3' -e syntax OK r <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 2 -e:1) v ->3 q <@> print vK ->r 3 <0> pushmark s ->4 k <|> mapwhile(other->l)[t8] lK/1 ->q j <@> mapstart lK/2 ->k 4 <0> pushmark s ->5 - <1> null lK/1 ->5 ;; You can see the ->[1] access and you already know there's only two +elements. p <2> aelem sK/2 ->k n <1> rv2av[t1] sKR/1 ->o m <1> rv2sv sKM/DREFAV,1 ->n l <$> gv(*_) s ->m o <$> const(IV 1) s ->p ;; You can see the entire sort block got optimized into something nati +ve (I guess. It's certainly not in perl anymore) i <@> sort lKMS* ->j 5 <0> pushmark s ->6 - <1> null sK/1 ->6 - <@> scope sK ->(end) - <0> ex-nextstate v ->- - <2> ncmp[t4] sK/2 ->- - <2> aelem sK/2 ->- - <1> rv2av[t2] sKR/1 ->- - <1> rv2sv sKM/DREFAV,1 ->- - <$> gv(*a) s ->- - <$> const(IV 3) s ->- - <2> aelem sK/2 ->- - <1> rv2av[t3] sKR/1 ->- - <1> rv2sv sKM/DREFAV,1 ->- - <$> gv(*b) s ->- - <$> const(IV 3) s ->- b <|> mapwhile(other->c)[t7] lK/1 ->i a <@> mapstart lK/2 ->b 6 <0> pushmark s ->7 - <1> null lK/1 ->7 ;; You can see the array get allocated and can see it has only two ele +ments. h <1> srefgen sK/1 ->b - <1> ex-list lKRM ->h g <@> anonlist sKRM/1 ->h c <0> pushmark s ->d e <1> lc[t6] sK/1 ->f - <1> ex-rv2sv sK/1 ->e d <$> gvsv(*_) s ->e - <1> ex-rv2sv sK/1 ->g - <1> ex-rv2sv sK/1 ->g f <$> gvsv(*_) s ->g 7 <$> const(IV 1) sM ->8 8 <$> const(IV 2) sM ->9 9 <$> const(IV 3) sM ->a

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊


Comment on Re^6: Perl is dying (better sorting than the ST)
Download Code
Re^7: Perl is dying (better sorting than the ST)
by demerphq (Chancellor) on Feb 11, 2007 at 11:22 UTC

    If perl knew the array was going to be thrown away at the end of the statement, and that all indexed look ups were within the bounds of the original.

    Its a big if, hence the reason I mentioned Python's tuples, which could be seen from a perl perspective to be a form of compiler hinting, telling the compiler that an AV need not be constructed. You could prototype the syntax using a RO AV for instance.

    ---
    $world=~s/war/peace/g

      I'd make you say map tuple( ... ) and then you'd get your promised thing which I guess is smaller and has no mutators.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        Yeah. The idea is that a tuple is immutable; once itís been constructed, it can no longer be modified. In that case, perl does know the extent of the data structure: itís the extent of the data structure at the time of its construction.

        It would have more applications than just map, too. The ideaís not bad. (Of course it doesnít redeem the ST in my opinionÖ)

        Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://599433]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-07-12 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (240 votes), past polls