google / guice

Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 11 and above, brought to you by Google.
https://github.com/google/guice
Apache License 2.0
12.51k stars 1.67k forks source link

Change RealMultibinder#doProvision to use ImmutableSet#buildWithExpectedSize, and avoid an extra T[] array allocation when permitDuplicates==true. #1789

Closed copybara-service[bot] closed 10 months ago

copybara-service[bot] commented 10 months ago

Change RealMultibinder#doProvision to use ImmutableSet#buildWithExpectedSize, and avoid an extra T[] array allocation when permitDuplicates==true.

Since the normal usage should be that the items in the values array are all unique, and ImmutableSet#copyOf will create an initial list of max(4, sqrt(length)) if length>4, but then grows that list to length (or larger) if the array was all unique (and then resize is back down during build()).

While here, the T[] values array is also only needed to be filled in when permitDuplicates==false (for the error message if duplicates were detected).