Unsafe making too strong assumptions about array alignment?

Andrew Haley aph-open at littlepinkcloud.com
Mon May 26 10:40:15 UTC 2025


On 5/22/25 18:00, Maurizio Cimadamore wrote:
> In other words, we are using the most general possible Unsafe access
> primitive -- because we don't know what the underlying alignment will
> be. Note also that `putLongUnaligned` is a VM intrinsics, and in most
> cases there will be no real performance difference between this and the
> aligned variant (so, in most cases, just using the unaligned primitive
> is better/more general, which is why both ByteBuffer and FFM do that).
> 
> I hope this helps clarifying the confusion?

I'm not sure it does.

There's a theoretical possibility that on a system with strict alignment 
but 32-bit-aligned byte arrays, this code

      public final void putLongUnaligned(Object o, long offset, long x) {
          if ((offset & 7) == 0) {
              putLong(o, offset, x);

might perform a misaligned store. The obvious response is that such 
systems must not allocate objects at less than the maximum (presumably 
64-bit) alignment.

-- 
Andrew Haley  (he/him)
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
https://keybase.io/andrewhaley
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671


More information about the panama-dev mailing list