1 /*
2 * Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package org.openjdk.bench.java.lang.foreign.pointers;
25
26 import org.openjdk.jmh.annotations.Benchmark;
27 import org.openjdk.jmh.annotations.BenchmarkMode;
28 import org.openjdk.jmh.annotations.Fork;
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, 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() {
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++) {
84 sum += intPointer.get(NativeType.C_INT, i);
85 }
86 return sum;
87 }
88
89 @Benchmark
90 public int testLoopPointerPointerInt_ptr() {
91 int sum = 0;
92 for (int i = 0 ; i < ELEM_SIZE ; i++) {
93 sum += intPointerPointer.get(NativeType.C_INT_PTR, i)
94 .get(NativeType.C_INT, 0);
95 }
96 return sum;
97 }
98
99 @Benchmark
100 public int testLoopPointerPoint_ptr() {
101 int sum = 0;
102 for (int i = 0 ; i < ELEM_SIZE ; i++) {
103 sum += pointPointer.get(Point.TYPE, i).x();
104 }
105 return sum;
106 }
107
108 @Benchmark
109 public int testLoopPointerInt_ptr_generic() {
110 int sum = 0;
111 for (int i = 0 ; i < ELEM_SIZE ; i++) {
112 sum += genericGet(intPointer, NativeType.C_INT, i);
113 }
114 return sum;
115 }
116
117 static <Z> Z genericGet(Pointer<Z> pz, NativeType<Z> type, long offset) {
118 return pz.get(type, offset);
119 }
120
121 @Benchmark
122 public int testLoopPointerInt_segment() {
123 int sum = 0;
124 for (int i = 0 ; i < ELEM_SIZE ; i++) {
125 sum += intSegment.getAtIndex(ValueLayout.JAVA_INT, i);
126 }
127 return sum;
128 }
129
130 @Benchmark
131 public int testLoopPointerPointerInt_segment() {
132 int sum = 0;
133 for (long i = 0 ; i < ELEM_SIZE ; i++) {
134 var segment = intPointerSegment.getAtIndex(UNSAFE_ADDRESS, i);
135 sum += segment.get(ValueLayout.JAVA_INT, 0);
136 }
137 return sum;
138 }
139
140 @Benchmark
141 public int testLoopPointerPoint_segment() {
142 int sum = 0;
143 for (int i = 0 ; i < ELEM_SIZE ; i++) {
144 sum += pointSegment.getAtIndex(ValueLayout.JAVA_INT, i * 2);
145 }
146 return sum;
147 }
148 }