Skip to content

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

Open
cky wants to merge 1 commit into
google:masterfrom
cky:make-iterators-peekingiterator-reuse-abstractiterator-peek
Open

Make Iterators.peekingIterator() reuse AbstractIterator.peek()#3847
cky wants to merge 1 commit into
google:masterfrom
cky:make-iterators-peekingiterator-reuse-abstractiterator-peek

Conversation

@cky

@cky cky commented Apr 7, 2020

Copy link
Copy Markdown

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 no SLO label Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants