static field record holding downcall MethodHandle

Remi Forax forax at univ-mlv.fr
Mon Mar 31 06:33:51 UTC 2025


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:

> 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/d5c4b936/attachment.htm>


More information about the panama-dev mailing list