More Optimizations in Perl 5.20-to-be

In a recent post on his blog Matthew Horsfall explained a few of the optimizations to Perl that he contributed recently. On this site, we reported on the fantastic work that Dave Mitchell is doing. The two of them inspired me to try my hand at this optimization game at a recent hackathon at work by improving the Perl optimizer.

sassign-+-add-+-padsv[$b]
| `-const[3]
`-padsv[$a]
padsv[$b]   (push $b on stack)
const[3] (push 3 on stack)
add (pop 3 and $b off of stack, push their sum on stack)
padsv[$a] (push $a on stack)
sassign (pop $a and the sum off of stack, set $a)
aslice-+-pushmark
|-list-+-pushmark
| |-const[1]
| `-const[2]
`-padav[@x]
pushmark (outer)
pushmark (inner)
const[1]
const[2]
list
padav
aslice
aslice-+-pushmark
|-const[1]
|-const[2]
`-padav[@x]
pushmark
const[1]
const[2]
padav
aslice
use 5.14.2;
use warnings;
use Dumbbench;
use Dumbbench::CPUFrequencyPinner;

my $opt = '/path/to/optimized/perl';
my $unopt = '/path/to/unoptimized/perl';

my $code = <<'HERE';
my @x = (1..8);
my @y;
@y = @x[3,7] for 1..1e6;
HERE

my $db = Dumbbench->new(
target_rel_precision => 0.005,
initial_runs => 20,
);

$db->add_instances(
Dumbbench::Instance::Cmd->new(command => [$unopt, '-e', $code],
name => "before"),
Dumbbench::Instance::Cmd->new(command => [$opt, '-e', $code],
name => "after"),
);

SCOPE: {
# Pin CPU frequency down to reduce variability
my $p = Dumbbench::CPUFrequencyPinner->new;
$SIG{INT} = sub {undef $p; exit;};
$p->set_max_frequencies($p->min_frequencies->[0]);
$db->run;
}

$db->report;
$db->box_plot->show();

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store