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

Viktor Klang duke at openjdk.org
Tue Jan 24 10:28:07 UTC 2023


On Mon, 9 Jan 2023 18:26:56 GMT, Stuart Marks <smarks at openjdk.org> wrote:

>> 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.
>
> src/java.base/share/classes/java/util/ImmutableCollections.java line 169:
> 
>> 167:     @SuppressWarnings("unchecked")
>> 168:     static <E> List<E> listCopy(Collection<? extends E> coll) {
>> 169:         if (coll instanceof List12 || (coll instanceof ListN && ! ((ListN<?>)coll).allowNulls)) {
> 
> Maybe replace the cast with an instanceof pattern here?

@stuart-marks Sorry, missed this notification. I initially had the same idea, but decided against it because it forces me to suppress "rawtypes" since `coll instanceof ListN` is not considered to be a rawtype, but `coll instanceof ListN c` is. And currently it won't allow for `coll instanceof ListN<E> c`...

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

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


More information about the core-libs-dev mailing list