1 /*
2 * Copyright (c) 2025, 2026, 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 package org.openjdk.bench.valhalla.acmp.field;
24
25 import jdk.internal.vm.annotation.LooselyConsistentValue;
26 import jdk.internal.vm.annotation.NullRestricted;
27 import org.openjdk.jmh.annotations.Benchmark;
28 import org.openjdk.jmh.annotations.BenchmarkMode;
29 import org.openjdk.jmh.annotations.CompilerControl;
30 import org.openjdk.jmh.annotations.Fork;
31 import org.openjdk.jmh.annotations.Measurement;
32 import org.openjdk.jmh.annotations.Mode;
33 import org.openjdk.jmh.annotations.OperationsPerInvocation;
34 import org.openjdk.jmh.annotations.OutputTimeUnit;
35 import org.openjdk.jmh.annotations.Scope;
36 import org.openjdk.jmh.annotations.Setup;
37 import org.openjdk.jmh.annotations.State;
38 import org.openjdk.jmh.annotations.Warmup;
39
40 import java.util.BitSet;
41 import java.util.Random;
42 import java.util.concurrent.TimeUnit;
43
44 /*
45 * For proper results it should be executed:
46 * java -jar target/benchmarks.jar org.openjdk.bench.valhalla.acmp.field.Value -wmb "org.openjdk.bench.valhalla.acmp.field.Value.*050"
47 */
48
49 @Fork(value = 3, jvmArgsAppend = {"--enable-preview"})
50 @Warmup(iterations = 3, time = 1)
51 @Measurement(iterations = 5, time = 1)
52 @OutputTimeUnit(TimeUnit.NANOSECONDS)
53 @BenchmarkMode(Mode.AverageTime)
54 @State(Scope.Thread)
55 public class Value128NullFreeNonAtomic {
56
57 public static final int SIZE = 100;
58
59 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
60 private static int cmp_branch_val(ValWrapper[] objects1, ValWrapper[] objects2) {
61 int s = 0;
62 for (int i = 0; i < SIZE; i++) {
63 if (objects1[i].f == objects2[i].f) {
64 s += 1;
65 } else {
66 s -= 1;
67 }
68 }
69 return s;
70 }
71
72 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
73 private static boolean cmp_result_val(ValWrapper[] objects1, ValWrapper[] objects2) {
74 boolean s = false;
75 for (int i = 0; i < SIZE; i++) {
76 s ^= objects1[i].f == objects2[i].f;
77 }
78 return s;
79 }
80
81 @Benchmark
82 @OperationsPerInvocation(SIZE)
83 @CompilerControl(CompilerControl.Mode.INLINE)
84 public int branch_val_equals000(ValState00 st) {
85 return cmp_branch_val(st.arr1, st.arr2);
86 }
87
88 @Benchmark
89 @OperationsPerInvocation(SIZE)
90 @CompilerControl(CompilerControl.Mode.INLINE)
91 public int branch_val_equals025(ValState25 st) {
92 return cmp_branch_val(st.arr1, st.arr2);
93 }
94
95 @Benchmark
96 @OperationsPerInvocation(SIZE)
97 @CompilerControl(CompilerControl.Mode.INLINE)
98 public int branch_val_equals050(ValState50 st) {
99 return cmp_branch_val(st.arr1, st.arr2);
100 }
101
102 @Benchmark
103 @OperationsPerInvocation(SIZE)
104 @CompilerControl(CompilerControl.Mode.INLINE)
105 public int branch_val_equals075(ValState75 st) {
106 return cmp_branch_val(st.arr1, st.arr2);
107 }
108
109 @Benchmark
110 @OperationsPerInvocation(SIZE)
111 @CompilerControl(CompilerControl.Mode.INLINE)
112 public int branch_val_equals100(ValState100 st) {
113 return cmp_branch_val(st.arr1, st.arr2);
114 }
115
116 @Benchmark
117 @OperationsPerInvocation(SIZE)
118 @CompilerControl(CompilerControl.Mode.INLINE)
119 public boolean result_val_equals000(ValState00 st) {
120 return cmp_result_val(st.arr1, st.arr2);
121 }
122
123 @Benchmark
124 @OperationsPerInvocation(SIZE)
125 @CompilerControl(CompilerControl.Mode.INLINE)
126 public boolean result_val_equals025(ValState25 st) {
127 return cmp_result_val(st.arr1, st.arr2);
128 }
129
130 @Benchmark
131 @OperationsPerInvocation(SIZE)
132 @CompilerControl(CompilerControl.Mode.INLINE)
133 public boolean result_val_equals050(ValState50 st) {
134 return cmp_result_val(st.arr1, st.arr2);
135 }
136
137 @Benchmark
138 @OperationsPerInvocation(SIZE)
139 @CompilerControl(CompilerControl.Mode.INLINE)
140 public boolean result_val_equals075(ValState75 st) {
141 return cmp_result_val(st.arr1, st.arr2);
142 }
143
144 @Benchmark
145 @OperationsPerInvocation(SIZE)
146 @CompilerControl(CompilerControl.Mode.INLINE)
147 public boolean result_val_equals100(ValState100 st) {
148 return cmp_result_val(st.arr1, st.arr2);
149 }
150
151 @LooselyConsistentValue
152 public static value class ValueInt4 {
153
154 public final int v0;
155 public final int v1;
156 public final int v2;
157 public final int v3;
158
159 public ValueInt4(int v) {
160 this.v0 = v;
161 this.v1 = v + 1;
162 this.v2 = v + 2;
163 this.v3 = v + 3;
164 }
165
166 }
167
168 private static void populate(ValWrapper[] arr1, ValWrapper[] arr2, int eq) {
169 if (eq <= 0) {
170 for (int i = 0; i < SIZE; i++) {
171 arr1[i] = new ValWrapper(new ValueInt4(2 * i));
172 arr2[i] = new ValWrapper(new ValueInt4(2 * i + 1));
173 }
174 } else if (eq >= 100) {
175 for (int i = 0; i < SIZE; i++) {
176 ValueInt4 x = new ValueInt4(i);
177 arr2[i] = new ValWrapper(x);
178 arr1[i] = new ValWrapper(x);
179 }
180 } else {
181 BitSet eqset = new Random(42).ints(0, SIZE).distinct().limit(eq * SIZE / 100).collect(BitSet::new, BitSet::set, BitSet::or);
182 for (int i = 0; i < SIZE; i++) {
183 if (eqset.get(i)) {
184 ValueInt4 x = new ValueInt4(i);
185 arr2[i] = new ValWrapper(x);
186 arr1[i] = new ValWrapper(x);
187 } else {
188 arr1[i] = new ValWrapper(new ValueInt4(2 * i));
189 arr2[i] = new ValWrapper(new ValueInt4(2 * i + 1));
190 }
191 }
192 }
193 }
194
195 public static class ValWrapper {
196
197 @NullRestricted
198 public final ValueInt4 f;
199
200 public ValWrapper(ValueInt4 f) {
201 this.f = f;
202 super();
203 }
204 }
205
206 @State(Scope.Thread)
207 public abstract static class ValState {
208 ValWrapper[] arr1, arr2;
209
210 public void setup(int eq) {
211 arr1 = new ValWrapper[SIZE];
212 arr2 = new ValWrapper[SIZE];
213 populate(arr1, arr2, eq);
214 }
215 }
216
217 public static class ValState00 extends ValState {
218 @Setup
219 public void setup() {
220 setup(0);
221 }
222 }
223
224 public static class ValState25 extends ValState {
225 @Setup
226 public void setup() {
227 setup(25);
228 }
229 }
230
231 public static class ValState50 extends ValState {
232 @Setup
233 public void setup() {
234 setup(50);
235 }
236 }
237
238 public static class ValState75 extends ValState {
239 @Setup
240 public void setup() {
241 setup(75);
242 }
243 }
244
245 public static class ValState100 extends ValState {
246 @Setup
247 public void setup() {
248 setup(100);
249 }
250 }
251
252 }