Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Streamline METAOP_REDUCE_RIGHT 2 param op version
- rewrite using nqp ops
- remove use of iterator: all the values are already in a lowlevel list
- 30% faster for 2 elems, 50% for 3+ elem cases
  • Loading branch information
lizmat committed Mar 2, 2017
1 parent 9d138b9 commit 60a8f9e
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/core/metaops.pm
Expand Up @@ -275,18 +275,28 @@ multi sub METAOP_REDUCE_RIGHT(\op) {
}
else {
sub (+values) {
my \iter = values.reverse.iterator;
my \first = iter.pull-one;
return op.() if nqp::eqaddr(first,IterationEnd);

my \second = iter.pull-one;
return op.(first) if nqp::eqaddr(second,IterationEnd);

my $result := op.(second, first);
until nqp::eqaddr((my \value = iter.pull-one),IterationEnd) {
$result := op.(value, $result);
}
$result;
nqp::if(
nqp::isgt_i((my int $i = values.elems),1), # reifies
nqp::stmts(
(my $result := nqp::atpos(
nqp::getattr(values,List,'$!reified'),
($i = nqp::sub_i($i,1))
)),
nqp::while(
nqp::isge_i(($i = nqp::sub_i($i,1)),0),
($result := op.(
nqp::atpos(nqp::getattr(values,List,'$!reified'),$i),
$result
))
),
$result
),
nqp::if(
$i,
op.(nqp::atpos(nqp::getattr(values,List,'$!reified'),0)),
op.()
)
)
}
}
}
Expand Down

0 comments on commit 60a8f9e

Please sign in to comment.