Benchmarks for Java JNI (using JavaCPP) vs Project Panama with JDK (openjdk 19-panama 2022-09-20)
Setup instructions for benchmark:
git clone https://github.com/deepu105/Java-FFI-benchmarks.git
sdk install java 19.ea.1.pma-open
sdk use java 19.ea.1.pma-open
Linux
export C_INCLUDE=/usr/include
jextract --source -d generated/src/main/java -t org.unix -I $C_INCLUDE $C_INCLUDE/unistd.h
macOS
export C_INCLUDE=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
jextract --source -d generated/src/main/java -t org.unix -I $C_INCLUDE $C_INCLUDE/unistd.h
When running the benchmark it by default only works for Linux. For macOS uncomment the appropriate function in src/main/java/org/sample/FFIBenchmark.java
mvn clean verify
java -jar target/benchmarks.jar
These results vary because they were ran on a developer machine with other services running. Also, according
to the JMH docs, to avoid blackholes
(methods that return void). e.g. If you call getPid()
and return void the JVM will optimize by removing dead code. To ensure the code isn't removed the method returns a primitive (int).
Below are some results (smaller numbers are better).
Benchmark Mode Cnt Score Error Units
FFIBenchmark.JNI avgt 40 9.698 ± 0.532 ns/op
FFIBenchmark.panamaDowncall avgt 40 8.431 ± 0.096 ns/op
FFIBenchmark.panamaJExtract avgt 40 8.488 ± 0.099 ns/op
Benchmark Mode Cnt Score Error Units
FFIBenchmark.JNI avgt 40 50.221 ± 0.512 ns/op
FFIBenchmark.panamaDowncall avgt 40 49.382 ± 0.701 ns/op
FFIBenchmark.panamaJExtract avgt 40 49.946 ± 0.721 ns/op