Skip to content

update benchmark to support Panama API in Java 16 #2

@mcimadamore

Description

@mcimadamore

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions