<div dir="ltr">Another idea related to usability and performance: Can Panama provide a thread-local "native stack" to help users allocate local variables?<div><br></div><div>I have heard from others that LWJGL is using this technology. I also tried to implement it:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><a href="https://github.com/Glavo/java-ffi-benchmark/blob/main/src/main/java/benchmark/experimental/NativeStack.java">https://github.com/Glavo/java-ffi-benchmark/blob/main/src/main/java/benchmark/experimental/NativeStack.java</a></div><div><br></div></blockquote><div><div><div>I ran benchmarks where allocating a small number of local variables was thirty times more efficient than using a confined arena.<br></div><div>If Panama can provide such a class, it will be more convenient and faster for users to assign temporary variables.</div><div><br></div><div>Glavo</div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Mar 25, 2023 at 3:07 AM Glavo <<a href="mailto:zjx001202@gmail.com">zjx001202@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I have run a series of benchmarks of Panama, JNI, JNA, and JNR based on the latest JDK. Here is its GitHub repository:<br><div><br></div><div> <a href="https://github.com/Glavo/java-ffi-benchmark" target="_blank">https://github.com/Glavo/java-ffi-benchmark</a></div><div><br></div><div>Here I tested the performance of no-ops, accessing structs, string conversions, and callbacks, respectively. I also tried the new isTrivial linker option.</div><div>I summarized the results in README and charted them.<br></div><div><br></div><div>In this email, in addition to sharing the above results, I would also like to talk about several issues I have encountered<br></div><div><br></div><div>1. MemorySegment.getUtf8String is unexpectedly slow</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>Panama is much faster than JNA in most cases, but the operation of converting C strings to Java strings is an exception.</div><div>I checked the source code of JNA and Panama, and the suspicious difference is that JNA uses strlen from the C standard library, while Panama uses Java loops. </div><div>Perhaps this method can be optimized.</div></blockquote><div><br></div><div>2. StructLayout must manually specify all padding</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>Can we provide a convenient method for automatically padding between fields based on alignment?</div><div>The current structLayout method is annoying for situations where you need to manually simulate the layout of a C struct.</div></blockquote><div><br></div><div>Glavo</div></div>
</blockquote></div>