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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|