Skip to content
Permalink
Browse files

Make 1...10 and 10...1 about 75x as fast

for 1...10_000_000 and 10_000_000...1 respectively

Bypass the whole SEQUENCE mechanism by optimizing internally to 1..10
and (1..10).reverse and profit from those optimizations.
  • Loading branch information...
lizmat committed Sep 25, 2018
1 parent ace87cb commit dfd6450d74c727630ad8e06f1ebfc8361e828c0a
Showing with 14 additions and 1 deletion.
  1. +14 −1 src/Perl6/Optimizer.nqp
@@ -1084,7 +1084,18 @@ class Perl6::Optimizer {
my @ub := get_bound($op[0],-1);
@ub ?? [QAST::IVal.new( :value(0) ), @ub[0]] !! []
},
);
'&infix:<...>', -> $op {
my @lb := get_bound($op[0], 0);
my @ub := get_bound($op[1], 0);
@lb && @ub
?? nqp::istype(@lb[0],QAST::IVal)
&& nqp::istype(@ub[0],QAST::IVal)
&& @lb[0].value > @ub[0].value
?? [@ub[0],@lb[0],1] # 10...1 -> (1..10).reverse
!! [@lb[0],@ub[0]] # 1...10 -> 1..10
!! []
}
);

# Poisonous calls.
my %poison_calls := nqp::hash(
@@ -2255,6 +2266,8 @@ class Perl6::Optimizer {
?? nqp::getattr($code, $block, '$!phasers')
!! nqp::null();
if $count == 1 && nqp::isnull($phasers) && %range_bounds{$c2.name}($c2) -> @bounds {
$reverse := @bounds[2] if $c2.name eq '&infix:<...>';

my $it_var := QAST::Node.unique('range_it_');
my $last_var := QAST::Node.unique('range_last_');
my $callee_var := QAST::Node.unique('range_callee_');

0 comments on commit dfd6450

Please sign in to comment.
You can’t perform that action at this time.