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

Per Minborg pminborg at openjdk.org
Mon Jan 9 18:37:56 UTC 2023


On Mon, 9 Jan 2023 08:33:09 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.
>
> Viktor Klang has updated the pull request incrementally with two additional commits since the last revision:
> 
>  - 8299444: java.util.Set.copyOf allocates needlessly for empty input collections
>    
>        Modifies ImmutableCollections.listCopy:
>        Introduces a check for isEmpty to avoid allocation in the case of an empty input collection.
>  - 8299444: java.util.Set.copyOf allocates needlessly for empty input collections
>    
>    Modifies Map.copyOf:
>    Introduces a check for isEmpty to avoid allocation in the case of an empty input Map.

On the note of `CHM::isEmpty`: It would be better to rewrite this method as a short-circuitable reduction of the many CounterCells' values. As soon as at least one of them are >0 then the map is not empty. In contrast, today we sum all of the values, in many cases, unnecessary.

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

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


More information about the core-libs-dev mailing list