Skip to content

eliminate sun.misc.Unsafe package dependency - #1055

Open
ChristThePhone wants to merge 2 commits into
msgpack:mainfrom
ChristThePhone:eliminate_sun_unsafe
Open

ChristThePhone wants to merge 2 commits into
msgpack:mainfrom
ChristThePhone:eliminate_sun_unsafe

Conversation

@ChristThePhone

@ChristThePhone ChristThePhone commented Dec 3, 2026 •

Copy link
Copy Markdown

In order to address #1046 with the help of AI :

sun.misc.Unsafe and the other JDK-internal APIs (sun.nio.ch.DirectBuffer, reflection into DirectByteBuffer) are gone from the code. ./sbt test passes for both modules, including the performance-vs-v6 assertions in MessageUnpackerTest. I only ran on JDK 21. The build targets Java 8, so I used only Java 8 APIs, but I didn't run it on JDK 8.

MessageBuffer is now a single ByteBuffer-backed class. It stores a big-endian slice and uses absolute get/put for primitives. For array-backed buffers, bulk copies go through System.arraycopy. Direct buffers use a duplicate() view, so a shared buffer's position and limit are never changed.

I deleted MessageBufferU, MessageBufferBE, DirectBufferAccess and DirectBufferAccessTest. With no Unsafe path left, the universal and big-endian variants had no purpose.

Behavior changes:

  • Public API removed: MessageBufferU and MessageBufferBE were public classes, so code that referenced them will no longer compile.
  • MessageBuffer.releaseBuffer(): it is kept but deprecated, and it now does nothing. It used to free direct-buffer memory through Unsafe.invokeCleaner, so direct memory is now freed only by the garbage collector.
  • Bounds checks: out-of-range access now throws instead of reading or writing raw memory.
  • Read-only heap buffers: wrap(ByteBuffer) still rejects them with IllegalArgumentException, because ByteStringTest depends on that.
  • sliceAsByteBuffer(): the result is now a true slice, with position 0 and an arrayOffset for array-backed buffers. Before, it was a wrap with a non-zero position. The existing tests didn't need changes for this.

Other edits:

  • Build config: I removed the --add-opens test JVM options and the !sun.* OSGi import from build.sbt.
  • CI: I removed the msgpack.universal-buffer CI step from CI.yml.
  • Docs: I removed the Java 17 --add-opens section from README.md and updated CLAUDE.md. I left RELEASE_NOTES.md alone, since those entries are historical.
  • Tests: I dropped the universal conditionals from MessageBufferTest, MessageUnpackerTest and ByteStringTest, so direct buffers are always tested. I also renamed the "unsafe" benchmark labels.

Harm Osterndorf added 2 commits December 3, 2026 13:57
@ChristThePhone

Copy link
Copy Markdown
Author

All runs used a fresh sbt cache, so the tests actually ran: 18 of 18 suites passed, with cached 0, no failures and no errors.

JDK JVM Result
8 (1.8.0_472) OpenJ9 pass
11 (11.0.24) OpenJ9 pass, with the direct-buffer timing warning
17 (17.0.21) OpenJ9 pass
21 HotSpot pass
23 (23.0.2) OpenJ9 pass
25 (25.0.1) HotSpot pass
  • Warning on JDK 11: the timing warning fired again. Direct buffer took 1.072 ms against 1.047 ms for v6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant