Aligning the source of a FloatVector?
Per-Ake Minborg
per-ake.minborg at oracle.com
Fri Sep 26 10:50:29 UTC 2025
The simple answer is no. As you rightfully point out, a float array can be moved at any time by the GC and can then acquire other alignment properties compared to the original location.
I am not sure of your use case, but you might be able to use a MemorySegment instead? If you create a native memory segment, you can set your own alignment (e.g., it can be super-aligned to 64-byte boundaries), and it will never move.
Best, Per
________________________________
From: panama-dev <panama-dev-retn at openjdk.org> on behalf of Peter Kessler OS <peter.kessler at os.amperecomputing.com>
Sent: Friday, September 26, 2025 1:42 AM
To: panama-dev at openjdk.org <panama-dev at openjdk.org>
Cc: Eric Caspole <eric.caspole at oracle.com>
Subject: Aligning the source of a FloatVector?
Is there way to control the alignment of `float[]` so that it loads nicely into a FloatVector?
`jdk.incubator.vector.FloatVector.fromArray(VectorSpecies<Float>, float[], int)` creates a `FloatVector` from some elements of a `float[]`. On a machine with 4 float vector lanes, that call reads 4 float values from the `float[]`. If the 4 values from the array are properly aligned relative to a cache line boundary -- and if the cache line width is a multiple of the number of lanes -- then everything goes smoothly because every `FloatVector.fromArray` call will read from only one cache line. If the values in the array are not aligned relative to a cache line boundary, then some of the `FloatVector.fromArray` calls will have to read from 2 cache lines. On some processors, reads from 2 cache lines will take longer than reads from a single cache line.
The JVM promises to align objects on 8-byte boundaries (and a user can ask for coarser alignment with `-XX:ObjectAlignmentInBytes=` in powers of 2, at the expense of wasting memory and cache space to "dark matter" between objects). But the JVM makes no promises about the alignment of elements within an array, other than that they will be "naturally aligned" for the type of the element.
I can discover the alignment of the elements of a `float[]` by noting the time taken to read them into `FloatVector` instances. And I can make use of that alignment information to pad the array to get advantageous alignment for the elements. But any garbage collection could move the array to a different alignment (maintaining `ObjectAlignmentInBytes`) which would negate any padding I might have added.
Is there a way (for example, an annotation) that will align the elements of an array such that vector reads will not cross cache line boundaries? The alignment mechanism will have to take into account the size of the object header (with and without compressed class pointers), the length field for the array, and any padding added to naturally align the elements of the array. A Java programmer will have no idea about those details. But they might be willing to annotate an array that they were going to use with `FloatVector`.
... peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20250926/99edaa8f/attachment-0001.htm>
More information about the panama-dev
mailing list