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

8314294: Unsafe::allocateMemory and Unsafe::freeMemory are slower than malloc/free #15977

Closed

Conversation

dholmes-ora
Copy link
Member

@dholmes-ora dholmes-ora commented Sep 29, 2023

To increase performance by avoiding a thread-state transition (native -> in_vm) we change the three "raw" allocation functions in Unsafe to be UNSAFE_LEAF rather than UNSAFE_ENTRY.

It is hard to track through all the related code to prove this is a safe change, but I could not spot anything obvious and testing indicated no issues (my main concern was potential missing WXWrite on macOS Aarch64).

Testing:

  • tiers 1-7 on linux and macos x64 and Aarch64, plus Windows x64

Thanks


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-8314294: Unsafe::allocateMemory and Unsafe::freeMemory are slower than malloc/free (Enhancement - P3)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 15977

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 29, 2023

👋 Welcome back dholmes! 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 29, 2023
@openjdk
Copy link

openjdk bot commented Sep 29, 2023

@dholmes-ora The following label will be automatically applied to this pull request:

  • hotspot

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 hotspot hotspot-dev@openjdk.org label Sep 29, 2023
@mlbridge
Copy link

mlbridge bot commented Sep 29, 2023

Webrevs

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

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

Looks reasonable. I clicked through some of the os::{malloc,realloc,free} implementations, and nothing pops out as requiring the VM mode.

@openjdk
Copy link

openjdk bot commented Sep 29, 2023

@dholmes-ora 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:

8314294: Unsafe::allocateMemory and Unsafe::freeMemory are slower than malloc/free

Reviewed-by: shade, dlong

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 76 new commits pushed to the master branch:

  • c45308a: 8301327: convert assert to guarantee in Handle_IDiv_Exception
  • bd7bb67: 8317257: RISC-V: llvm build broken
  • 0259da9: 8301686: TLS 1.3 handshake fails if server_name doesn't match resuming session
  • 355811a: 8316559: Refactor some util/Calendar tests to JUnit
  • ecb5e8a: 8317264: Pattern.Bound has static fields that should be static final.
  • f2c221d: 8317119: Remove unused imports in the java.util.stream package
  • 09dad0e: 8313810: BoxLayout uses
    instead of list for layout options
  • 73a47f0: 4622866: javax.swing.text.Document.remove(int, int) has a misleading picture
  • cfcbfc6: 8316879: RegionMatches1Tests fails if CompactStrings are disabled after JDK-8302163
  • ca5eee2: 8316445: Mark com/sun/management/HotSpotDiagnosticMXBean/CheckOrigin.java as vm.flagless
  • ... and 66 more: https://git.openjdk.org/jdk/compare/a2391a92cd09630cc3c46024f7e02924a997cc86...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 Sep 29, 2023
Copy link
Member

@dean-long dean-long left a comment

Choose a reason for hiding this comment

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

Looks good.

@mcimadamore
Copy link
Contributor

Thanks for taking care of this @dholmes-ora. Do you know if Unsafe::copyMemory, or Unsafe::setMemory can also receive same treatment? These are bulk operations, so they are less sensitive to the transition cost - but for small copies it can still be a factor.

@dholmes-ora
Copy link
Member Author

Thanks for the reviews @shipilev and @dean-long !

@dholmes-ora
Copy link
Member Author

@mcimadamore setMemory and copyMemory are targeting Java arrays not native memory so they have to be safepoint-aware and so cannot be leaf operations.

@dholmes-ora
Copy link
Member Author

/integrate

@openjdk
Copy link

openjdk bot commented Oct 3, 2023

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

  • 6e1aacd: 8296631: NSS tests failing on OL9 linux-aarch64 hosts
  • d2e2c4c: 8309667: TLS handshake fails because of ConcurrentModificationException in PKCS12KeyStore.engineGetEntry
  • e25121d: 8316929: Shenandoah: Shenandoah degenerated GC and full GC need to cleanup old OopMapCache entries
  • 5c8366e: 8268622: Performance issues in javac Name class
  • ad81abd: 8317034: Remove redundant type cast in the java.util.stream package
  • d7d1d42: 8316771: Krb5.java has not defined messages for all error codes
  • f985006: 8309356: Read files in includedir in alphanumeric order
  • a564d43: 8315692: Parallelize gc/stress/TestStressRSetCoarsening.java test
  • 878d27d: 8317273: compiler/codecache/OverflowCodeCacheTest.java fails transiently on Graal
  • 2637e8d: 8317314: Remove unimplemented ObjArrayKlass::oop_oop_iterate_elements_bounded
  • ... and 96 more: https://git.openjdk.org/jdk/compare/a2391a92cd09630cc3c46024f7e02924a997cc86...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Oct 3, 2023

@dholmes-ora Pushed as commit 26c21f5.

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

@dholmes-ora dholmes-ora deleted the 8314294-unsafe-alloc-perf branch October 3, 2023 05:46
@mcimadamore
Copy link
Contributor

@mcimadamore setMemory and copyMemory are targeting Java arrays not native memory so they have to be safepoint-aware and so cannot be leaf operations.

Makes sense. Thanks!

@dean-long
Copy link
Member

@mcimadamore setMemory and copyMemory are targeting Java arrays not native memory so they have to be safepoint-aware and so cannot be leaf operations.

I think the key requirement is that they not run in the "native" state. If we had a compiler intrinsic then it should be able to run in the in_Java state. Offering a safepoint seems optional, depending on how much data is being modified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot hotspot-dev@openjdk.org integrated Pull request has been integrated
4 participants