Performance impact of *_UNALIGNED when segment is aligned.
Per-Ake Minborg
per-ake.minborg at oracle.com
Mon May 19 11:46:56 UTC 2025
Hi Lee, and thank you for your questions.
Generally, the impact of unaligned/aligned access by the CPU using scalar or vector load or store operations depends on many factors, and it is neither desirable nor possible to document all variants. On some newer platforms, there is no significant difference for scalar operations in most cases, whereas there might be a significant difference on other platforms. Some operations must operate on aligned memory (e.g., CAS operations).
If you are using an aligned layout (such as JAVA_INT), the alignment constraint must be asserted for each access, whereas this is not the case for the unaligned counterparts (e.g., JAVA_INT_UNALIGNED). So, generally, specifying unaligned access is faster. However, in many cases, the JIT compiler can hoist alignment checking making the difference smaller or even insignificant.
Vector operations, on the other hand, are impacted by alignment and cache line crossing. Emanuel Peter has analyzed the impact of alignment with vector operations in this comprehensive analysis<https://github.com/openjdk/jdk/pull/25065>. In short, if you are working with the Vector API, you may also want to worry about alignment, because there can be a significant performance impact (30%+ in some cases).
Best,
Per Minborg
________________________________
From: panama-dev <panama-dev-retn at openjdk.org> on behalf of Lee Rhodes <leerho at gmail.com>
Sent: Thursday, May 15, 2025 6:55 PM
To: panama-dev at openjdk.org <panama-dev at openjdk.org>
Subject: Performance impact of *_UNALIGNED when segment is aligned.
The DataSketches library can be handed segments from other applications and cannot guarantee the underlying memory alignment of these segments and it would be too burdensome to require a specific alignment. As I result, the library will have to always use *_UNALIGNED layouts when accessing these segments.
>From the Javadocs I understand that there can be a performance impact accessing *_UNALIGNED values in a segment that is not appropriately aligned. For example, accessing JAVA_LONG values in a MemorySegment.ofArray(byte[]). This leads me to a couple of questions related to performance and alignment that don't seem to be answered in the documentation:
* Is there a performance impact of using *_UNALIGNED layouts on segments that are fortuitously properly aligned, instead of being configured with the proper alignment?
* Is the performance of underlying vector operations used on segment bulk operations impacted by alignment? For example, are the vector operations disabled if the segment alignment doesn't match an array element natural alignment? Or are they just slower?
Lee.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20250519/1c504b96/attachment.htm>
More information about the panama-dev
mailing list