Skip to content

review/unsafeaccess jdk25 - #127

Draft
bmarwell wants to merge 6 commits into
OpenHFT:eafrom
bmarwell:review/unsafeaccess-jdk25
Draft

bmarwell wants to merge 6 commits into
OpenHFT:eafrom
bmarwell:review/unsafeaccess-jdk25

Conversation

@bmarwell

@bmarwell bmarwell commented Oct 2, 2026 •

Copy link
Copy Markdown

AI generated, unreviewed from me.

  • Add RuntimeSupport JVM version gate for Unsafe use
  • Add Unsafe-free heap array access backend
  • Use HeapAccess so JDK 25+ never instantiates Unsafe

bmarwell and others added 3 commits October 2, 2026 15:48
- cache feature version once; Unsafe only below JDK 25
- net.openhft.hashing.useUnsafe=false forces the safe backend

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- SafeHeapAccess: allocation-free native-order view of arrays
- HeapAccess selects backend and base offsets once (ADR-007)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- hash facades and XXH3 go through HeapAccess
- UnsafeAccess refuses to initialise on the safe backend
- raw memory APIs throw UnsupportedOperationException on 25+
- strings use UnknownJvmStringHash on 25+
- tests guarded; docs and ADR-007 updated

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bmarwell
bmarwell requested a review from a team as a code owner October 2, 2026 13:48
@bmarwell
bmarwell marked this pull request as draft October 2, 2026 13:49
bmarwell and others added 3 commits October 2, 2026 16:01
- note historical unsafeHash/OBJECT_ACCESS names
- document UnsupportedOperationException on JDK 25+ raw-memory APIs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- ForeignRawAccess: Unsafe.getX(null, addr) as FFM method handles
- HeapAccess.rawMemoryAccess() used by hashMemory entry points
- Access.unsafe() still throws on the safe backend
- tests on JDK 25/26; docs and ADR-007 updated

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bmarwell

bmarwell commented Oct 2, 2026

Copy link
Copy Markdown
Author

Known gaps on JDK 25+ (unsafe-free backend)

None of the performance points below are measured yet.

  • Access.unsafe(): public API, so kept for compatibility; the library itself never calls it. It now throws UnsupportedOperationException on the safe backend, because object + field-offset access (the Pair example in its Javadoc) cannot be expressed without Unsafe. FFM has no equivalent for ordinary objects.
  • SafeHeapAccess reads: it only implements getByte, so getLong is composed from eight byte reads. Likely slower than one Unsafe.getLong unless C2 merges the loads; most visible on short inputs and 8-byte-stride algorithms (XXH3, wyhash, metro). JDK < 25 is unchanged.
  • MemorySegment.ofArray(): not used. It allocates a segment object per call, which breaks the zero-allocation guarantee unless escape analysis removes it (not guaranteed, unmeasured).
  • Likely fix, not built: for byte[], a byteArrayViewVarHandle read (Java 9+, reached reflectively to keep the Java 8 build) should give a single load. Same-width array types could be indexed directly. To be decided after benchmarking.
  • Direct buffers: hashBytes(ByteBuffer) uses ByteBufferAccess on the safe path. The old fast path read the native address through the internal sun.nio.ch.DirectBuffer and hashed it via Unsafe. Avoiding that internal API costs us the fast path; FFM would need a segment per call (allocation).
  • Strings: String.value is a private field, which FFM cannot read; the old path used reflection plus Unsafe offsets. JDK 25+ therefore uses UnknownJvmStringHash (CharSequence reads, charAt per element): correct and allocation-free, but slower for long strings.
  • FFM raw memory (hashMemory): uses the restricted MemorySegment.reinterpret, so JDK 24+ warns once unless --enable-native-access=ALL-UNNAMED is set. Tested on x86 Linux (JDK 25, 26) only; untested on AIX ppc64 / Linux ppc64le. Addresses >= 2^63 are unsupported.

This branch has not been deployed

No deployments
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