8299444: java.util.Set.copyOf allocates needlessly for empty input collections
Reviewed-by: rriggs, shade, smarks
This commit is contained in:
parent
d98a323a8b
commit
a2a7703370
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -166,10 +166,12 @@ class ImmutableCollections {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
static <E> List<E> listCopy(Collection<? extends E> coll) {
|
||||
if (coll instanceof List12 || (coll instanceof ListN && ! ((ListN<?>)coll).allowNulls)) {
|
||||
if (coll instanceof List12 || (coll instanceof ListN<?> c && !c.allowNulls)) {
|
||||
return (List<E>)coll;
|
||||
} else if (coll.isEmpty()) { // implicit nullcheck of coll
|
||||
return List.of();
|
||||
} else {
|
||||
return (List<E>)List.of(coll.toArray()); // implicit nullcheck of coll
|
||||
return (List<E>)List.of(coll.toArray());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1738,6 +1738,8 @@ public interface Map<K, V> {
|
||||
static <K, V> Map<K, V> copyOf(Map<? extends K, ? extends V> map) {
|
||||
if (map instanceof ImmutableCollections.AbstractImmutableMap) {
|
||||
return (Map<K,V>)map;
|
||||
} else if (map.isEmpty()) { // Implicit nullcheck of map
|
||||
return Map.of();
|
||||
} else {
|
||||
return (Map<K,V>)Map.ofEntries(map.entrySet().toArray(new Entry[0]));
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -728,6 +728,8 @@ public interface Set<E> extends Collection<E> {
|
||||
static <E> Set<E> copyOf(Collection<? extends E> coll) {
|
||||
if (coll instanceof ImmutableCollections.AbstractImmutableSet) {
|
||||
return (Set<E>)coll;
|
||||
} else if (coll.isEmpty()) { // Implicit nullcheck of coll
|
||||
return Set.of();
|
||||
} else {
|
||||
return (Set<E>)Set.of(new HashSet<>(coll).toArray());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user