-
Notifications
You must be signed in to change notification settings - Fork 11.1k
[collect] Optimize Iterables.get()
for ImmutableCollection
#7888
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
base: master
Are you sure you want to change the base?
Conversation
Hi Guava team, I've read through the contribution guidelines, which suggest creating an issue first for adding significant new features. I think this feature is small enough and has an uncontroversial API, so I went ahead and created the PR right away given that I did not have to spend signifiant time on the implementation. If you have things you'd like to discuss, I'm happy to do so either here or I can open an issue for this. Thanks, |
Would it work to use |
Sorry, let me put a little more usefully:
|
Rather than adding a new method, perhaps you could optimize the existing [ |
This change adds a new method `Collections2.getElement(Collection, int)`, which returns the i-th element of the collection. Unlike existing methods via Java `Stream` or `Iterable`, the method in this change supports fast access for `ImmutableCollection`s backed by an internal array in `O(1)` instead of `O(n)` with the existing approach. Users can use this method to, e.g., retrieve a random element from `ImmutableSet` (e.g., to select a random node to connect to from a pool of nodes in a distributed system).
d5742ed
to
94e2948
Compare
i-th
element of Collection
sIterables.get()
for ImmutableCollection
Moved the code to I'm a bit on the fence for I think this needs some benchmarking before it should be merged. Would you mind already running CI on it please? |
This change adds a fast-path to
Iterables.get()
if the iterable is of typeImmutableCollection
that allows access to the element inO(1)
instead ofO(n)
. This extends the existing optimization that allowed constant-time access for iterables of typeList
.