develooper Front page | perl.perl5.porters | Postings from September 2012

[PATCH] hash assign speed improvements

Thread Next
From:
Ruslan Zakirov
Date:
September 5, 2012 10:23
Subject:
[PATCH] hash assign speed improvements
Message ID:
CAMOxC8sFEw1GVnkfBWiXz2zPhKGAvmm5HWOAvXWrycY60+gi5Q@mail.gmail.com
Hello,

My goal was to improve the following perl code:

my %args = (
    option => 'default',
    @_
);

Noticed that aassign op just stores values from left to right. In
scalar context op does "if_exists" check on top to calculate
duplicates. In array context it's even more complicated.

I in the patch I've split scalar and void cases without any magic on
hash and implemented reverse loop. New code walks from right to left
and uses HV_FETCH_LVALUE | HV_FETCH_EMPTY_HE to get all required
structures and information to decide whether to store entry or not and
to store it without additional hv_* calls.

What should I do to get it applied into core?

Benchmark results:

           long void    167070.13    164361.78       1.65
          long array     71407.41     71137.20       0.38
         long scalar    163249.04    153363.34       6.45
      long_dup array    155761.01    154963.39       0.51
     long_dup scalar    481229.33    207464.73     131.96
       long_dup void    508155.84    278565.14      82.42
         short array    388833.06    399981.22      -2.79
        short scalar   1025353.33    979928.77       4.64
          short void   1168535.43   1183844.18      -1.29
     short_dup array    331987.02    342546.29      -3.08
    short_dup scalar    922557.53    731192.85      26.17
      short_dup void   1019730.52    901985.06      13.05

Benchmark script:

use Benchmark 'timethese';

my %array = (
    short     => [ 1..4      ],
    long      => [ 1..30     ],
    short_dup => [ (1..4)x2  ],
    long_dup  => [ (1..4)x15 ],
);

my $count = -30;

while ( my ($aname, $array)  = each %array ) {
    timethese( $count, {
        "$aname void" => sub {
            my %h = @$array;
            return;
        },
        "$aname scalar" => sub {
            my $r = (my %h = @$array);
            return;
        },
        "$aname array" => sub {
            my @r = (my %h = @$array);
            return;
        },
    } );
}

In case somebody interested I have stats on aassign in an app.

-- 
Best regards, Ruslan.

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