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

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

Replies are listed 'Best First'.
Re^9: Does @{ } copy arrays?
by ikegami (Patriarch) on Oct 23, 2009 at 19:52 UTC

    I consider my example to pass $#a as an argument an extremely common case

    While I disagree about the commonness of passing $#a, there are options if it is deemed to be too slow.

    • $#a as an arg is an lvalue. Users can pass 0+$#a if they want the extra speed of an rvalue.

    • $#a as an arg is an rvalue. Users can use ${ \$#a } if they want an lvalue.

    • $#a as an arg is a delayed lvalue. I don't know if the mechanism can be extended to $#a, but hash element lookups are like this:

      $ perl -le'\$h{k}; print exists($h{k})?1:0' 1 $ perl -le'sub{ $x=$_[0] }->($h{k}); print exists($h{k})?1:0' 0 $ perl -le'sub{ $_[0]=$x }->($h{k}); print exists($h{k})?1:0' 1 $ perl -le'sub{ $x=$_[0] }->(@h{k}); print exists($h{k})?1:0' 1 $ perl -le'sub{ $_[0]=$x }->(@h{k}); print exists($h{k})?1:0' 1

      Unfortunately, none of those 'remedies' work:

      C:\test>p1 [0] Perl> use Devel::Size qw[ size total_size ];; [0] Perl> @a = 1 .. 10;; [0] Perl> print size( \@a ), ':', total_size( \@a );; 256 : 496 [0] Perl> print $#a;; 9 [0] Perl> print size( \@a ), ':', total_size( \@a );; 440 : 680 C:\test>p1 [0] Perl> use Devel::Size qw[ size total_size ];; [0] Perl> @a = 1 .. 10;; [0] Perl> print size( \@a ), ':', total_size( \@a );; 256 : 496 [0] Perl> $x = 0+$#a;; [0] Perl> print size( \@a ), ':', total_size( \@a );; 432 : 672 C:\test>p1 [0] Perl> use Devel::Size qw[ size total_size ];; [0] Perl> @a = 1 .. 10;; [0] Perl> print size( \@a ), ':', total_size( \@a );; 256 : 496 [0] Perl> $x = ${ \$#a };; [0] Perl> print size( \@a ), ':', total_size( \@a );; 432 : 672

      In every case, the mere mention of $#a has already caused the damage to be done. Trading 76-bytes per array to save 4-bytes per array does not make sense.


      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.

        In every case, the mere mention of $#a has already caused the damage to be done.

        As I've already mentioned, we're talking about a Perl that's fixed to not add the magic for rvalue $#a.

      Hmm ... as far as I understood dave, copying to an rvalue doesn't help, because the array has already been "prepared" for later modification without taking care about any rvalue context !(?)

      so calling with arg @a-1 is always the fastest alternative.

      Cheers Rolf

        because the array has already been "prepared" for later modification without taking care about any rvalue context !

        What? We're talking about a Perl that's fixed to not add the magic for rvalue $#a.

        You said it would be easy to forget foo($#a) is an lvalue and that it's a common occurrence.

        I replied that it's no biggie if it's true. That case can be handled if there is a need.

Re^9: Does @{ } copy arrays?
by BrowserUk (Patriarch) on Oct 23, 2009 at 16:19 UTC
    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:

    1. Not assigning the arguments to named lexicals within the sub as is normal:
      sub ...{ my( ..., ... ) = @_; }
    2. 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.
      sigh...

      Look a compiler must cover _all_ cases not only what you consider "common"...

      Cheers Rolf

        Sigh...

        And well you might. You're obviously finding this quite hard. Read back. No one suggested that the compiler should suddenly stop catering for all cases did they!

        Prior to this recent change, both lvalue and rvalue uses--and yes, even your obscured, aliased lvalue assignment--worked just fine. And they didn't double or treble the size of small anonymous arrays in the process. But subsequent to this change, any reference to $#--lvalue or rvalue--has the immediate affect of adding magic to the array, regardless whether it is actually used!

        My suggestion was that the application of magic be deferred until it is actually required--when it is used as an lvalue--rather than when it is first referenced. As ikegami has demonstrated, that is perfectly feasible to do.

        The whole point about knowing which are the common cases, and which are the obscure--may only happen in golf and pointless demonstrations--cases, is that you optimise for the former, not the latter.


        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.