Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Optimize .flat somewhat.
This cuts a further 20% off "for 1..100 X 1..100 { }".
  • Loading branch information
jnthn committed Sep 14, 2015
1 parent 7e265b6 commit 57a7f6a
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/core/Iterable.pm
Expand Up @@ -26,17 +26,29 @@ my role Iterable {
iter
}

my constant NO_RESULT_YET = Mu.CREATE;
method pull-one() is rw {
my $result;
loop {
my $result := NO_RESULT_YET;
my $got;
repeat while nqp::eqaddr($result, NO_RESULT_YET) {
if $!nested-iter {
$result := $!nested-iter.pull-one();
last unless $result =:= IterationEnd;
$!nested-iter := Iterator;
$got := $!nested-iter.pull-one();
if nqp::eqaddr($got, IterationEnd) {
$!nested-iter := Iterator;
}
else {
$result := $got;
}
}
else {
$got := $!source.pull-one();
if nqp::istype($got, Iterable) && !nqp::iscont($got) {
$!nested-iter := $got.flat.iterator;
}
else {
$result := $got;
}
}
$result := $!source.pull-one();
last unless nqp::istype($result, Iterable) && !nqp::iscont($result);
$!nested-iter := $result.flat.iterator;
}
$result
}
Expand Down

0 comments on commit 57a7f6a

Please sign in to comment.