zakgof / java-native-benchmark Goto Github PK
View Code? Open in Web Editor NEWBenchmarking Java's native call APIs: JNI, JNA, JNR, BridJ and Project Panama
Benchmarking Java's native call APIs: JNI, JNA, JNR, BridJ and Project Panama
Did you also benchmark JNA in direct mapping mode? It is said to approach the speed of JNI. See here: https://github.com/java-native-access/jna/blob/master/www/DirectMapping.md
Hey there. Would it be possible to rerun this against the latest open jdk 19 build to see if there is any performance changes between JNI and Panama?
Many thanks!
Hi,
I've done some work to rewrite your benchmark to support the Panama APIs we have in Java 16 (Foreign Memory Access API [1] and Foreign Linker API [2]) - the results I've got are:
Benchmark Mode Cnt Score Error Units
JmhCallOnly.bridj avgt 50 279.563 ▒ 4.312 ns/op
JmhCallOnly.jna avgt 50 826.049 ▒ 11.840 ns/op
JmhCallOnly.jna_direct avgt 50 934.675 ▒ 13.662 ns/op
JmhCallOnly.jni_javacpp avgt 50 36.899 ▒ 0.618 ns/op
JmhCallOnly.jnr avgt 50 233.757 ▒ 3.090 ns/op
JmhCallOnly.panama avgt 50 30.332 ▒ 0.386 ns/op
JmhGetSystemTimeSeconds.bridj avgt 50 1306.806 ▒ 131.925 ns/op
JmhGetSystemTimeSeconds.java_calendar avgt 50 136.437 ▒ 3.949 ns/op
JmhGetSystemTimeSeconds.java_date avgt 50 50.450 ▒ 0.750 ns/op
JmhGetSystemTimeSeconds.java_localdatetime avgt 50 58.232 ▒ 1.456 ns/op
JmhGetSystemTimeSeconds.jna avgt 50 3194.622 ▒ 216.633 ns/op
JmhGetSystemTimeSeconds.jnaDirect avgt 50 3227.971 ▒ 193.953 ns/op
JmhGetSystemTimeSeconds.jni_javacpp avgt 50 418.683 ▒ 5.789 ns/op
JmhGetSystemTimeSeconds.jnr avgt 50 327.486 ▒ 7.548 ns/op
JmhGetSystemTimeSeconds.panama avgt 50 390.615 ▒ 3.987 ns/op
Would it be possible to update the benchmark?
Here's the source code I've used for the Panama benchmark:
package com.zakgof.jnbenchmark.panama;
import java.lang.invoke.*;
import jdk.incubator.foreign.*;
import jdk.incubator.foreign.MemoryLayout.*;
import static jdk.incubator.foreign.CLinker.*;
public class PanamaBenchmark {
static final MemoryLayout SYSTEMTIME = MemoryLayout.ofStruct(
C_SHORT.withName("wYear"),
C_SHORT.withName("wMonth"),
C_SHORT.withName("wDayOfWeek"),
C_SHORT.withName("wDay"),
C_SHORT.withName("wHour"),
C_SHORT.withName("wMinute"),
C_SHORT.withName("wSecond"),
C_SHORT.withName("wMilliseconds")
);
// var handle
final static VarHandle wSecond = SYSTEMTIME.varHandle(short.class, PathElement.groupElement("wSecond"));
// method handle
final static MethodHandle GetSystemTime = CLinker.getInstance().downcallHandle(
LibraryLookup.ofDefault().lookup("GetSystemTime").get(),
MethodType.methodType(void.class, MemoryAddress.class),
FunctionDescriptor.ofVoid(C_POINTER)
);
final MemorySegment systemtime_shared;
public PanamaBenchmark() {
this.systemtime_shared = MemorySegment.allocateNative(SYSTEMTIME);
}
public short all() {
try (MemorySegment systemtime = MemorySegment.allocateNative(SYSTEMTIME)) {
GetSystemTime.invokeExact(systemtime.address());
return (short) wSecond.get(systemtime);
} catch (Throwable ex) {
throw new AssertionError(ex);
}
}
public void callOnly() {
try {
GetSystemTime.invokeExact(systemtime_shared.address());
} catch (Throwable ex) {
throw new AssertionError(ex);
}
}
}
I hope this helps.
[1] - https://openjdk.java.net/jeps/393
[2] - https://openjdk.java.net/jeps/389
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.