Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

extend OP_AELEMFAST optimisation to lexical arrays

1 view
Skip to first unread message

Dave Mitchell

unread,
Feb 22, 2004, 11:04:35 AM2/22/04
to perl5-...@perl.org
As many of you may be aware, there is currently an optimisation done on
accesses to a package array where the index is a constant 0<=i<256:

$ ./perl -Ilib -MO=Concise,-exec -e'$a[256]'
...
3 <$> gv(*a) s
4 <1> rv2av sKR/1
5 <$> const(IV 256) s
6 <2> aelem vK/2

$ ./perl -Ilib -MO=Concise,-exec -e'$a[255]'
...
3 <$> aelemfast(*a) s/255

Well, with change #22357 this now works with lexical arrays too:

$ ./perl -Ilib -MO=Concise,-exec -e'my @a;$a[256]'
...
5 <0> padav[@a:1,2] sR
6 <$> const(IV 256) s
7 <2> aelem vK/2

$ ./perl -Ilib -MO=Concise,-exec -e'my @a;$a[255]'
...
5 <$> aelemfast[@a:1,2] sR*/255

the OPf_SPECIAL flag is used to indicate that the op points to the lexical
in op_targ rather than the GV in op_sv.

It seems to make such array accesses a lot faster: I got these timings
(run three times):

before:
my 12.35 our 8.11
my 12.38 our 8.11
my 13.18 our 8.69
after:
my 8.30 our 8.20
my 8.29 our 8.36
my 8.27 our 8.26

And here's the code:

my @a = 1..10;
our @b = 1..10;
my $x;
use Time::HiRes qw(gettimeofday tv_interval);
my $t0;

$t0 = [gettimeofday];
for (1..10_000_000) {
$x = $a[0]+$a[1]+$a[2]+$a[3]+$a[4]+$a[5];
}
my $elapsed1 = tv_interval($t0);

$t0 = [gettimeofday];
for (1..10_000_000) {
$x = $b[0]+$b[1]+$b[2]+$b[3]+$b[4]+$b[5];
}
my $elapsed2 = tv_interval($t0);

printf "my %5.2f our %5.2f\n", $elapsed1, $elapsed2;

--
In England there is a special word which means the last sunshine
of the summer. That word is "spring".

Yitzchak Scott-Thoennes

unread,
Feb 22, 2004, 4:48:47 PM2/22/04
to perl5-...@perl.org
On Sun, Feb 22, 2004 at 04:04:35PM +0000, Dave Mitchell <da...@fdisolutions.com> wrote:
> As many of you may be aware, there is currently an optimisation done on
> accesses to a package array where the index is a constant 0<=i<256:

> Well, with change #22357 this now works with lexical arrays too:
>

> It seems to make such array accesses a lot faster: I got these timings
> (run three times):

You're just doing this to avoid writing that closure doc you
promised... but if it results in things like this (and bison, inplace
sort, misc bug fixes, etc.), keep avoiding it for quite a while longer
:) Care to rewrite reg*.c so Hugo doesn't have to?

Many thanks for all your work.

Dave Mitchell

unread,
Feb 22, 2004, 6:05:21 PM2/22/04
to Yitzchak Scott-Thoennes, perl5-...@perl.org
On Sun, Feb 22, 2004 at 01:48:47PM -0800, Yitzchak Scott-Thoennes wrote:
> You're just doing this to avoid writing that closure doc you
> promised...

D'oh, I was hoping people had forgotten....

> but if it results in things like this (and bison, inplace
> sort, misc bug fixes, etc.), keep avoiding it for quite a while longer
> :) Care to rewrite reg*.c so Hugo doesn't have to?

If I ever offer to do that, please shoot me!

> Many thanks for all your work.

Thanks for the thanks!

--
A walk of a thousand miles begins with a single step...
then continues for another 1,999,999 or so.

0 new messages