ikegami has asked for the wisdom of the Perl Monks concerning the following question:
$#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,
Is it ok to define sv inside the curlies?
Is the arg guaranteed to be an AV?
Is the sv_2mortal appropriate?
Also, I initially tried to avoid creating a new SV by using
dTARGET; SETi( AvFILL(MUTABLE_AV(av)) + CopARYBASE_get(PL_curcop) );
instead of
SETs(sv_2mortal(newSViv( AvFILL(MUTABLE_AV(av)) + CopARYBASE_get(PL_curcop) )));
but I got the following while building the rest of Perl:
panic: pad_sv po at lib/re.pm line 99.
Is using TARG the wrong way to go, or did I just do it wrong? I based my code on other ops.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Review of patch to avoid adding magic with rvalue $#a (XS gurus wanted)
by almut (Canon) on Oct 26, 2009 at 20:16 UTC | |
|
Re: Review of patch to avoid adding magic with rvalue $#a (XS gurus wanted)
by Herkum (Parson) on Oct 26, 2009 at 16:07 UTC | |
by ikegami (Patriarch) on Oct 26, 2009 at 16:33 UTC |