static field record holding downcall MethodHandle

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Mar 31 08:48:11 UTC 2025


Yes, you need a constant root (e.g. static final field), followed by one 
or more "trusted" final edges.


The record provides the trusted edges. But you still need to store the 
record in a static final field (which you have done).


Note that, while we could do what you propose in jextract's generated 
code, the record creation is still eager (so it needs to run at class 
initialization time). Jextract code goes to some length to make sure 
that only the downcall handles that are used are actually initialized.

Once we have the stable value API, we might be able to do a lot better here.


Cheers
Maurizio


On 31/03/2025 07:33, Remi Forax wrote:
> I would complete saying that it's static + final which is important.
> static only or final only are not enough for the VM to consider the 
> field as a constant.
>
> regards,
> Rémi
>
> ------------------------------------------------------------------------
>
>     *From: *"Per-Ake Minborg" <per-ake.minborg at oracle.com>
>     *To: *"duncan gittins" <duncan.gittins at gmail.com>, "panama-dev"
>     <panama-dev at openjdk.org>
>     *Sent: *Monday, March 31, 2025 8:24:27 AM
>     *Subject: *Re: static field record holding downcall MethodHandle
>
>     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:
>
>     publicstaticfinalFunctionDescriptor DESC=
>     FunctionDescriptor.ofVoid(ADDRESS, JAVA_LONG, JAVA_LONG, ADDRESS);
>
>     publicstaticfinalMemorySegment ADDR= LOOKUP.findOrThrow("qsort");
>
>     publicstaticfinalMethodHandle 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?
>
>     recordDowncall(MemorySegment address, FunctionDescriptor
>     descriptor, MethodHandle handle) {
>
>     publicDowncall(MemorySegment addr, FunctionDescriptor desc) {
>
>     this(addr, desc, Linker.nativeLinker().downcallHandle(addr, desc));
>
>     }
>
>     }
>     This means the definition is now a one liner:
>
>     privatestaticfinalDowncallCALL =
>     newDowncall(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/c373df0c/attachment-0001.htm>


More information about the panama-dev mailing list