<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div>I would complete saying that it's static + final which is important.</div><div>static only or final only are not enough for the VM to consider the field as a constant.</div><div><br data-mce-bogus="1"></div><div>regards,</div><div>Rémi</div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Per-Ake Minborg" <per-ake.minborg@oracle.com><br><b>To: </b>"duncan gittins" <duncan.gittins@gmail.com>, "panama-dev" <panama-dev@openjdk.org><br><b>Sent: </b>Monday, March 31, 2025 8:24:27 AM<br><b>Subject: </b>Re: static field record holding downcall MethodHandle<br></blockquote></div><div><style style="display:none;"> P {margin-top:0;margin-bottom:0;} </style></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi Duncan,</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
As components in a record are trusted by the VM, this will give similar performance.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
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.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Best, Per</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> panama-dev <panama-dev-retn@openjdk.org> on behalf of Duncan Gittins <duncan.gittins@gmail.com><br>
<b>Sent:</b> Sunday, March 30, 2025 9:55 AM<br>
<b>To:</b> panama-dev <panama-dev@openjdk.org><br>
<b>Subject:</b> static field record holding downcall MethodHandle</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div>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:</div>
<div><br>
</div>
<div>
<div style="background-color:rgb(255,255,255); padding:0px 0px 0px 2px">
<div style="color:rgb(0,0,0); background-color:rgb(255,255,255); font-family:"Consolas"; font-size:10pt; white-space:pre">
<p style="margin:0px"><span style="color:rgb(0,0,0)"></span><span style="color:rgb(127,0,85); font-weight:bold">public</span><span style="color:rgb(0,0,0)">
</span><span style="color:rgb(127,0,85); font-weight:bold">static</span><span style="color:rgb(0,0,0)">
</span><span style="color:rgb(127,0,85); font-weight:bold">final</span><span style="color:rgb(0,0,0)"> FunctionDescriptor
</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">DESC</span><span style="color:rgb(0,0,0)"> = FunctionDescriptor.</span><span style="color:rgb(0,0,0); font-style:italic">ofVoid</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">ADDRESS</span><span style="color:rgb(0,0,0)">,
</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">JAVA_LONG</span><span style="color:rgb(0,0,0)">,
</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">JAVA_LONG</span><span style="color:rgb(0,0,0)">,
</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">ADDRESS</span><span style="color:rgb(0,0,0)">);</span></p>
<p style="margin:0px"><span style="color:rgb(0,0,0)"></span><span style="color:rgb(127,0,85); font-weight:bold">public</span><span style="color:rgb(0,0,0)">
</span><span style="color:rgb(127,0,85); font-weight:bold">static</span><span style="color:rgb(0,0,0)">
</span><span style="color:rgb(127,0,85); font-weight:bold">final</span><span style="color:rgb(0,0,0)"> MemorySegment
</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">ADDR</span><span style="color:rgb(0,0,0)"> =
</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">LOOKUP</span><span style="color:rgb(0,0,0)">.findOrThrow(</span><span style="color:rgb(42,0,255)">"qsort"</span><span style="color:rgb(0,0,0)">);</span></p>
<p style="margin:0px"><span style="color:rgb(0,0,0)"></span><span style="color:rgb(127,0,85); font-weight:bold">public</span><span style="color:rgb(0,0,0)">
</span><span style="color:rgb(127,0,85); font-weight:bold">static</span><span style="color:rgb(0,0,0)">
</span><span style="color:rgb(127,0,85); font-weight:bold">final</span><span style="color:rgb(0,0,0)"> MethodHandle
</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">MH</span><span style="color:rgb(0,0,0)"> =
</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">LINKER</span><span style="color:rgb(0,0,0)">.downcallHandle(</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">ADDR</span><span style="color:rgb(0,0,0)">,
</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">DESC</span><span style="color:rgb(0,0,0)">);</span></p>
<p style="margin:0px"><br>
</p>
</div>
</div>
Would I get similar performance benefit for qsort MH.invokeExact if I changed above to use a record for Download fields instead?
<br>
</div>
<div><br>
</div>
<div>
<div style="background-color:rgb(255,255,255); padding:0px 0px 0px 2px">
<div style="color:rgb(0,0,0); background-color:rgb(255,255,255); font-family:"Consolas"; font-size:10pt; white-space:pre">
<p style="margin:0px"><span style="color:rgb(0,0,0)"></span><span style="color:rgb(127,0,85); font-weight:bold">record</span><span style="color:rgb(0,0,0)"> Downcall(MemorySegment
</span><span style="color:rgb(106,62,62)">address</span><span style="color:rgb(0,0,0)">, FunctionDescriptor
</span><span style="color:rgb(106,62,62)">descriptor</span><span style="color:rgb(0,0,0)">, MethodHandle
</span><span style="color:rgb(106,62,62)">handle</span><span style="color:rgb(0,0,0)">) {</span></p>
<p style="margin:0px"></p>
<p style="margin:0px"><span style="color:rgb(0,0,0)"></span><span style="color:rgb(127,0,85); font-weight:bold">public</span><span style="color:rgb(0,0,0)"> Downcall(MemorySegment
</span><span style="color:rgb(106,62,62)">addr</span><span style="color:rgb(0,0,0)">, FunctionDescriptor
</span><span style="color:rgb(106,62,62)">desc</span><span style="color:rgb(0,0,0)">) {</span></p>
<p style="margin:0px"><span style="color:rgb(0,0,0)"></span><span style="color:rgb(127,0,85); font-weight:bold">this</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(106,62,62)">addr</span><span style="color:rgb(0,0,0)">,
</span><span style="color:rgb(106,62,62)">desc</span><span style="color:rgb(0,0,0)">, Linker.</span><span style="color:rgb(0,0,0); font-style:italic">nativeLinker</span><span style="color:rgb(0,0,0)">().downcallHandle(</span><span style="color:rgb(106,62,62)">addr</span><span style="color:rgb(0,0,0)">,
</span><span style="color:rgb(106,62,62)">desc</span><span style="color:rgb(0,0,0)">));</span></p>
<p style="margin:0px"><span style="color:rgb(0,0,0)">}</span></p>
}</div>
<div style="color:rgb(0,0,0); background-color:rgb(255,255,255); font-family:"Consolas"; font-size:10pt; white-space:pre">
<br>
</div>
</div>
This means the definition is now a one liner:</div>
<div><br>
</div>
<div>
<div style="background-color:rgb(255,255,255); padding:0px 0px 0px 2px">
<div style="color:rgb(0,0,0); background-color:rgb(255,255,255); font-family:"Consolas"; font-size:10pt; white-space:pre">
<div style="background-color:rgb(255,255,255); padding:0px 0px 0px 2px">
<div style="color:rgb(0,0,0); background-color:rgb(255,255,255); font-family:"Consolas"; font-size:10pt; white-space:pre">
<p style="margin:0px"><span style="color:rgb(0,0,0)"></span><span style="color:rgb(127,0,85); font-weight:bold">private</span><span style="color:rgb(0,0,0)">
</span><span style="color:rgb(127,0,85); font-weight:bold">static</span><span style="color:rgb(0,0,0)">
</span><span style="color:rgb(127,0,85); font-weight:bold">final</span><span style="color:rgb(0,0,0)">
</span><span style="text-decoration:underline rgb(0,102,204); color:rgb(0,102,204)">Downcall</span><span style="color:rgb(0,0,0)">
</span><span style="color:rgb(0,0,0)">CALL = </span><span style="color:rgb(127,0,85); font-weight:bold">new</span><span style="color:rgb(0,0,0)"> Downcall(</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">LOOKUP</span><span style="color:rgb(0,0,0)">.findOrThrow(</span><span style="color:rgb(42,0,255)">"qsort"</span><span style="color:rgb(0,0,0)">),
FunctionDescriptor.</span><span style="color:rgb(0,0,0); font-style:italic">ofVoid</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">ADDRESS</span><span style="color:rgb(0,0,0)">,
</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">JAVA_LONG</span><span style="color:rgb(0,0,0)">,
</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">JAVA_LONG</span><span style="color:rgb(0,0,0)">,
</span><span style="color:rgb(0,0,192); font-style:italic; font-weight:bold">ADDRESS</span><span style="color:rgb(0,0,0)">));</span></p>
<p style="margin:0px">// invoke with <span style="color:rgb(0,0,0)">CALL.</span><span style="background-color:rgb(255,255,255); padding:0px 0px 0px 2px"><span style="color:rgb(0,0,0); background-color:rgb(255,255,255); font-family:"Consolas"; font-size:10pt; white-space:pre"><span style="color:rgb(0,0,0)"></span><span style="color:rgb(0,0,192)">handle</span></span></span><span style="background-color:rgb(255,255,255); padding:0px 0px 0px 2px"><span style="color:rgb(0,0,0); background-color:rgb(255,255,255); font-family:"Consolas"; font-size:10pt; white-space:pre"><span style="color:rgb(0,0,192)"></span><span style="color:rgb(0,0,0)">.invokeExact(</span><span style="color:rgb(106,62,62)">base</span><span style="color:rgb(0,0,0)">,
</span><span style="color:rgb(106,62,62)">nitems</span><span style="color:rgb(0,0,0)">,
</span><span style="color:rgb(106,62,62)">size</span><span style="color:rgb(0,0,0)">,
</span><span style="color:rgb(106,62,62)">compar</span><span style="color:rgb(0,0,0)">)</span></span></span>
<br>
</p>
<p style="margin:0px">Kind regards</p>
<p style="margin:0px"><br>
</p>
<p style="margin:0px">Duncan Gittins</p>
<div><br>
</div>
<p style="margin:0px"><br>
</p>
<p style="margin:0px"><br>
</p>
<p style="margin:0px"><br>
</p>
</div>
</div>
<p style="margin:0px"><br>
</p>
</div>
</div>
<br>
</div>
</div>
</div><br></blockquote></div></div></body></html>