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

8315850: Improve AbstractMap anonymous Iterator classes #15615

Closed
wants to merge 5 commits into from

Conversation

minborg
Copy link
Contributor

@minborg minborg commented Sep 7, 2023

This PR proposes to slightly improve some iterators of AbstractMap:

  • Declare two fields final
  • Use distinct classes rather than anonymous classes

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-8315850: Improve AbstractMap anonymous Iterator classes (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 15615

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 7, 2023

👋 Welcome back pminborg! 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 openjdk bot added the rfr Pull request is ready for review label Sep 7, 2023
@openjdk
Copy link

openjdk bot commented Sep 7, 2023

@minborg 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 Sep 7, 2023
@mlbridge
Copy link

mlbridge bot commented Sep 7, 2023

Webrevs

@forax
Copy link
Member

forax commented Sep 7, 2023

Hello,
In Java, sharing code may have a runtime cost (or not) depending on the shape of the code, because the VM may have to speculate on the class of some value at runtime.
Here, in hasNext() and remove(), the VM has to find the class of the field 'i' at runtime.

Iterators are performance sentive (they are used in for loop) and for that they need the escape analysis to work.
If the iterator code is polymorphic, so part of the code may be unknown so the escape analysis will consider that the iterator escape. So usually, it's a good practice to not share the iterator code.

Also the field should be package private (or even maybe private) but not protected. Protected is very rare in Java because usually classes that share implementation details are in the same package (or in the same nestmate nest).

@stuart-marks
Copy link
Member

stuart-marks commented Sep 7, 2023

Note, I had asked @minborg to remove the @Override annotations. They're generally not used in the collections framework, and where they are, they add a lot of clutter but not much value.

There are still some @Override annotations in the iterator class hierarchy. Once you pull them out, you can use (non-standard) compact formatting, that is, putting the method implementation on the same line as the signature. This makes the classes consume much less vertical space. This formatting style is also used frequently in the collections framework. See HashMap for an example of this:

https://github.com/openjdk/jdk/blob/jdk-21%2B35/src/java.base/share/classes/java/util/HashMap.java#L1626

@stuart-marks
Copy link
Member

@forax Note that HashMap uses a similar subclassing idiom for the iterators of the keySet, values, and entrySet collections in order to share the iterator logic:

https://github.com/openjdk/jdk/blob/jdk-21%2B35/src/java.base/share/classes/java/util/HashMap.java#L1626

@forax
Copy link
Member

forax commented Sep 7, 2023

Hi @stuart-marks ,
for HashMap, all the fields of HashIterator have only one implementation, in this patch, the field of AbstractIterator is typed Iterator (so multiple implementations at runtime). This is not the same code shape.

I think the best is to write a simple to test that iterate over both the keySet() and the values() of an implementation of AbstractMap that only defines size() and get() and use -XX:+PrintInlining to see if the VM reports a profile pollution for the code before and after this change.

@altrisi
Copy link
Contributor

altrisi commented Sep 8, 2023

With the recent changes this ends up just moving the iterator classes around (from anonymous to inner) and making a field final. I don't understand how fully splitting them (vs the previous AbstractIterator revision) helps against polymorphism, given they both would store the same Iterator types in the field (whatever the result of entrySet is), and the next method is two different implementations anyway, that I'd assume would be treated differently. Though I don't know enough about the JIT (or benchmarked this) to know if it does make a difference.

@forax
Copy link
Member

forax commented Sep 8, 2023

@altrisi,
the profile stored by the VM is attached to a specific bytecode, if you share that bytecode, you share the profile.

see https://wiki.openjdk.org/display/HotSpot/MethodData

@@ -924,4 +896,21 @@ public String toString() {
public <T> T[] toArray(T[] a) { return view().toArray(a); }
public String toString() { return view().toString(); }
}

// Iterator implementations.
// Here, we avoid polymorphism to ensure full VM optimization capabilities.
Copy link
Member

Choose a reason for hiding this comment

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

I would remove this comment about avoiding polymorphism, since I think it's a total red herring and is potentially confusing. Otherwise this is fine.

@openjdk
Copy link

openjdk bot commented Oct 11, 2023

@minborg This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8315850: Improve AbstractMap anonymous Iterator classes

Reviewed-by: liach, smarks

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 456 new commits pushed to the master branch:

  • 424de29: 8317866: replace NET_SocketAvailable
  • 6d6c900: 8038244: (fs) Check return value of malloc in Java_sun_nio_fs_AixNativeDispatcher_getmntctl()
  • eca6ea4: 8317873: Add @sealedGraph to IllegalFormatException
  • 2edf9c3: 8317763: Follow-up to AVX512 intrinsics for Arrays.sort() PR
  • 839cb19: 8317581: [s390x] Multiple test failure with LockingMode=2
  • 387896f: 8309621: [XWayland][Screencast] screen capture failure with sun.java2d.uiScale other than 1
  • 8d2ad2b: 8317977: update problemlist to include MacOS for sun/security/tools/keytool/NssTest.java
  • b92de54: 8317964: java/awt/Mouse/MouseModifiersUnitTest/MouseModifiersUnitTest_Standard.java fails on macosx-all after JDK-8317751
  • 2a80160: 8314283: Support for NSS tests on aarch64 platforms
  • 3f6d016: 8314896: additional clarifications to reversed() default methods' implementation requirements
  • ... and 446 more: https://git.openjdk.org/jdk/compare/726c9c977dbaab75a2df4a931e3414ccabb7db44...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 11, 2023
@minborg
Copy link
Contributor Author

minborg commented Oct 12, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Oct 12, 2023

Going to push as commit d95b548.
Since your change was applied there have been 456 commits pushed to the master branch:

  • 424de29: 8317866: replace NET_SocketAvailable
  • 6d6c900: 8038244: (fs) Check return value of malloc in Java_sun_nio_fs_AixNativeDispatcher_getmntctl()
  • eca6ea4: 8317873: Add @sealedGraph to IllegalFormatException
  • 2edf9c3: 8317763: Follow-up to AVX512 intrinsics for Arrays.sort() PR
  • 839cb19: 8317581: [s390x] Multiple test failure with LockingMode=2
  • 387896f: 8309621: [XWayland][Screencast] screen capture failure with sun.java2d.uiScale other than 1
  • 8d2ad2b: 8317977: update problemlist to include MacOS for sun/security/tools/keytool/NssTest.java
  • b92de54: 8317964: java/awt/Mouse/MouseModifiersUnitTest/MouseModifiersUnitTest_Standard.java fails on macosx-all after JDK-8317751
  • 2a80160: 8314283: Support for NSS tests on aarch64 platforms
  • 3f6d016: 8314896: additional clarifications to reversed() default methods' implementation requirements
  • ... and 446 more: https://git.openjdk.org/jdk/compare/726c9c977dbaab75a2df4a931e3414ccabb7db44...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Oct 12, 2023
@openjdk openjdk bot closed this Oct 12, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Oct 12, 2023
@openjdk
Copy link

openjdk bot commented Oct 12, 2023

@minborg Pushed as commit d95b548.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

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 integrated Pull request has been integrated
5 participants