Skip to content

Commit

Permalink
Make lazy gather up to 15% faster
Browse files Browse the repository at this point in the history
By using nqp::ifnull to see whether there has been an iterator created
already.  The benchmark is for:

    lazy gather { .take for ^500000 }

so very light on what is actually been gathered.  So your actual mileage
will vary.
  • Loading branch information
lizmat committed Dec 25, 2019
1 parent 55d0143 commit 971174f
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/core.c/Iterable.pm6
Expand Up @@ -85,19 +85,24 @@ my role Iterable {
has $!iterator;

method new(\iterable) {
nqp::p6bindattrinvres(
nqp::create(self),self,'$!iterable',iterable
)
my \iter := nqp::create(self);
nqp::bindattr(iter,self,'$!iterable',iterable);
nqp::bindattr(iter,self,'$!iterator',nqp::null);
iter
}

method pull-one() is raw {
$!iterator := $!iterable.iterator unless $!iterator.DEFINITE;
$!iterator.pull-one
nqp::ifnull(
$!iterator,
$!iterator := $!iterable.iterator
).pull-one
}

method push-exactly(\target, int $n) {
$!iterator := $!iterable.iterator unless $!iterator.DEFINITE;
$!iterator.push-exactly(target, $n);
nqp::ifnull(
$!iterator,
$!iterator := $!iterable.iterator
).push-exactly(target, $n);
}

method is-lazy(--> True) { }
Expand Down

0 comments on commit 971174f

Please sign in to comment.