Unsafe vs MemorySegments / Bounds checking...

Ioannis Tsakpinis iotsakp at gmail.com
Tue Nov 5 14:56:50 UTC 2024


> The alignment check can be disabled, by using a layout that is not
> aligned (e.g. JAVA_LONG_UNALIGNED - you seem to be using that here?).

Yes, it tests both aligned and unaligned layouts, with different masks
that eliminate the bounds check, or the alignment check, or both. The
cost is a single AND instruction that seems to be pipelined nicely with
minimal overhead. And of course you lose access to 3/4ths of the 64-bit
memory address space, but that shouldn't be a problem in practice.

Using _UNALIGNED is an easy way to avoid the alignment check, but I
wanted to highlight the impact because of the "scary" javadoc ("they
may induce performance and portability issues") which makes it sound
like the aligned layouts are more efficient and someone might use those
when comparing with Unsafe.

> It's a bit more complex than that, as that code really likes having a
> single implementation for all the liveness checks - otherwise you get
> bad profile pollution when multiple session kinds are involved. We're
> working on a solution:

Interesting, thanks!


More information about the panama-dev mailing list