Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use simplifier to optimize simple &postcircumfix calls
  • Loading branch information
sorear committed Nov 24, 2010
1 parent 8e193aa commit 1bda7f5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/SAFE.setting
Expand Up @@ -780,6 +780,15 @@ my class Pair is Enum {
}
# }}}
# List utilities {{{
sub _at_pos(\&container, $index) {
$container.defined
?? $container.at-pos($index)
!! Any!Any::butWHENCE(sub (\$var) {
$container.defined && die("Autovivification collision");
$container = Array.new;
$container!Array::extend($index, $var);
});
}
sub postcircumfix:<[ ]>(\$container, $index) { # TODO: is rwtrans
$container.defined
?? $container.at-pos($index)
Expand All @@ -802,6 +811,22 @@ sub postcircumfix:<{ }>(\$container, $key, :$exists, :$delete) {
});
}

sub _exists_key(\$container, $key) {
$container.defined ?? $container.exists-key($key) !! False
}
sub _delete_key(\$container, $key) {
$container.defined ?? $container.delete-key($key) !! Any
}
sub _at_key(\$container, $key) {
$container.defined
?? $container.at-key($key)
!! Any!Any::butWHENCE(sub (\$var) {
$container.defined && die("Autovivification collision");
$container = Hash.new;
$container!Hash::extend($key, $var);
});
}

my class GatherIterator is IterCursor {
has $!frame;
has $!reify;
Expand Down
20 changes: 20 additions & 0 deletions perf/hashmark.pl
@@ -0,0 +1,20 @@
# vim: ft=perl6
sub _exists_key(\$container, $key) {
$container.defined ?? $container.exists-key($key) !! False
}
sub _delete_key(\$container, $key) {
$container.defined ?? $container.delete-key($key) !! Any
}
sub _at_key(\$container, $key) {
$container.defined
?? $container.at-key($key)
!! Any!Any::butWHENCE(sub (\$var) {
$container.defined && die("Autovivification collision");
$container = Hash.new;
$container!Hash::extend($key, $var);
});
}

my $i = 0;
my %hash;
%hash{$i} = $i until ($i++) == 100000;
17 changes: 17 additions & 0 deletions src/Optimizer/Simplifier.pm
Expand Up @@ -41,6 +41,8 @@ our %funcs = (
'&infix:<=>' => \&do_assign,
'&infix:<==>' => \&do_numeq,
'&postfix:<++>' => \&do_postinc,
'&postcircumfix:<{ }>' => \&do_atkey,
'&postcircumfix:<[ ]>' => \&do_atpos,
);

sub do_assign {
Expand Down Expand Up @@ -75,6 +77,21 @@ sub do_numeq {
return Op::Builtin->new(name => 'numeq', args => $args);
}

# TODO: similar conversion for :delete and :exists
sub do_atkey {
my ($body, $nv, $invname, $args) = @_;
return unless @$args == 2;
return Op::CallSub->new(invocant => Op::Lexical->new(name => '&_at_key'),
positionals => $args);
}

sub do_atpos {
my ($body, $nv, $invname, $args) = @_;
return unless @$args == 2;
return Op::CallSub->new(invocant => Op::Lexical->new(name => '&_at_pos'),
positionals => $args);
}

sub run_optree {
my ($body, $op, $nv) = @_;
Carp::confess "WTF" if !defined $nv;
Expand Down

0 comments on commit 1bda7f5

Please sign in to comment.