TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Interfacing with native methods on Graal VM

99 pointsby wippleralmost 7 years ago

3 comments

kjeetgillalmost 7 years ago
Awesome. I wonder how well this works on a stock JDK10 using graal.<p>Whenever I see a speed boost to do what is conceptually the same thing I&#x27;m always curious where the fat was cut. What did we give up? You can dump the resulting assembly with -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly and diff might be revealing.<p>My hunch is that the line from the tutorial: `@CFunction(transition = Transition.NO_TRANSITION)` makes all the difference. Explanation of NO_TRANSITION from [0]:<p>No prologue and epilogue is emitted. The C code must not block and must not call back to Java. Also, long running C code delays safepoints (and therefore garbage collection) of other threads until the call returns.<p>Which is probably great for BLAS-like calls. This lines up with my understanding from Cliff Click&#x27;s great talk &quot;Why is JNI Slow?&quot;[1] basically saying that to be faster you need make assumptions about what the native code could and couldn&#x27;t do and that generally developers would shoot themselves in the foot.<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;oracle&#x2F;graal&#x2F;blob&#x2F;master&#x2F;sdk&#x2F;src&#x2F;org.graalvm.nativeimage&#x2F;src&#x2F;org&#x2F;graalvm&#x2F;nativeimage&#x2F;c&#x2F;function&#x2F;CFunction.java" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;oracle&#x2F;graal&#x2F;blob&#x2F;master&#x2F;sdk&#x2F;src&#x2F;org.graa...</a> [1]: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=LoyBTqkSkZk" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=LoyBTqkSkZk</a>
评论 #17478738 未加载
评论 #17478358 未加载
评论 #17478125 未加载
Reason077almost 7 years ago
Back in the day, GCC&#x27;s Java native compiler &quot;GCJ&quot;, had an alternative native method interface called CNI.<p>GCC recognized <i>#extern &quot;Java&quot;</i> in headers generated from class files. You could then call (gcj-compiled) Java classes from C++ as if they were native C++ classes, as well as implement Java &quot;native&quot; methods in natural C++.<p>The whole thing performed a lot better than JNI since it was, more or less, just using the standard platform calling conventions. Calling a native CNI method from Java had the same overhead as any regular Java virtual method call.<p>Ultimately, GCJ faded away because there wasn&#x27;t a great deal of interest in native Java compilation back then, and too many compatibility challenges in the pre-OpenJDK days. But it&#x27;s interesting to see many of it&#x27;s ideas coming back now in the form of Graal&#x2F;GraalVM.
评论 #17478080 未加载
repolfxalmost 7 years ago
There&#x27;s an effort to bring a more modern FFI to Java that works similar to the one described in the article, called project Panama. It has tools to convert C header files into the equivalent annotated Java definitions and is intended to help improve performance as well.<p>You can follow along here:<p><a href="http:&#x2F;&#x2F;mail.openjdk.java.net&#x2F;pipermail&#x2F;panama-dev&#x2F;" rel="nofollow">http:&#x2F;&#x2F;mail.openjdk.java.net&#x2F;pipermail&#x2F;panama-dev&#x2F;</a><p>The same project is also adding support for writing vector code in Java (SSE, AVX etc).
评论 #17478353 未加载
评论 #17478605 未加载