Skip to content

Commit

Permalink
Streamline the basic Iterator.push-exactly
Browse files Browse the repository at this point in the history
Instead of counting up to the given number, copy the given number and count
down with it, which simplifies the end check.  Makes it about 8% faster.
  • Loading branch information
lizmat committed Mar 27, 2018
1 parent 8ad7eb3 commit 9d784dd
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/core/Iterator.pm6
Expand Up @@ -27,16 +27,16 @@ my role Iterator {
# pushed, or IterationEnd if it reached the end of the iteration.
method push-exactly($target, int $n) {
nqp::stmts(
(my int $i = -1),
(my int $todo = nqp::add_i($n,1)),
nqp::until( # doesn't sink
nqp::isge_i($i = nqp::add_i($i,1),$n)
nqp::not_i($todo = nqp::sub_i($todo,1))
|| nqp::eqaddr((my $pulled := self.pull-one),IterationEnd),
$target.push($pulled) # don't .sink $pulled here, it can be a Seq
),
nqp::if(
nqp::eqaddr($pulled,IterationEnd),
IterationEnd,
$i
$n
)
)
}
Expand Down

3 comments on commit 9d784dd

@b2gills
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the returned value should be $n - $todo

@lizmat
Copy link
Contributor Author

@lizmat lizmat commented on 9d784dd Mar 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. The return of push-exactly should be:

Returns the number of things pushed, or IterationEnd if it reached the end of the iteration.

If it reached the end of the iterator, then IterationEnd is returned. Otherwise it returns the number of items pushed: if it didn't reach the end, that would be the number of things requested, aka $n

Or am I missing something?

@b2gills
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right right, I didn't pay attention to the if statement that surrounds the return value. Nevermind.

To be fair, it would still be correct as $todo would always be 0 at that statement.

Please sign in to comment.