RFR: 8302818: Optimize wrapper sets and immutable sets of Enums [v5]

Chen Liang liach at openjdk.org
Tue Mar 21 22:44:46 UTC 2023


On Tue, 21 Feb 2023 03:39:46 GMT, Tingjun Yuan <duke at openjdk.org> wrote:

>> Currently, the two subclasses of `java.util.EnumSet` optimize bulk operations when the argument is also a `EnumSet`, but there is no such optimization for wrapper sets (returned by `Collections.unmodifiableSet`, `Collections.synchronizedSet`, etc.) and immutable sets (returned by `Set.of` methods) of `Enum`s.
>> 
>> This PR introduces optimization classes for these situations.  No public APIs are changed.
>
> Tingjun Yuan has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Set.copyOf: need defensive copy

The `RandomAccess` `SortedSet` `NaviagableSet` implementations are public to users while `RegularEnumSetCompatible` and `JumboEnumSetCompatible` aren't. So I guess we can implement another interface for these wrappers to retrieve their backing instances, like:

interface WrapperCollection {
  Collection<?> getBacking();
}

and thus:

interface RegularEnumSetCompatible {
  static RegularEnumSetCompatible tryConvert(Collection<?> coll) {
    if (coll instanceof RegularEnumSetCompatible compat) return compat;
    if (coll instanceof WrapperCollection wrap) return tryConvert(wrap.getBacking());
    return null;
  }
}


Adding extra `getClass() == UnmodifiableRegularEnumSet.class` indeed will affect most users, which rarely are enum set compatibles.

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

PR Comment: https://git.openjdk.org/jdk/pull/12498#issuecomment-1478689026


More information about the core-libs-dev mailing list