Skip to content

Commit

Permalink
IterateOneWithPhasers handles empty iterators 20% faster
Browse files Browse the repository at this point in the history
By letting .new return the Empty iterator if the first pull fails.

Example code: my @A; for @A { NEXT Nil }
  • Loading branch information
lizmat committed Jul 15, 2021
1 parent d828172 commit 2efe430
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/core.c/Any-iterable-methods.pm6
Expand Up @@ -63,15 +63,11 @@ augment class Any {
has $!LAST;

method new(\block, \source, \label) {
my $iter := nqp::create(self);

nqp::if(
nqp::eqaddr((my $pulled := source.pull-one),IterationEnd),
nqp::stmts( # nothing to do
nqp::bindattr($iter,self,'$!slipper',nqp::null),
nqp::bindattr($iter,self,'$!pulled',IterationEnd)
),
nqp::stmts( # iterate at least once
Rakudo::Iterator.Empty, # nothing to do
nqp::stmts( # iterate at least once
(my $iter := nqp::create(self)),
nqp::if(
nqp::istype($pulled,Slip),
nqp::stmts( # Set up a slipper and get next value
Expand All @@ -94,10 +90,9 @@ augment class Any {
block.callable_for_phaser('NEXT') // nqp::null),
nqp::bindattr($iter,self,'$!LAST',
block.callable_for_phaser('LAST')),
$iter
)
);

$iter
)
}

method is-lazy() { $!source.is-lazy }
Expand Down

0 comments on commit 2efe430

Please sign in to comment.