[11630] Iterator#flatMap#hasNext calls outer#hasNext 1 time, not 2-3 times #8220
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes scala/bug#11630
When doing two calls in succession to
Iterator#flatMap
:the status quo is to have the flatmapped iterator call the sub-iterator's
hasNext
3 times:hasNext
to the outside callernext()
This PR replaces that logic with a system where these 3 calls are replaced with a single call during the
it.hasNext
phase, which is achieved by caching the state of the iterator in a new private field_hasNext: Int
which can either be:0
= known to be false (hasNext is known to be false)1
= known to be true (hasNext is known to be true true)-1
= unknown (not known if hasNext would be true or false)The resulting state of the iterator after a
hasNext
call is either0
or1
. After a call tonext()
we won't know if we have exhausted the entire iterator or not, since we are not forcing any more work to be done, so we set our state to-1
.This partially was a port of the old 2.12 implementation, but that implementation still required 2 calls to
cur.hasNext
, whereas this PR represents the additional improvement to only callcur.hasNext
once during ahasNext(); next();
call sequence.TODO