Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8302818: Optimize wrapper sets and immutable sets of Enums #12498

Closed
wants to merge 28 commits into from

Conversation

yuantj
Copy link
Contributor

@yuantj yuantj commented Feb 9, 2023

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 Enums.

This PR introduces optimization classes for these situations. No public APIs are changed.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8302818: Optimize wrapper sets and immutable sets of Enums (Enhancement - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/12498/head:pull/12498
$ git checkout pull/12498

Update a local copy of the PR:
$ git checkout pull/12498
$ git pull https://git.openjdk.org/jdk.git pull/12498/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 12498

View PR using the GUI difftool:
$ git pr show -t 12498

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/12498.diff

Webrev

Link to Webrev Comment

Sorry, something went wrong.

@bridgekeeper
Copy link

bridgekeeper bot commented Feb 9, 2023

👋 Welcome back yuantj! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Feb 9, 2023

@yuantj The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Feb 9, 2023
@liach
Copy link
Member

liach commented Feb 9, 2023

Just curious, how does this enum set change affect the performance of construction of regular sets via Set.of calls?

@liach
Copy link
Member

liach commented Feb 9, 2023

On the JBS there's https://bugs.openjdk.org/browse/JDK-8145048 which appears to fit your changes.

@yuantj
Copy link
Contributor Author

yuantj commented Feb 9, 2023

Just curious, how does this enum set change affect the performance of construction of regular sets via Set.of calls?

See ImmutableCollections.setFromArray, it relies on the fact that EnumSet.copyOf will throw a ClassCastException if the elements are not enum constants of a same enum class. Trying to catch an exception is a bit expensive (so maybe I can optimize that).

@yuantj
Copy link
Contributor Author

yuantj commented Feb 9, 2023

On the JBS there's https://bugs.openjdk.org/browse/JDK-8145048 which appears to fit your changes.

I only enhanced EnumSet, but not EnumMap (yet) so I don't think it fits my changes.

@yuantj yuantj marked this pull request as ready for review February 10, 2023 10:27
@yuantj yuantj marked this pull request as draft February 11, 2023 12:58
@liach
Copy link
Member

liach commented Feb 19, 2023

Just took a peek again. Besides the critical error of not throwing IAE for duplicate inputs (which you should probably add new test cases for), I think the 2 implementations share the same toArray hashCode, and the contains logic can be pulled to call an abstract contains(int ordinal) instead.

@yuantj
Copy link
Contributor Author

yuantj commented Feb 19, 2023

@liach Thanks for reviewing. I've fixed the issues you've mentioned.

@liach
Copy link
Member

liach commented Feb 19, 2023

Have you submitted an issue at https://bugreport.java.com/bugreport/ ? It will show up a few days later on the JBS. If not, I can create an issue on the JBS for you too as long as you provide a prompt.

@yuantj
Copy link
Contributor Author

yuantj commented Feb 19, 2023

Have you submitted an issue at https://bugreport.java.com/bugreport/ ? It will show up a few days later on the JBS. If not, I can create an issue on the JBS for you too as long as you provide a prompt.

No I haven't. Submit one for me please. Thanks!

@yuantj yuantj marked this pull request as ready for review February 20, 2023 12:09
@openjdk openjdk bot added the rfr Pull request is ready for review label Feb 20, 2023
@mlbridge
Copy link

mlbridge bot commented Feb 20, 2023

result.addAll(c); // optimized for compatible sets
} else {
while (i.hasNext())
result.add(i.next());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just use addAll in all cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why the original code didn't use addAll, so I didn't change it. 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why the original code didn't use addAll, so I didn't change it. 😂

It seems to be okay to use addAll in all cases. Do we need extra test cases for this?

@bridgekeeper
Copy link

bridgekeeper bot commented Mar 21, 2023

@yuantj This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@cl4es
Copy link
Member

cl4es commented Mar 21, 2023

I'm wary of the impact of adding new wrapper classes. It may make the factory methods slightly slower due additional checks, but also risks increasing the number of classes at various call-sites which might upset call site inlining.

An alternative design which would avoid adding more classes could be to add package-private accessors to the existing Unmodifiable/Synchronized wrapper classes so that EnumSet/-Map can retrieve the underlying set of an unmodifiable or synchronized Set or Map and then use it directly for these bulk operations. Then you'd contain the additional overhead to EnumSet/-Map.

@pavelrappo
Copy link
Member

An alternative design which would avoid adding more classes could be to add package-private accessors to the existing Unmodifiable/Synchronized wrapper classes so that EnumSet/-Map can retrieve the underlying set of an unmodifiable or synchronized Set or Map and then use it directly for these bulk operations. Then you'd contain the additional overhead to EnumSet/-Map.

Another alternative, in cases where arg.getClass() == X.class can be safely substituted with requireNonNull(arg) instanceof X, is a marker interface.

@yuantj
Copy link
Contributor Author

yuantj commented Mar 21, 2023

Thanks again for reviewing. Correct me if I'm wrong.

@cl4es I need to add these new wrapper classes {Unmodifiable,Synchronized}{Regular,Jumbo}EnumSet to implement interfaces {Regular,Jumbo}EnumSetCompatible, just like *RandomAccessList which appear to implement RandomAccess. I don't think removing these wrapper classes and introducing accessors is a good idea, because I will have to check for the wrapper classes in bulk operation methods, which are used more frequently.

@pavelrappo {Unmodifiable,Sychronized}Set also have subclasses implementing SortedSet and NavigableSet, I'm not sure whether instanceof is safe for these subclasses.

@liach
Copy link
Member

liach commented Mar 21, 2023

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.

Copy link
Member

@liach liach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, in JEP 441, there is pattern matching support for different enums implementing one interface in one switch, see the enum constants section there. I wonder how useful this is, and how often do we have interfaces with more than one enum implementations (e.g. StandardOptions/ExtendedOptions)

@Stable
final long[] elements;

@Stable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless

implements Serializable permits ImmutableRegularEnumSet, ImmutableJumboEnumSet {
static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();

@Stable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless

@jdk.internal.ValueBased
static final class ImmutableRegularEnumSet<E extends Enum<E>> extends AbstractImmutableEnumSet<E>
implements RegularEnumSetCompatible<E>, Serializable {
@Stable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless

@cl4es
Copy link
Member

cl4es commented Mar 21, 2023

If this level of complexity is indeed needed to get whatever improvement you're after then I don't see how this can be worth its weight. Microbenchmarking might help support your case here, but assessing the potential performance costs from gradually increasing the number of classes floating around at various call sites in arbitrary applications is hard. Thus it is something we need to be very careful not to do without solid evidence.

I have deleted `{Regular,Jumbo}EnumSetCompatible` interfaces and introduced some package-private methods in `AbstractCollection`. This avoids rarely-used checks from static factories in `Collections` and does not reduce much performance compared against the previous implementation.
@openjdk openjdk bot removed the rfr Pull request is ready for review label Mar 22, 2023
@yuantj
Copy link
Contributor Author

yuantj commented Mar 22, 2023

I have deleted {Regular,Jumbo}EnumSetCompatible interfaces and introduced some package-private methods in AbstractCollection. This avoids rarely-used checks from static factories in Collections and does not reduce much performance compared against the previous implementation.

@openjdk openjdk bot added the rfr Pull request is ready for review label Mar 22, 2023
@mlbridge
Copy link

mlbridge bot commented Mar 22, 2023

Mailing list message from - on core-libs-dev:

Hello Stuart Marks, would you be able to take a look at this patch,
improving performance of enum-only immutable collections, as you are
the primary engineer in this area?

On Mon, Feb 20, 2023 at 9:40?PM Tingjun Yuan <duke at openjdk.org> wrote:

Copy link

@ExE-Boss ExE-Boss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Immutable*ESIterator.lastReturned* fields can be made into locals of Immutable*ESIterator::next(), as the only reason those fields exist in RegularEnumSet.EnumSetIterator and JumboEnumSet.EnumSetIterator is to support Iterator::remove(), which is not a valid operation on immutable enum sets:


private final class ImmutableRESIterator implements Iterator<E> {
long unseen = elements;
long lastReturned = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
long lastReturned = 0;

public E next() {
if (unseen == 0)
throw new NoSuchElementException();
lastReturned = unseen & -unseen;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lastReturned = unseen & -unseen;
long lastReturned = unseen & -unseen;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lastReturned = unseen & -unseen;
long lastReturned = Long.lowestOneBit(unseen);

More descriptive. Same for below.

Comment on lines +1298 to +1299
long lastReturned = 0;
int lastReturnedIndex = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
long lastReturned = 0;
int lastReturnedIndex = 0;

Comment on lines +1314 to +1315
lastReturned = unseen & -unseen;
lastReturnedIndex = unseenIndex;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lastReturned = unseen & -unseen;
lastReturnedIndex = unseenIndex;
long lastReturned = unseen & -unseen;
int lastReturnedIndex = unseenIndex;

@stuart-marks
Copy link
Member

I agree with Claes here in my skepticism of the usefulness of this sort of change. It seems like it's adding lots of complexity. In addition, it also seems like there's a lot of churn in the design. I'd suggest returning this PR to draft status until it can be determined that there's a clear, net improvement.

@yuantj
Copy link
Contributor Author

yuantj commented Mar 22, 2023

@cl4es @stuart-marks Thanks for reviewing and commenting. I'm converting this PR to draft until I finish evaluating whether these changes are necessary or not.

@yuantj yuantj marked this pull request as draft March 22, 2023 16:29
@openjdk openjdk bot removed the rfr Pull request is ready for review label Mar 22, 2023
@bridgekeeper
Copy link

bridgekeeper bot commented May 17, 2023

@yuantj This pull request has been inactive for more than 8 weeks and will be automatically closed if another 8 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 12, 2023

@yuantj This pull request has been inactive for more than 16 weeks and will now be automatically closed. If you would like to continue working on this pull request in the future, feel free to reopen it! This can be done using the /open pull request command.

@bridgekeeper bridgekeeper bot closed this Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

None yet

6 participants