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

Stuart Marks smarks at openjdk.org
Wed Jan 4 19:41:48 UTC 2023


On Wed, 4 Jan 2023 14:41:20 GMT, Viktor Klang <duke 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.

There's no regression test. However, with the current code (prior to this change) a call to `Set.of(zeroLengthArray)` returns the same instance as `Set.of()`, so it's difficult to write a simple functional test for this change. The test would have to assert that "no HashSet is allocated along this code path" which is much harder to test and frankly probably isn't worth it. So, please add one of the `noreg-*` labels to the bug to indicate the rationale for omitting a regression test.

https://openjdk.org/guide/#jbs-label-dictionary

I'd probably add the `Map.copyOf` change to this PR to avoid some bug/PR/review overhead. Thanks for mentioning this @shipilev.

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

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


More information about the core-libs-dev mailing list