[collect] Optimize Iterables.get() for ImmutableCollection#7888
[collect] Optimize Iterables.get() for ImmutableCollection#7888Yannic wants to merge 1 commit intogoogle:masterfrom
Iterables.get() for ImmutableCollection#7888Conversation
|
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 CollectionsIterables.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 typeImmutableCollectionthat allows access to the element inO(1)instead ofO(n). This extends the existing optimization that allowed constant-time access for iterables of typeList.