Re^7: Does @{ } copy arrays?
by ikegami (Patriarch) on Oct 23, 2009 at 16:47 UTC
|
I suppose there are cases where it's not immediately apparent to the parser if a given occurrence is an lvalue or rvalue?
Perl is very much aware of which expressions should return something "M"odifiable.
$ perl -MO=Concise,-exec -e' $x=$#a ' 2>&1 | grep av2arylen
5 <1> av2arylen sK/1
$ perl -MO=Concise,-exec -e' $#a=$x ' 2>&1 | grep av2arylen
6 <1> av2arylen sKRM*/1
$ perl -MO=Concise,-exec -e' foo($#a) ' 2>&1 | grep av2arylen
6 <1> av2arylen sKM/1
$ perl -MO=Concise,-exec -e' \$#a ' 2>&1 | grep av2arylen
5 <1> av2arylen sKRM/1
$ perl -MO=Concise,-exec -e' 1 for $#a ' 2>&1 | grep av2arylen
7 <1> av2arylen sKM/1
$ perl -MO=Concise,-exec -e' 1 for 0..$#a ' 2>&1 | grep av2arylen
8 <1> av2arylen sK/1
| [reply] [d/l] |
|
|
Can you provide the decoder ring for things like "sKRM*/1"?
-QM
--
Quantum Mechanics: The dreams stuff is made of
| [reply] |
|
|
B::Concise lists what opcode flag each letter represents. "M"odifiable is the relevant one here.
Interesting tidbit:
As an optimisation, lvalue subs weren't making arylen as Modifiable.
$ perl -MO=Concise,-exec,f -e'sub f :lvalue { $#a }' 2>&1 | grep av2ar
+ylen
4 <1> av2arylen sK/1
My patch relied on that flag, so it was buggy. The second patch I mentioned made lvalue subs mark arylen as Modifiable.
| [reply] [d/l] |
|
|
|
|
Re^7: Does @{ } copy arrays?
by BrowserUk (Patriarch) on Oct 19, 2009 at 03:35 UTC
|
an edge case
Do you consider your example the common case? Relative to all the readonly uses of $#{}. Eg. 0 .. $#a; there are myriad others.
dave_the_m suggested elsewhere that "$#{..} >= 0. As well as being uglier and more long winded than @{...},". Well, ditto scalar( @a ) - 1 versus $#a.
But as so often in these cases, discussion is not entertained.
On the basis of a very casual survey of code on my system and cpan, this "optimisation" is a space pessimisation if 9 cases out of 10 I looked at. In some cases, extremely.
I'd love to see the evidence justifying the addition of tens of words to any array that you reference $#, in order to save "1 pointers-worth of memory just to store NULL" on those you don't. And that's before you consider the peformance effects.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
|
as dave already said This could probably be optimised when pp_av2arylen is called in rvalue context.
but my example shows that your suggestion Could not the application of magic to an array, and all the penalties that go with it, be deferred until it is used in an lvalue context? would effect manipulate code at long distance.
And yes of course I consider my example to pass $#a as an argument an extremely common case.
| [reply] |
|
|
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
|
And yes of course I consider my example to pass $#a as an argument an extremely common case.
The validity of your example as a counter-argument relies not just upon passing $#a as an argument to a sub, but also on:
- Not assigning the arguments to named lexicals within the sub as is normal:
sub ...{
my( ..., ... ) = @_;
}
- And then, using the $_[...] alias as an lvalue.
I bet you cannot find a single example of anyone doing this on cpan; or in any publically available piece of real code.
Ergo, not a common case.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
|
|
|