static field record holding downcall MethodHandle

Duncan Gittins duncan.gittins at gmail.com
Sun Mar 30 16:55:49 UTC 2025


Sorry if this is a silly question. I wanted to check my understanding of
method handles in downcalls.  The example below is from jextract, though I
have similar handwritten sections in my own non-jextract examples. For best
performance, the MethodHandle is declared in static field:

public static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
ADDRESS, JAVA_LONG, JAVA_LONG, ADDRESS);

public static final MemorySegment ADDR = LOOKUP.findOrThrow("qsort");

public static final MethodHandle MH = LINKER.downcallHandle(ADDR, DESC);


Would I get similar performance benefit for qsort MH.invokeExact if I
changed above to use a record for Download fields instead?

record Downcall(MemorySegment address, FunctionDescriptor descriptor,
MethodHandle handle) {

public Downcall(MemorySegment addr, FunctionDescriptor desc) {

this(addr, desc, Linker.nativeLinker().downcallHandle(addr, desc));

}
}

This means the definition is now a one liner:

private static final Downcall CALL = new Downcall(LOOKUP.findOrThrow("qsort"),
FunctionDescriptor.ofVoid(ADDRESS, JAVA_LONG, JAVA_LONG, ADDRESS));

// invoke with CALL.handle.invokeExact(base, nitems, size, compar)

Kind regards


Duncan Gittins
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20250330/df30a822/attachment.htm>


More information about the panama-dev mailing list