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

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.

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

    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.

Re^10: Does @{ } copy arrays?
by LanX (Saint) on Oct 23, 2009 at 20:33 UTC
    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.