Skip to content

Conversation

@dain
Copy link
Member

@dain dain commented Jan 31, 2026

Replace sun.misc.Unsafe with VarHandle for memory access in XxHash64.

Changes

  • Replace Unsafe.getLong/getInt/getByte with VarHandle.get()
  • Replace Unsafe.copyMemory with System.arraycopy
  • Add public hash(byte[]) API for direct byte array hashing
  • Inline static hash implementation to avoid method call overhead

Performance

Inlining the updateBody method in the implementation was necessary to avoid an 18% regression at 64 bytes with VarHandle. After inlining, performance is equivalent for the byte[] API. The Slice API has a slight regression
at 64 bytes (~1ns per invocation) which I suspect is due to interaction between VarHandle bounds checking and Slice wrapper methods.

Size unsafe_bytes unsafe_slice varhandle_bytes varhandle_slice
8B 1.4 ns 1.6 ns 1.9 ns 1.6 ns
64B 6.5 ns 6.8 ns 6.5 ns 7.8 ns
1KB 65 ns 65 ns 63 ns 64 ns
64KB 3,968 ns 4,065 ns 4,032 ns 4,065 ns

@dain dain requested review from electrum, martint and wendigo January 31, 2026 06:41
@dain dain force-pushed the xxhash-varhandle branch from 52ada04 to b19dc33 Compare January 31, 2026 08:55
@dain dain force-pushed the xxhash-varhandle branch from b19dc33 to ca82b7d Compare February 1, 2026 03:28
@dain dain force-pushed the xxhash-varhandle branch from ca82b7d to 3b3c589 Compare February 1, 2026 03:42
@dain dain merged commit 58fa235 into airlift:master Feb 1, 2026
1 check passed
@dain dain deleted the xxhash-varhandle branch February 1, 2026 06:36
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.

2 participants