Hacker News new | ask | show | jobs
by kjeetgill 2907 days ago
Awesome. I wonder how well this works on a stock JDK10 using graal.

Whenever I see a speed boost to do what is conceptually the same thing I'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.

My hunch is that the line from the tutorial: `@CFunction(transition = Transition.NO_TRANSITION)` makes all the difference. Explanation of NO_TRANSITION from [0]:

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.

Which is probably great for BLAS-like calls. This lines up with my understanding from Cliff Click's great talk "Why is JNI Slow?"[1] basically saying that to be faster you need make assumptions about what the native code could and couldn't do and that generally developers would shoot themselves in the foot.

[0]: https://github.com/oracle/graal/blob/master/sdk/src/org.graa... [1]: https://www.youtube.com/watch?v=LoyBTqkSkZk

3 comments

A team I was on in the past had a well known bottleneck for performance on the most performance critical component. It was one that couldn't possibly be avoided or minimised. It was one called with high frequency, and wall clock wise, didn't take too long.

"JNI is slow", being the conventional wisdom, and knowing just how frequent the calls would be, people had ignored it as an option.

Randomly one of the devs who was most bothered by the bottleneck, had an hour spare and threw the conventional wisdom out the window and dropped in JNI calls to an standard (highly optimised) library and re-benchmarked. 40% performance boost. Further experiments found that "JNI is slow" isn't as true as conventional wisdom quite had it.

ART on Android also has some annotations (@FastNative) for that, but they are forbidden outside system code given that they are quite unsafe.

https://android.googlesource.com/platform/libcore/+/master/d...

EDIT: I forgot to mention @CriticalNative as well

https://android.googlesource.com/platform/libcore/+/master/d...

Yes I think `Transition.NO_TRANSITION` uses the new FFI, GNFI, or Graal Native Function Interface, described here [0].

[0]: https://dl.acm.org/citation.cfm?id=2500832