Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Iterators.peekingIterator() reuse AbstractIterator.peek() #3847

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cky
Copy link

@cky cky commented Apr 7, 2020

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 cky force-pushed the make-iterators-peekingiterator-reuse-abstractiterator-peek branch 2 times, most recently from fea75db to d9fdbe8 Compare April 7, 2020 00:35
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 cky force-pushed the make-iterators-peekingiterator-reuse-abstractiterator-peek branch from d9fdbe8 to 5bbe4d4 Compare April 7, 2020 01:24
@cgdecker cgdecker added the P3 label Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants