Permalink
Browse files

Optimize &defined as a builtin

  • Loading branch information...
sorear committed Nov 24, 2010
1 parent 1bda7f5 commit 7d54b1c473f57d85bc305539107e0f59e308b1d7
Showing with 17 additions and 6 deletions.
  1. +5 −5 perf/hashmark.pl
  2. +5 −1 src/Op.pm
  3. +7 −0 src/Optimizer/Simplifier.pm
View
@@ -1,20 +1,20 @@
# vim: ft=perl6
sub _exists_key(\$container, $key) {
- $container.defined ?? $container.exists-key($key) !! False
+ defined($container) ?? $container.exists-key($key) !! False
}
sub _delete_key(\$container, $key) {
- $container.defined ?? $container.delete-key($key) !! Any
+ defined($container) ?? $container.delete-key($key) !! Any
}
sub _at_key(\$container, $key) {
- $container.defined
+ defined($container)
?? $container.at-key($key)
!! Any!Any::butWHENCE(sub (\$var) {
- $container.defined && die("Autovivification collision");
+ defined($container) && die("Autovivification collision");
$container = Hash.new;
$container!Hash::extend($key, $var);
});
}
my $i = 0;
my %hash;
-%hash{$i} = $i until ($i++) == 100000;
+%hash{$i} = $i until ($i++) == 1000000;
View
@@ -1190,7 +1190,11 @@ use CgOp;
my ($self, $body) = @_;
no strict 'refs';
my $name = $self->name;
- &{ "CgOp::bif_$name" }(map { $_->cgop($body) } @{ $self->args });
+ my @a = (map { $_->cgop($body) } @{ $self->args });
+ if ($name eq 'defined') {
+ return CgOp::obj_asdef($a[0]);
+ }
+ &{ "CgOp::bif_$name" }(@a);
}
__PACKAGE__->meta->make_immutable;
@@ -43,6 +43,7 @@ our %funcs = (
'&postfix:<++>' => \&do_postinc,
'&postcircumfix:<{ }>' => \&do_atkey,
'&postcircumfix:<[ ]>' => \&do_atpos,
+ '&defined' => \&do_defined,
);
sub do_assign {
@@ -77,6 +78,12 @@ sub do_numeq {
return Op::Builtin->new(name => 'numeq', args => $args);
}
+sub do_defined {
+ my ($body, $nv, $invname, $args) = @_;
+ return unless @$args == 1;
+ return Op::Builtin->new(name => 'defined', args => $args);
+}
+
# TODO: similar conversion for :delete and :exists
sub do_atkey {
my ($body, $nv, $invname, $args) = @_;

0 comments on commit 7d54b1c

Please sign in to comment.