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

Is that going to revert?
No. It's a trade off that made $#a slightly slower and bigger while making arrays smaller and faster (as long as you don't access them via the $# mechanism).

Dave.

Replies are listed 'Best First'.
Re^5: Does @{ } copy arrays?
by BrowserUk (Patriarch) on Oct 17, 2009 at 15:41 UTC

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