Skip to content

Commit

Permalink
Re-order to restore for 1..1000 {} optimization.
Browse files Browse the repository at this point in the history
More inlining possibilities led to 1..1000 style things being turned
into code no longer recognized as a loop over a range, so we lost the
optimization. Move it to before any inlining attempts are made.
timotimo++ for spotting this optimization regression.
  • Loading branch information
jnthn committed Aug 1, 2014
1 parent a8c752b commit 69eb2f8
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/Perl6/Optimizer.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,17 @@ class Perl6::Optimizer {
if $optype eq 'handle' {
return self.visit_handle($op);
}

# If it's a for 1..1000000 { } we can simplify it to a while loop. We
# check this here, before the tree is transformed by call inline opts.
if $optype eq 'callmethod' && $op.name eq 'sink' &&
nqp::istype($op[0], QAST::Op) && $op[0].op eq 'callmethod' && $op[0].name eq 'map' && @($op[0]) == 2 &&
nqp::istype((my $c1 := $op[0][0]), QAST::Op) && $c1.name eq '&infix:<,>' &&
nqp::istype((my $c2 := $op[0][0][0]), QAST::Op) && nqp::existskey(%range_bounds, $c2.name) &&
$!symbols.is_from_core($c2.name) {
self.optimize_for_range($op, $c2);
return $op;
}

# A chain with exactly two children can become the op itself.
if $optype eq 'chain' {
Expand Down Expand Up @@ -1093,15 +1104,6 @@ class Perl6::Optimizer {
self.optimize_private_method_call($op);
}

# If it's a for 1..1000000 { } we can simplify it to a while loop.
elsif $optype eq 'callmethod' && $op.name eq 'sink' &&
nqp::istype($op[0], QAST::Op) && $op[0].op eq 'callmethod' && $op[0].name eq 'map' && @($op[0]) == 2 &&
nqp::istype((my $c1 := $op[0][0]), QAST::Op) && $c1.name eq '&infix:<,>' &&
nqp::istype((my $c2 := $op[0][0][0]), QAST::Op) && nqp::existskey(%range_bounds, $c2.name) &&
$!symbols.is_from_core($c2.name) {
self.optimize_for_range($op, $c2);
}

# If we end up here, just leave op as is.
if $op.op eq 'chain' {
$!chain_depth := $!chain_depth - 1;
Expand Down

0 comments on commit 69eb2f8

Please sign in to comment.