RFR: 6323374: (coll) Optimize Collections.unmodifiable* and synchronized*
Claes Redestad
redestad at openjdk.java.net
Tue Feb 16 23:23:47 UTC 2021
On Tue, 16 Feb 2021 21:57:43 GMT, Ian Graves <igraves at openjdk.org> wrote:
> Modify the `unmodifiable*` methods in `java.util.Collections` to be idempotent. That is, when given an immutable collection from `java.util.ImmutableCollections` or `java.util.Collections`, these methods will return the reference instead of creating a new immutable collection that wraps the existing one.
Changes requested by redestad (Reviewer).
src/java.base/share/classes/java/util/Collections.java line 1130:
> 1128: public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
> 1129: if(s.getClass() == UnmodifiableSet.class ||
> 1130: s.getClass() == ImmutableCollections.Set12.class ||
These might be problematic: `ImmutableCollections.Set*` differs in behavior subtly from `UnmodifiableSet`, e.g., `set.contains(null)` will throw an NPE. I'd limit the check and optimization to `UnmodifiableSet` here
src/java.base/share/classes/java/util/Collections.java line 1315:
> 1313: public static <T> List<T> unmodifiableList(List<? extends T> list) {
> 1314: if (list.getClass() == UnmodifiableList.class || list.getClass() == UnmodifiableRandomAccessList.class ||
> 1315: list.getClass() == ImmutableCollections.List12.class ||
Similar issue here
src/java.base/share/classes/java/util/Collections.java line 1473:
> 1471: public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) {
> 1472: if(m.getClass() == UnmodifiableMap.class ||
> 1473: m.getClass() == ImmutableCollections.Map1.class ||
And here.
-------------
PR: https://git.openjdk.java.net/jdk/pull/2596
More information about the core-libs-dev
mailing list