static field record holding downcall MethodHandle

Duncan Gittins duncan.gittins at gmail.com
Mon Mar 31 10:06:42 UTC 2025


Thanks for the info Maurizio, Per and Rémi

I did try out that record in my own modifications to jextract and it
reduces the jar sizes, but as you suggest its better to review with Stable
Value API in due course.

Kind regards


Duncan

On Mon, 31 Mar 2025, 09:48 Maurizio Cimadamore, <
maurizio.cimadamore at oracle.com> wrote:

> 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>
> <per-ake.minborg at oracle.com>
> *To: *"duncan gittins" <duncan.gittins at gmail.com>
> <duncan.gittins at gmail.com>, "panama-dev" <panama-dev at openjdk.org>
> <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>
> <panama-dev-retn at openjdk.org> on behalf of Duncan Gittins
> <duncan.gittins at gmail.com> <duncan.gittins at gmail.com>
> *Sent:* Sunday, March 30, 2025 9:55 AM
> *To:* panama-dev <panama-dev at openjdk.org> <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/fb18efac/attachment.htm>


More information about the panama-dev mailing list