RFR: 8299444 java.util.Set.copyOf allocates needlessly for empty input collections

Viktor Klang duke at openjdk.org
Wed Jan 4 15:51:49 UTC 2023


On Wed, 4 Jan 2023 15:20:10 GMT, Pavel Rappo <prappo at openjdk.org> wrote:

>> Currently Set.copyOf allocates both a HashSet and a new empty array when the input collection is empty.
>> 
>> This patch avoids allocating anything for the case where the parameter collection's isEmpty returns true.
>
> Curious: how bad was that "needless allocation" that it was required to be optimized away?

@pavelrappo Fair question! I guess it depends on what dominates the benchmark: given that the copyOf methods goes to some length to try to avoid allocating new instances of the immutable sets the proposed patch seemed like rather low-hanging fruit. I found this as I was running some (to be fair, rather specific) benches which drew my attention to it.

-------------

PR: https://git.openjdk.org/jdk/pull/11847


More information about the core-libs-dev mailing list