in reply to Re^4: Does @{ } copy arrays?
in thread Does @{ } copy arrays?

Thank you for your response.

arrays smaller and faster (as long as you don't access them via the $#

That's unfortunate.

Is it really impossible to make read-only references to $#.. equivalent (in performance, cost and side-effects), to ( Scalar @.. - 1 )?

Ie. 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?


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.
RIP PCW It is as I've been saying!(Audio until 20090817)

Replies are listed 'Best First'.
Re^6: Does @{ } copy arrays?
by LanX (Saint) on Oct 17, 2009 at 19:28 UTC
    Ie. 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?

    ehm, that sounds like a very risky approach for tuning an edge case:

    DB<7> sub tst { $_[0]=3 } DB<8> @a=(1..10) DB<9> tst $#a DB<10> p @a 1234

    personally I wouldn't care too much about the performance penalty, but the possible side-effects after forgetting that $#a is an lvalue and $_[0] is an alias, should be a reason enough to stick with scalar @a.

    Cheers Rolf

      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
        Can you provide the decoder ring for things like "sKRM*/1"?

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

      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.
        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.

        Cheers Rolf