29 import org.openjdk.jmh.annotations.Measurement;
30 import org.openjdk.jmh.annotations.Mode;
31 import org.openjdk.jmh.annotations.OutputTimeUnit;
32 import org.openjdk.jmh.annotations.Scope;
33 import org.openjdk.jmh.annotations.Setup;
34 import org.openjdk.jmh.annotations.State;
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, jvmArgsAppend = "--enable-preview")
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(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() {
77 arena.close();
78 }
79
80 @Benchmark
81 public int testLoopPointerInt_ptr() {
82 int sum = 0;
83 for (int i = 0 ; i < ELEM_SIZE ; i++) {
|
29 import org.openjdk.jmh.annotations.Measurement;
30 import org.openjdk.jmh.annotations.Mode;
31 import org.openjdk.jmh.annotations.OutputTimeUnit;
32 import org.openjdk.jmh.annotations.Scope;
33 import org.openjdk.jmh.annotations.Setup;
34 import org.openjdk.jmh.annotations.State;
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(3)
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() {
77 arena.close();
78 }
79
80 @Benchmark
81 public int testLoopPointerInt_ptr() {
82 int sum = 0;
83 for (int i = 0 ; i < ELEM_SIZE ; i++) {
|