performance: arrayElementVarHandle / calculated index / aligned vs unaligned
Per-Ake Minborg
per-ake.minborg at oracle.com
Wed Dec 18 14:13:18 UTC 2024
Hi Matthias!
I've rewritten the benchmark slightly (just to make them "normalized" the way we use to write them) even though your benchmarks work equally well. See attachment. By using the commands
jvmArgsAppend = {
"-XX:+PrintCompilation",
"-XX:+UnlockDiagnosticVMOptions",
"-XX:+PrintInlining" }
in a @Fork annotation, and observing the output, it appears all the methods are inlined properly. So, even though some methods are more complex, it appears they are treated in the same way when it comes to inlining.
By looking at the actual assembly generated for the benchmarks using these commands (for an M1 in my case):
@Fork(value = 1, jvmArgsAppend = {
"-Xbatch",
"-XX:-TieredCompilation",
"-XX:CompileCommand=dontinline,org.openjdk.bench.java.lang.foreign.Alignment::findAligned*",
"-XX:CompileCommand=PrintAssembly,org.openjdk.bench.java.lang.foreign.Alignment::findAligned*"
})
I can see that the C2 compiler is able to unroll the segment access in the findAligned method but not in the findAlignedNext method. This is one reason findAligned is faster. In order to see real assembly, the "hsdis-aarch64.dylib" must be present and it is recommended to use a "fast-debug" version of the JDK. Read more on Jorn Vernee's blog here: https://jornvernee.github.io/hsdis/2022/04/30/hsdis.html
The question then becomes why that is the case. This drives us into another field of expertise where I am not the right person to provide an answer. Generally, there is no guarantee as to how the C2 compiler works and we are improving it continuously. Maybe someone else can provide additional information.
Best, Per Minborg
________________________________
From: panama-dev <panama-dev-retn at openjdk.org> on behalf of Matthias Ernst <matthias at mernst.org>
Sent: Wednesday, December 18, 2024 9:26 AM
To: panama-dev at openjdk.org <panama-dev at openjdk.org>
Subject: performance: arrayElementVarHandle / calculated index / aligned vs unaligned
Hi,
I'm trying to use the foreign memory api to interpret some variable-length encoded data, where an offset vector encodes the start offset of each stride. Accessing element `i` in this case involves reading `offset[i+1]` in addition to `offset[i]`. The offset vector is modeled as a `JAVA_LONG.arrayElementVarHandle()`.
Just out of curiosity about bounds and alignment checks I switched the layout to JAVA_LONG_UNALIGNED for reading (data is still aligned) and I saw a large difference in performance where I didn't expect one, and it seems to boil down to the computed index `endOffset[i+1]` access, not for the `[i]` case. My expectation would have been that all variants exhibit the same performance, since alignment checks would be moved out of the loop.
A micro-benchmark (attached) to demonstrate:
long-aligned memory segment, looping over the same elements in 6 different ways:
{aligned, unaligned} x {segment[i] , segment[i+1], segment[i+1] (w/ base offset) } gives very different results for aligned[i+1] (but not for aligned[i]):
Benchmark Mode Cnt Score Error Units
Alignment.findAligned thrpt 217.050 ops/s
Alignment.findAlignedPlusOne thrpt 110.366 ops/s. <= #####
Alignment.findAlignedNext thrpt 110.377 ops/s. <= #####
Alignment.findUnaligned thrpt 216.591 ops/s
Alignment.findUnalignedPlusOne thrpt 215.843 ops/s
Alignment.findUnalignedNext thrpt 216.483 ops/s
openjdk version "23.0.1" 2024-10-15
OpenJDK Runtime Environment (build 23.0.1+11-39)
OpenJDK 64-Bit Server VM (build 23.0.1+11-39, mixed mode, sharing)
Macbook Air M3
Needless to say that the difference was smaller with more app code in play, but large enough to give me pause. Likely it wouldn't matter at all but I want to have a better idea which design choices to pay attention to. With the foreign memory api, I find it a bit difficult to distinguish convenience from performance-relevant options (e.g. using path expressions vs computed offsets vs using a base offset. Besides "make layouts and varhandles static final" what would be other rules of thumb?)
Thx
Matthias
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20241218/23a11924/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Alignment.java
Type: application/octet-stream
Size: 5825 bytes
Desc: Alignment.java
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20241218/23a11924/Alignment-0001.java>
More information about the panama-dev
mailing list