Performance of memory var handles in hot loops
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Tue Apr 7 14:04:15 UTC 2020
Doh!
I didn't notice the benchmark at the bottom of the email.
Yes, Vlad is correct, currently MemoryAddress::addOffset is to be
avoided like the plague in performance sensitive code.
The best way to get performant code is to use an indexed var handle -
which you can get by creating the sequence layout you want, and then
obtaining a VarHandle from that e.g.
VarHandle indexedHandle = MemoryLayout.ofSequence(N,
MemoryLayouts.JAVA_DOUBLE).varHandle(double.class,
PathElement.sequenceElement());
This gives you a VarHandle which takes a MemoryAddress _and_ a long
index (the logical index of the element into segment viewed as an double
array).
when you do your loop, it is then important that you use a loop like this:
for (int i = 0 ; i < SIZE ; i++) {
double d = (double)indexedHandle.get(baseAddress, (long)i);
}
That is, please refrain from using a long loop variable (as doing so
will run into more performance issues).
Of course, longer term C2 will get better, and a lot of these
"recommendations" will go away, but that's where we are now.
P.S.
I'm also pretty sure that, while the code above can match Unsafe for
'int' carriers, the alignment check introduced for other carriers might
cause some performance degradation. That's another performance pothole
we're aware of.
Maurizio
On 07/04/2020 14:54, Vladimir Ivanov wrote:
> Thanks for the feedback, Antoine!
>
> I just briefly looked through the benchmark yet, but the first
> question is: have you considered using indexed memory accessors
> instead of MemoryAddress::addOffset()?
>
> Best regards,
> Vladimir Ivanov
>
> On 07.04.2020 14:57, Antoine Chambille wrote:
>> @Benchmark
>> public void scalarSegment(Data state) {
>> final MemoryAddress ia = state.inputMA;
>> final MemoryAddress oa = state.outputMA;
>> for(int i = 0; i < SIZE; i++) {
>> MH.set(oa.addOffset(8*i),
>> (double) MH.get(ia.addOffset(8*i)) +
>> (double) MH.get(oa.addOffset(8*i)));
>> }
>> }
More information about the panama-dev
mailing list