Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use simplifier to optimize simple &postcircumfix calls

  • Loading branch information...
commit 1bda7f52a11431359e4dd26aaf8b6a69c8c4af45 1 parent 8e193aa
@sorear authored
Showing with 62 additions and 0 deletions.
  1. +25 −0 lib/SAFE.setting
  2. +20 −0 perf/hashmark.pl
  3. +17 −0 src/Optimizer/Simplifier.pm
View
25 lib/SAFE.setting
@@ -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)
@@ -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;
View
20 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;
View
17 src/Optimizer/Simplifier.pm
@@ -41,6 +41,8 @@ our %funcs = (
'&infix:<=>' => \&do_assign,
'&infix:<==>' => \&do_numeq,
'&postfix:<++>' => \&do_postinc,
+ '&postcircumfix:<{ }>' => \&do_atkey,
+ '&postcircumfix:<[ ]>' => \&do_atpos,
);
sub do_assign {
@@ -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;
Please sign in to comment.
Something went wrong with that request. Please try again.