Make Iterators.peekingIterator() reuse AbstractIterator.peek() - #3847
Open
cky wants to merge 1 commit into
Open
Conversation
cky
force-pushed
the
make-iterators-peekingiterator-reuse-abstractiterator-peek
branch
2 times, most recently
from
April 7, 2020 00:35
fea75db to
d9fdbe8
Compare
Currently, `Iterators.peekingIterator()` will treat `AbstractIterator` just like any other iterator, and will call `next()` to get the element, which it then stashes. This is wasteful, since `AbstractIterator` already provides a `peek()` method that is designed to be compatible with the `PeekingIterator` interface, so use it instead. `AbstractIterator` subclasses `UnmodifiableIterator`, so we can do likewise with the `PeekingIterator` forwarder. The resulting forwarder is much more lightweight than the default `Iterators.peekingIterator()` implementation.
cky
force-pushed
the
make-iterators-peekingiterator-reuse-abstractiterator-peek
branch
from
April 7, 2020 01:24
d9fdbe8 to
5bbe4d4
Compare
vztot
approved these changes
Apr 23, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Currently,
Iterators.peekingIterator()will treatAbstractIteratorjust like any other iterator, and will callnext()to get the element, which it then stashes. This is wasteful, sinceAbstractIteratoralready provides apeek()method that is designed to be compatible with thePeekingIteratorinterface, so use it instead.AbstractIteratorsubclassesUnmodifiableIterator, so we can do likewise with thePeekingIteratorforwarder. The resulting forwarder is much more lightweight than the defaultIterators.peekingIterator()implementation.