FFM API: EA failure with 4+ address parameters

Ioannis Tsakpinis iotsakp at gmail.com
Wed Oct 23 15:51:02 UTC 2024


Hello,

I've been migrating LWJGL to FFM and during testing of some real-world
code (Vulkan rendering), I noticed a spike in GC activity with the FFM
implementation, compared to the old JNI implementation. I think I've
been able to track it down to what looks like an odd failure of escape
analysis. Specifically, scalar replacement no longer occurs when
calling functions with 4 or more ADDRESS (MemorySegment) parameters.

JMH benchmark (you'll need to build a shared library with some empty
functions and run with GC profiling enabled) that demonstrates the
issue:

https://gist.github.com/Spasi/71d5cfa687a1dbe95b3fce608d31ae6b

There are functions with arity from 0 to 5. When invoking a downcall,
raw pointer values are wrapped with MemorySegment.ofAddress() and
passed to each argument. The exact pointer values do not matter.

Until 3 ADDRESS parameters, or 3 ADDRESS parameters plus other non-
ADDRESS parameters, everything is fine and there's no GC activity.
With 4 or more ADDRESS parameters, allocations are no longer
eliminated. The problem also occurs with a single MemorySegment that
is passed to all parameters:

var p = MemorySegment.ofAddress(42L);
functionMH.invokeExact(p, p, p, p);

Any idea what might be causing this?

- Ioannis


More information about the panama-dev mailing list