static field record holding downcall MethodHandle
Per-Ake Minborg
per-ake.minborg at oracle.com
Mon Mar 31 06:24:27 UTC 2025
Hi Duncan,
As components in a record are trusted by the VM, this will give similar performance.
If you always use LOOKUP, then you could perhaps create an even more easy-to-use constructor where you just provide the name of the native function (e.g. "qsort") and the FunctionDescription.
Best, Per
________________________________
From: panama-dev <panama-dev-retn at openjdk.org> on behalf of Duncan Gittins <duncan.gittins at gmail.com>
Sent: Sunday, March 30, 2025 9:55 AM
To: panama-dev <panama-dev at openjdk.org>
Subject: static field record holding downcall MethodHandle
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/20250331/47718f19/attachment-0001.htm>
More information about the panama-dev
mailing list