Skip to content

Commit

Permalink
Merge pull request #1844 from MasterDuke17/array_slice_optimization
Browse files Browse the repository at this point in the history
Rewrite @A[1,3] to @a.AT-POS(1),@a.AT-POS(3) for literal indices
  • Loading branch information
lizmat committed Aug 6, 2018
2 parents 4522132 + c1044bc commit 85050ac
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Perl6/Optimizer.nqp
Expand Up @@ -1397,6 +1397,27 @@ class Perl6::Optimizer {
return self.optimize_array_variable_initialization($op);
}

# Turn `@a[1, 3]` into `@a.AT-POS(1), @a.AT-POS(3)`
elsif
$!level > 0
&& ($optype eq 'call' || $optype eq 'callstatic')

# workaround because op_eq_core needs to be "primed"
&& ((try $!symbols.find_lexical($op.name)) || 1)

&& self.op_eq_core($op, '&postcircumfix:<[ ]>')

# if it's 3 that means the slice is on the LHS of an assignment
&& +@($op) == 2

&& nqp::istype($op[0], QAST::Var)
&& nqp::substr($op[0].name, 0, 1) eq '@'
&& nqp::istype($op[1], QAST::WVal)
&& nqp::istype($op[1].value, $!symbols.find_in_setting("List"))
{
return self.optimize_array_slice($op);
}

# Calls are especially interesting as we may wish to do some
# kind of inlining.
elsif $optype eq 'call' {
Expand Down Expand Up @@ -1501,6 +1522,27 @@ class Perl6::Optimizer {
}
}

method optimize_array_slice($op) {
unless nqp::isconcrete($op[1].value) {
return $op
}

my $reified := nqp::getattr($op[1].value, $!symbols.find_in_setting("List"), '$!reified');

unless $reified.HOW.name($reified) eq 'BOOTArray' {
return $op
}

my $new_op := QAST::Op.new(:op('callstatic'), :name('&infix:<,>'));
my $array_var := $op[0];

for $reified -> $var {
$new_op.push: QAST::Op.new(:op('callmethod'), :name('AT-POS'), $array_var, QAST::WVal.new(:value($var)));
}

$new_op;
}

method optimize_array_variable_initialization($op) {
my $Positional := $!symbols.find_in_setting('Positional');
if $op[0].returns =:= $Positional {
Expand Down

0 comments on commit 85050ac

Please sign in to comment.