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

Please don't use $#{..} >= 0. As well as being uglier and more long winded than @{...}, it's less efficient in perl 5.10.0 onwards (attaching of PERL_MAGIC_arylen magic to the AV).

Dave.

Replies are listed 'Best First'.
Re^3: Does @{ } copy arrays?
by BrowserUk (Patriarch) on Oct 16, 2009 at 21:57 UTC
    less efficient in perl 5.10.0 onwards (attaching of PERL_MAGIC_arylen magic to the AV).

    Is that going to revert?


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

        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.
Re^3: Does @{ } copy arrays?
by QM (Parson) on Oct 17, 2009 at 01:06 UTC
    Well, wouldn't $#{}, and @{} in a scalar context need similar magic? Isn't the second just an increment of the first? (There must be more magic anticipated by $#{} to warrant the slowdown?)

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

      @a in scalar context just pushes its size onto the stack. $#a in scalar context could be an lvalue or an rvalue, so a 'magic' var is pushed onto the stack that updates the array's size if assigned to. This could probably be optimised when pp_av2arylen is called in rvalue context.

      Dave.

        Ah, of course. I suppose there are cases where it's not immediately apparent to the parser if a given occurrence is an lvalue or rvalue?

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

        This could probably be optimised when pp_av2arylen is called in rvalue context.

        Done by commits 02d85cc37a4acecafdc2f0b45640b03cd1f4ac71 and 28c5b5bcd7f52e6b2219508a1066cd0ccc8dd19a. You'll see these changes in 5.11.2 (next month's dev release) and 5.12 (early to mid 2010)

Re^3: Does @{ } copy arrays?
by bv (Friar) on Oct 17, 2009 at 16:34 UTC

    Interesting! That's the kind of answer I was hoping someone would post. For anyone who's keeping score, here's a benchmark and my results:

    #!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my $i=0; cmpthese(-10, { deref => sub { my $aryptr = [qw(Once upon a time in a galaxy far far away +)]; if ( @{ $aryptr } ) { $i=1; } $aryptr = []; if ( @{ $aryptr } ) { $i=-1; } }, arylen => sub { my $aryptr = [qw(Once upon a time in a galaxy far far away +)]; if ( $#{ $aryptr } >= 0 ) { $i=1; } $aryptr = []; if ( $#{ $aryptr } >= 0 ) { $i=-1; } }, } ); __END__ Rate arylen deref arylen 95903/s -- -28% deref 132418/s 38% --

    print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

      The point is that $#a should be faster than @a-1. That's not what you are testing. You have have an extra operation in your $#a test instead of having one less.

      #!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); our @a = qw( Once upon a time in a galaxy far far away ); cmpthese(-3, { cnt_minus_1 => 'my $x = @a-1;', last_idx => 'my $x = $#a;', });
      Rate last_idx cnt_minus_1 last_idx 3225597/s -- -25% cnt_minus_1 4308243/s 34% --
Re^3: Does @{ } copy arrays?
by Anonymous Monk on Oct 17, 2009 at 18:09 UTC
    Uh oh, please don't tell people this, they'll think perl is stupid