Skip to content

Commit

Permalink
Make 1...10 and 10...1 about 75x as fast
Browse files Browse the repository at this point in the history
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 dfd6450
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Perl6/Optimizer.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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_');
Expand Down

0 comments on commit dfd6450

Please sign in to comment.