Skip to content

Commit

Permalink
Re-instate old behaviour of List.sum
Browse files Browse the repository at this point in the history
And also do *NOT* special case [+] anymore.  With new-disp, that
optimization was probably questionable anyway.  Should this show
any performance regressions, we can look at that later again, or
wait until RakuAST's grammar has landed.

This should address #5209
and keep #5205 happy as well.
  • Loading branch information
lizmat committed Feb 16, 2023
1 parent 92d485e commit 6b6c0ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
22 changes: 9 additions & 13 deletions src/core.c/List.pm6
Expand Up @@ -370,24 +370,20 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
method to(List:D:) { self.elems ?? self[self.end].to !! Nil }
method from(List:D:) { self.elems ?? self[0].from !! Nil }

multi method sum(List:D \SELF:) {
multi method sum(List:D:) {
nqp::if(
self.is-lazy,
self.fail-iterator-cannot-be-lazy('.sum'),
nqp::if(
(my int $elems = self.elems), # reify
nqp::if(
nqp::iscont(SELF),
$elems,
nqp::stmts(
(my $sum := 0),
(my int $i = -1),
nqp::while(
nqp::islt_i(++$i,$elems),
($sum := $sum + nqp::ifnull(nqp::atpos($!reified,$i),0))
),
$sum
)
nqp::stmts(
(my $sum := 0),
(my int $i = -1),
nqp::while(
nqp::islt_i(++$i,$elems),
($sum := $sum + nqp::ifnull(nqp::atpos($!reified,$i),0))
),
$sum
)
)
)
Expand Down
38 changes: 18 additions & 20 deletions src/core.c/metaops.pm6
Expand Up @@ -198,31 +198,29 @@ multi sub METAOP_REDUCE_LEFT(\op) {
}
}
else {
nqp::eqaddr(op,&infix:<+>)
?? &sum
!! sub (+values) {
my $iter := values.iterator;
sub (+values) {
my $iter := values.iterator;
nqp::if(
nqp::eqaddr((my $result := $iter.pull-one),IterationEnd),
op.(), # identity
nqp::if(
nqp::eqaddr((my $result := $iter.pull-one),IterationEnd),
op.(), # identity
nqp::eqaddr((my $value := $iter.pull-one),IterationEnd),
nqp::if(
nqp::eqaddr((my $value := $iter.pull-one),IterationEnd),
nqp::if(
nqp::isle_i(op.arity,1),
op.($result), # can call with 1 param
$result # what we got
nqp::isle_i(op.arity,1),
op.($result), # can call with 1 param
$result # what we got
),
nqp::stmts(
($result := op.($result,$value)),
nqp::until(
nqp::eqaddr(($value := $iter.pull-one),IterationEnd),
($result := op.($result,$value))
),
nqp::stmts(
($result := op.($result,$value)),
nqp::until(
nqp::eqaddr(($value := $iter.pull-one),IterationEnd),
($result := op.($result,$value))
),
$result # final result
)
$result # final result
)
)
}
)
}
}
}

Expand Down

0 comments on commit 6b6c0ec

Please sign in to comment.