$#a adds magic to the associated array. This costs time and memory. I've written a patch to prevent that when $#a is used as an rvalue.

speed_test.pl:

use strict; use warnings; use Devel::Peek qw( Dump ); use Time::HiRes qw( time ); my @a = qw( a b c d e f g h i j ); my $stime = time; my $x; $x = $#a for 1..1_000_000; my $etime = time; print("time delta = ", $etime-$stime, "\n"); Dump(\@a, 1) if $ARGV[0];

Before change:

$ ./perl -Ilib speed_test.pl 0 time delta = 0.623134136199951 $ ./perl -Ilib speed_test.pl 0 time delta = 0.621975898742676 $ ./perl -Ilib speed_test.pl 0 time delta = 0.626943826675415 $ ./perl -Ilib speed_test.pl 1 time delta = 0.636697053909302 SV = IV(0x8400afc) at 0x8400b00 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x83ca0f0 SV = PVAV(0x83bb1b4) at 0x83ca0f0 REFCNT = 2 FLAGS = (PADMY,RMG) MAGIC = 0x83c5210 MG_VIRTUAL = &PL_vtbl_arylen_p MG_TYPE = PERL_MAGIC_arylen_p(@) MG_FLAGS = 0x02 REFCOUNTED MG_OBJ = 0x83ca280 SV = PVMG(0x840f42c) at 0x83ca280 REFCNT = 1 FLAGS = (GMG,SMG,pIOK) IV = 9 NV = 0 PV = 0 MAGIC = 0x83c5230 MG_VIRTUAL = &PL_vtbl_arylen MG_TYPE = PERL_MAGIC_arylen(#) MG_OBJ = 0x83ca0f0 ARRAY = 0x83c5180 FILL = 9 MAX = 9 ARYLEN = 0x83ca280 FLAGS = (REAL)

After change:

$ ./perl -Ilib speed_test.pl 0 time delta = 0.463121175765991 $ ./perl -Ilib speed_test.pl 0 time delta = 0.498654127120972 $ ./perl -Ilib speed_test.pl 0 time delta = 0.462605953216553 $ ./perl -Ilib speed_test.pl 1 time delta = 0.464058876037598 SV = IV(0x83cb27c) at 0x83cb280 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x83cb0f0 SV = PVAV(0x83bc1b4) at 0x83cb0f0 REFCNT = 2 FLAGS = (PADMY) ARRAY = 0x83c6180 FILL = 9 MAX = 9 ARYLEN = 0x0 FLAGS = (REAL)

Unfortunately, I'm not very confident about my patch

From c045b931b2afb3b2af6641a9943f65f295ee7da1 Mon Sep 17 00:00:00 2001 From: Eric Brine <ikegami@adaelis.com> Date: Fri, 23 Oct 2009 19:05:40 -0400 Subject: [PATCH] Avoid adding magic with rvalue $#a --- pp.c | 17 ++++++++++++----- 1 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pp.c b/pp.c index 6f56368..b9b8ba6 100644 --- a/pp.c +++ b/pp.c @@ -321,12 +321,19 @@ PP(pp_av2arylen) { dVAR; dSP; AV * const av = MUTABLE_AV(TOPs); - SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av)); - if (!*sv) { - *sv = newSV_type(SVt_PVMG); - sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0); + const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET; + if (lvalue) { + SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av)); + if (!*sv) { + *sv = newSV_type(SVt_PVMG); + sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0); + } + SETs(*sv); + } else { + SETs(sv_2mortal(newSViv( + AvFILL(MUTABLE_AV(av)) + CopARYBASE_get(PL_curcop) + ))); } - SETs(*sv); RETURN; } -- 1.6.5

Specifically,


In reply to Review of patch to avoid adding magic with rvalue $#a (XS gurus wanted) by ikegami

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.