< prev index next >

test/micro/org/openjdk/bench/java/lang/foreign/pointers/PointerBench.java

Print this page

 35 import org.openjdk.jmh.annotations.TearDown;
 36 import org.openjdk.jmh.annotations.Warmup;
 37 
 38 import java.lang.foreign.AddressLayout;
 39 import java.lang.foreign.Arena;
 40 import java.lang.foreign.MemoryLayout;
 41 import java.lang.foreign.MemorySegment;
 42 import java.lang.foreign.ValueLayout;
 43 import java.util.concurrent.TimeUnit;
 44 
 45 @BenchmarkMode(Mode.AverageTime)
 46 @Warmup(iterations = 3, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 47 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 48 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 49 @Fork(value = 3, jvmArgs = { "--enable-native-access=ALL-UNNAMED" })
 50 @State(Scope.Benchmark)
 51 public class PointerBench {
 52 
 53     final Arena arena = Arena.ofConfined();
 54     static final int ELEM_SIZE = 1_000_000;

 55     Pointer<Integer> intPointer = Pointer.allocate(NativeType.C_INT, ELEM_SIZE, arena);

 56     Pointer<Pointer<Integer>> intPointerPointer = Pointer.allocate(NativeType.C_INT_PTR, ELEM_SIZE, arena);

 57     Pointer<Point> pointPointer = Pointer.allocate(Point.TYPE, ELEM_SIZE, arena);
 58     MemorySegment intSegment = intPointer.segment();
 59     MemorySegment intPointerSegment = intPointerPointer.segment();
 60     MemorySegment pointSegment = pointPointer.segment();
 61 
 62     public static final AddressLayout UNSAFE_ADDRESS = ValueLayout.ADDRESS
 63             .withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, ValueLayout.JAVA_BYTE));
 64 
 65     @Setup
 66     public void setup() {
 67         for (int i = 0 ; i < ELEM_SIZE ; i++) {
 68             intSegment.setAtIndex(ValueLayout.JAVA_INT, i, i);
 69             intPointerSegment.setAtIndex(ValueLayout.ADDRESS, i, intSegment.asSlice(4 * i));
 70             pointSegment.setAtIndex(ValueLayout.JAVA_INT, (i * 2), i);
 71             pointSegment.setAtIndex(ValueLayout.JAVA_INT, (i * 2) + 1, i);
 72         }
 73     }
 74 
 75     @TearDown
 76     public void teardown() {

 35 import org.openjdk.jmh.annotations.TearDown;
 36 import org.openjdk.jmh.annotations.Warmup;
 37 
 38 import java.lang.foreign.AddressLayout;
 39 import java.lang.foreign.Arena;
 40 import java.lang.foreign.MemoryLayout;
 41 import java.lang.foreign.MemorySegment;
 42 import java.lang.foreign.ValueLayout;
 43 import java.util.concurrent.TimeUnit;
 44 
 45 @BenchmarkMode(Mode.AverageTime)
 46 @Warmup(iterations = 3, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 47 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 48 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 49 @Fork(value = 3, jvmArgs = { "--enable-native-access=ALL-UNNAMED" })
 50 @State(Scope.Benchmark)
 51 public class PointerBench {
 52 
 53     final Arena arena = Arena.ofConfined();
 54     static final int ELEM_SIZE = 1_000_000;
 55     @SuppressWarnings("initialization")
 56     Pointer<Integer> intPointer = Pointer.allocate(NativeType.C_INT, ELEM_SIZE, arena);
 57     @SuppressWarnings("initialization")
 58     Pointer<Pointer<Integer>> intPointerPointer = Pointer.allocate(NativeType.C_INT_PTR, ELEM_SIZE, arena);
 59     @SuppressWarnings("initialization")
 60     Pointer<Point> pointPointer = Pointer.allocate(Point.TYPE, ELEM_SIZE, arena);
 61     MemorySegment intSegment = intPointer.segment();
 62     MemorySegment intPointerSegment = intPointerPointer.segment();
 63     MemorySegment pointSegment = pointPointer.segment();
 64 
 65     public static final AddressLayout UNSAFE_ADDRESS = ValueLayout.ADDRESS
 66             .withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, ValueLayout.JAVA_BYTE));
 67 
 68     @Setup
 69     public void setup() {
 70         for (int i = 0 ; i < ELEM_SIZE ; i++) {
 71             intSegment.setAtIndex(ValueLayout.JAVA_INT, i, i);
 72             intPointerSegment.setAtIndex(ValueLayout.ADDRESS, i, intSegment.asSlice(4 * i));
 73             pointSegment.setAtIndex(ValueLayout.JAVA_INT, (i * 2), i);
 74             pointSegment.setAtIndex(ValueLayout.JAVA_INT, (i * 2) + 1, i);
 75         }
 76     }
 77 
 78     @TearDown
 79     public void teardown() {
< prev index next >