|
| 1 | +package com.evolvedbinary.jnibench.jmhbench; |
| 2 | + |
| 3 | +import java.lang.foreign.Arena; |
| 4 | +import java.lang.foreign.FunctionDescriptor; |
| 5 | +import java.lang.foreign.Linker; |
| 6 | +import java.lang.foreign.MemorySegment; |
| 7 | +import java.lang.foreign.SymbolLookup; |
| 8 | +import java.lang.foreign.ValueLayout; |
| 9 | +import java.lang.invoke.MethodHandle; |
| 10 | +import org.openjdk.jmh.annotations.Benchmark; |
| 11 | +import org.openjdk.jmh.infra.Blackhole; |
| 12 | + |
| 13 | +public class IteratorBenchmarkJavaFfm extends IteratorBenchmark { |
| 14 | + private static final MethodHandle ITERATOR_CREATE_HANDLE; |
| 15 | + private static final MethodHandle ITERATOR_HAS_NEXT_HANDLE; |
| 16 | + private static final MethodHandle ITERATOR_NEXT_HANDLE; |
| 17 | + private static final MethodHandle ITERATOR_DISPOSE_HANDLE; |
| 18 | + |
| 19 | + static { |
| 20 | + try { |
| 21 | + Linker linker = Linker.nativeLinker(); |
| 22 | + SymbolLookup symbolLookup = SymbolLookup.loaderLookup(); |
| 23 | + |
| 24 | + ITERATOR_CREATE_HANDLE = findHandle(symbolLookup, linker, "iterator_create", |
| 25 | + FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_INT, |
| 26 | + ValueLayout.JAVA_LONG)); |
| 27 | + ITERATOR_HAS_NEXT_HANDLE = findHandle(symbolLookup, linker, "iterator_has_next", |
| 28 | + FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS)); |
| 29 | + ITERATOR_NEXT_HANDLE = findHandle(symbolLookup, linker, "iterator_next", |
| 30 | + FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS, |
| 31 | + ValueLayout.ADDRESS, ValueLayout.JAVA_INT)); |
| 32 | + ITERATOR_DISPOSE_HANDLE = findHandle(symbolLookup, linker, "iterator_dispose", |
| 33 | + FunctionDescriptor.ofVoid(ValueLayout.ADDRESS)); |
| 34 | + } catch (Exception e) { |
| 35 | + System.err.println("FFM Setup failed: " + e.getMessage()); |
| 36 | + throw new RuntimeException(e); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private static MethodHandle findHandle(SymbolLookup lookup, Linker linker, String symbol, |
| 41 | + FunctionDescriptor descriptor) { |
| 42 | + var symbolOpt = lookup.find(symbol) |
| 43 | + .or(() -> lookup.find("_" + symbol)); // macOS prefix |
| 44 | + return symbolOpt.map(memorySegment -> linker.downcallHandle(memorySegment, descriptor)) |
| 45 | + .orElse(null); |
| 46 | + } |
| 47 | + |
| 48 | + @Benchmark |
| 49 | + public void iterateFfm(BenchmarkState benchmarkState, Blackhole blackhole) throws Throwable { |
| 50 | + if (ITERATOR_CREATE_HANDLE == null || ITERATOR_HAS_NEXT_HANDLE == null |
| 51 | + || ITERATOR_NEXT_HANDLE == null || ITERATOR_DISPOSE_HANDLE == null) { |
| 52 | + throw new RuntimeException("FFM not available"); |
| 53 | + } |
| 54 | + |
| 55 | + MemorySegment iteratorHandle = null; |
| 56 | + try (Arena arena = Arena.ofConfined()) { |
| 57 | + iteratorHandle = (MemorySegment) ITERATOR_CREATE_HANDLE.invokeExact(benchmarkState.numElements, |
| 58 | + benchmarkState.elementSize); |
| 59 | + MemorySegment buffer = arena.allocate(benchmarkState.elementSize); |
| 60 | + int bufferSize = Math.toIntExact(buffer.byteSize()); |
| 61 | + |
| 62 | + while (((int) ITERATOR_HAS_NEXT_HANDLE.invokeExact(iteratorHandle)) != 0) { |
| 63 | + int bytesRead = (int) ITERATOR_NEXT_HANDLE.invokeExact(iteratorHandle, buffer, bufferSize); |
| 64 | + if (bytesRead < 0) { |
| 65 | + throw new IllegalStateException("Iterator exhausted unexpectedly"); |
| 66 | + } |
| 67 | + byte[] result = new byte[bytesRead]; |
| 68 | + MemorySegment.copy(buffer, ValueLayout.JAVA_BYTE, 0, result, 0, bytesRead); |
| 69 | + blackhole.consume(result); |
| 70 | + } |
| 71 | + } finally { |
| 72 | + if (iteratorHandle != null) { |
| 73 | + ITERATOR_DISPOSE_HANDLE.invokeExact(iteratorHandle); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments