Skip to content

8337237: Use FFM instead of Unsafe for Java 2D RenderBuffer class#20339

Closed
prrace wants to merge 3 commits intoopenjdk:masterfrom
prrace:ffm_buffer
Closed

8337237: Use FFM instead of Unsafe for Java 2D RenderBuffer class#20339
prrace wants to merge 3 commits intoopenjdk:masterfrom
prrace:ffm_buffer

Conversation

@prrace
Copy link
Contributor

@prrace prrace commented Jul 25, 2024

Migrate from using Unsafe to FFM's MemorySegment API for allocating and setting native memory.
This code is used by Metal, OpenGL and D3D, so I manually tested SwingSet2 and J2Demo as well as running all the usual tests.
I also did some micro-benchmarking on the performance of Unsafe vs MemorySegment.
The performance of either is more than sufficient for us .. ie they could be 10x slower and we wouldn't even notice.
But they are in the same ballpark, and if one or the other is clearly faster it is the new FFM code.


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-8337237: Use FFM instead of Unsafe for Java 2D RenderBuffer class (Bug - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 20339

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 25, 2024

👋 Welcome back prr! 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 Jul 25, 2024

@prrace 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:

8337237: Use FFM instead of Unsafe for Java 2D RenderBuffer class

Reviewed-by: jvernee, jdv

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

  • 3dd07b9: 8336163: Remove declarations of some debug-only methods in release build
  • 720b446: 8335181: Incorrect handling of HTTP/2 GOAWAY frames in HttpClient
  • f132b34: 8336854: CAInterop.java#actalisauthenticationrootca conflicted with /manual and /timeout
  • 90527a5: 8336742: Shenandoah: Add more verbose logging/stats for mark termination attempts
  • 8e682ac: 8338158: Cleanup ShouldNotXXX uses in machnode.cpp
  • ca99f37: 8338156: Fix -Wzero-as-null-pointer-constant warnings in jvmciCompilerToVM.cpp
  • 84c3065: 8335865: Shenandoah: Improve THP pretouch after JDK-8315923
  • 21ca91e: 8300800: UB: Shift exponent 32 is too large for 32-bit type 'int'
  • 58b9570: 8338142: (dc) DatagramChannelImpl.blockingReceive can use untimed-park when no timeout set
  • 6af1d6f: 8335927: Revisit AnnotationConstantValueEntry and AnnotationValue.OfConstant
  • ... and 213 more: https://git.openjdk.org/jdk/compare/476d2ae69d6f67fdf9e2a9353f224141318690f2...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 changed the title 8337237 8337237: Use FFM instead of Unsafe for Java 2D RenderBuffer class Jul 25, 2024
@openjdk openjdk bot added the rfr Pull request is ready for review label Jul 25, 2024
@openjdk
Copy link

openjdk bot commented Jul 25, 2024

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

  • client

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 client client-libs-dev@openjdk.org label Jul 25, 2024
@mlbridge
Copy link

mlbridge bot commented Jul 25, 2024

Webrevs

@minborg
Copy link
Contributor

minborg commented Jul 26, 2024

Most put operations require that the item is properly aligned now that the FFM API is used in conjunction with the aligned ValueLayout instances like JAVA_LONG. Unsafe did not impose this restriction. Is this the intended behavior?

@prrace
Copy link
Contributor Author

prrace commented Jul 26, 2024

Most put operations require that the item is properly aligned now that the FFM API is used in conjunction with the aligned ValueLayout instances like JAVA_LONG. Unsafe did not impose this restriction. Is this the intended behavior?

Yes, we have pre-existing comments saying the caller has to make sure to properly align variables.

@SWinxy
Copy link
Contributor

SWinxy commented Jul 29, 2024

Could this class use {Byte,Short,Integer,Long,Double}.BYTES instead of SIZEOF_{BYTE,SHORT,INT,LONG,DOUBLE} constants? That's what I always write when working with native memory, and I find it less magic-word-y. E.g.:

// putShort(short)
curOffset += Short.BYTES;

// put(short[], ...)
position(position() + length * Short.BYTES);

@prrace
Copy link
Contributor Author

prrace commented Jul 29, 2024

Could this class use {Byte,Short,Integer,Long,Double}.BYTES instead of SIZEOF_{BYTE,SHORT,INT,LONG,DOUBLE} constants? That's what I always write when working with native memory, and I find it less magic-word-y. E.g.:

// putShort(short)
curOffset += Short.BYTES;

// put(short[], ...)
position(position() + length * Short.BYTES);

Note that this code was written about a decade before that field was added to JDK, so it I don't think you can criticise the author not using it.
But I'll use it to update the initialisation of the final fields so the uses don't need to change.

Copy link
Member

@JornVernee JornVernee left a comment

Choose a reason for hiding this comment

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

Latest version looks good to me.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jul 30, 2024
@SWinxy
Copy link
Contributor

SWinxy commented Jul 30, 2024

Note that this code was written about a decade before that field was added to JDK, so it I don't think you can criticise the author not using it.

Wow. I did not realize that. Everything looks good.

Copy link
Member

@jayathirthrao jayathirthrao left a comment

Choose a reason for hiding this comment

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

There are no alignment issues while reading/writing into RenderBuffer, verified that by taking a look at individual Metal/OpenGL/D3D calls.

Also as captured we should not worry about performance impact of these calls as these are minute computations compared to actual rendering logic.

Latest change looks good to me.

@prrace
Copy link
Contributor Author

prrace commented Aug 14, 2024

/integrate

@openjdk
Copy link

openjdk bot commented Aug 14, 2024

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

  • 6a39014: 8338110: Exclude Fingerprinter::do_type from ubsan checks
  • 0e3903f: 8338393: Parallel: Remove unused ParallelCompactData::clear_range
  • d8e4d3f: 8338402: GHA: some of bundles may not get removed
  • a5d948f: 8332842: Optimize empty CopyOnWriteArrayList allocations
  • fbe4f05: 8337976: Insufficient error recovery in parser for switch inside class body
  • 38bd8a3: 8338236: Compile error in cgroup code on Linux when using clang
  • 9fe1777: 8338280: Parallel: Inline ParallelCompactData::verify_clear
  • 66bee25: 8338315: G1: G1CardTableEntryClosure:do_card_ptr remove unused parameter worker_id
  • 3dd07b9: 8336163: Remove declarations of some debug-only methods in release build
  • 720b446: 8335181: Incorrect handling of HTTP/2 GOAWAY frames in HttpClient
  • ... and 221 more: https://git.openjdk.org/jdk/compare/476d2ae69d6f67fdf9e2a9353f224141318690f2...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Aug 14, 2024

@prrace Pushed as commit c0384b6.

💡 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

client client-libs-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

6 participants