RFR: 6323374: (coll) Optimize Collections.unmodifiable* and synchronized*

Claes Redestad redestad at openjdk.java.net
Wed Feb 17 14:17:39 UTC 2021


On Wed, 17 Feb 2021 02:27:57 GMT, liach <github.com+7806504+liach at openjdk.org> wrote:

>> 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
>
> No? This unmodifiable set here just delegates call to the backing field `c`, so all exceptions from `c`'s calls are just delegated, aren't they? The NPE will still be thrown; it's just that the stack trace will be different (i.e. one more level of call in the stacktrace)

Yes, my bad. There might be more issues similar to the one @michaelhixson points out elsewhere in this PR, so a thorough review is probably needed here.

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

PR: https://git.openjdk.java.net/jdk/pull/2596


More information about the core-libs-dev mailing list