develooper Front page | perl.perl5.porters | Postings from February 2004

extend OP_AELEMFAST optimisation to lexical arrays

Thread Next
From:
Dave Mitchell
Date:
February 22, 2004 08:04
Subject:
extend OP_AELEMFAST optimisation to lexical arrays
Message ID:
20040222160435.GA20408@fdisolutions.com
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".

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About