Unsafe vs MemorySegments / Bounds checking...
Ioannis Tsakpinis
iotsakp at gmail.com
Tue Nov 5 18:36:24 UTC 2024
> FWIW, one more thing you could try is to mask off the sign bit from the
> address value before doing the access through VIRTUAL_MEMORY. You will
> still pay for the bitwise and, but it may be cheaper than the sign check.
Masking off the sign bit is not enough, because if address is equal to
Long.MAX_VALUE, then address & SIGN_MASK == Long.MAX_VALUE again, which
is not a valid offset for a MemorySegment with size == Long.MAX_VALUE
and the JVM cannot eliminate the bound check. That's why the benchmark
uses a mask with two zero high bits and why I mentioned that you lose
access to 3/4ths of the full 64-bit memory address space.
More information about the panama-dev
mailing list