1 /*
2 * Copyright (c) 2025, 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.sum;
24
25 import jdk.internal.value.ValueClass;
26 import jdk.internal.vm.annotation.LooselyConsistentValue;
27 import jdk.internal.vm.annotation.NullRestricted;
28 import jdk.internal.vm.annotation.Strict;
29 import org.openjdk.jmh.annotations.Benchmark;
30 import org.openjdk.jmh.annotations.CompilerControl;
31 import org.openjdk.jmh.annotations.Fork;
32 import org.openjdk.jmh.annotations.Setup;
33
34 @Fork(value = 3, jvmArgsAppend = {"--enable-preview", "--add-exports", "java.base/jdk.internal.value=ALL-UNNAMED"})
35 public class ValueNullFreeNonAtomic extends SumBase {
36
37 public interface InterfaceSum {
38 public int sum();
39 }
40
41 @LooselyConsistentValue
42 public static value class ValueInt implements InterfaceSum {
43 public final int v0;
44 public ValueInt(int value) {
45 this.v0 = value;
46 }
47 public int sum() {
48 return v0;
49 }
50 }
51
52 @LooselyConsistentValue
53 public static value class ValueInt2_w2_d0 implements InterfaceSum {
54 public final int v0, v1;
55
56 public ValueInt2_w2_d0(int v0, int v1) {
57 this.v0 = v0;
58 this.v1 = v1;
59 }
60
61 public int sum() {
62 return v0 + v1;
63 }
64 }
65
66 @LooselyConsistentValue
67 public static value class ValueInt2_w2_d1 implements InterfaceSum {
68 @Strict
69 @NullRestricted
70 public final ValueInt v0, v1;
71
72 public ValueInt2_w2_d1(ValueInt v0, ValueInt v1) {
73 this.v0 = v0;
74 this.v1 = v1;
75 }
76
77 public ValueInt2_w2_d1(int v0, int v1) {
78 this(new ValueInt(v0), new ValueInt(v1));
79 }
80
81 public int sum() {
82 return v0.sum() + v1.sum();
83 }
84 }
85
86 @LooselyConsistentValue
87 public static value class ValueInt4_w4_d0 implements InterfaceSum {
88 public final int v0, v1, v2, v3;
89
90 public ValueInt4_w4_d0(int v0, int v1, int v2, int v3) {
91 this.v0 = v0;
92 this.v1 = v1;
93 this.v2 = v2;
94 this.v3 = v3;
95 }
96
97 public int sum() {
98 return v0 + v1 + v2 + v3;
99 }
100 }
101
102 @LooselyConsistentValue
103 public static value class ValueInt4_w4_d1 implements InterfaceSum {
104 @Strict
105 @NullRestricted
106 public final ValueInt v0, v1, v2, v3;
107
108 public ValueInt4_w4_d1(ValueInt v0, ValueInt v1, ValueInt v2, ValueInt v3) {
109 this.v0 = v0;
110 this.v1 = v1;
111 this.v2 = v2;
112 this.v3 = v3;
113 }
114
115 public ValueInt4_w4_d1(int v0, int v1, int v2, int v3) {
116 this(new ValueInt(v0), new ValueInt(v1), new ValueInt(v2), new ValueInt(v3));
117 }
118
119 public int sum() {
120 return v0.sum() + v1.sum() + v2.sum() + v3.sum();
121 }
122 }
123
124 @LooselyConsistentValue
125 public static value class ValueInt4_w2_d1 implements InterfaceSum {
126 @Strict
127 @NullRestricted
128 public final ValueInt2_w2_d0 v0, v1;
129
130 public ValueInt4_w2_d1(ValueInt2_w2_d0 v0, ValueInt2_w2_d0 v1) {
131 this.v0 = v0;
132 this.v1 = v1;
133 }
134
135 public ValueInt4_w2_d1(int v0, int v1, int v2, int v3) {
136 this(new ValueInt2_w2_d0(v0, v1), new ValueInt2_w2_d0(v2, v3));
137 }
138
139 public int sum() {
140 return v0.sum() + v1.sum();
141 }
142 }
143
144 @LooselyConsistentValue
145 public static value class ValueInt4_w2_d2 implements InterfaceSum {
146 @Strict
147 @NullRestricted
148 public final ValueInt2_w2_d1 v0, v1;
149
150 public ValueInt4_w2_d2(ValueInt2_w2_d1 v0, ValueInt2_w2_d1 v1) {
151 this.v0 = v0;
152 this.v1 = v1;
153 }
154
155 public ValueInt4_w2_d2(int v0, int v1, int v2, int v3) {
156 this(new ValueInt2_w2_d1(v0, v1), new ValueInt2_w2_d1(v2, v3));
157 }
158 public int sum() {
159 return v0.sum() + v1.sum();
160 }
161
162 }
163
164 public static class ValState_of_Int extends SizeState {
165 public ValueInt[] arr;
166 @Setup
167 public void setup() {
168 arr = (ValueInt[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt.class, size * 4, new ValueInt(0));
169 for (int i = 0; i < arr.length; i++) {
170 arr[i] = new ValueInt(i);
171 }
172 }
173 }
174
175 public static class IntState_of_Int extends SizeState {
176 public InterfaceSum[] arr;
177 @Setup
178 public void setup() {
179 arr = new InterfaceSum[size * 4];
180 for (int i = 0; i < arr.length; i++) {
181 arr[i] = new ValueInt(i);
182 }
183 }
184 }
185
186 public static class ValState_of_Int2_w2_d0 extends SizeState {
187 public ValueInt2_w2_d0[] arr;
188 @Setup
189 public void setup() {
190 arr = (ValueInt2_w2_d0[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt2_w2_d0.class, size * 2, new ValueInt2_w2_d0(0, 0));
191 for (int i = 0; i < arr.length; i++) {
192 arr[i] = new ValueInt2_w2_d0(2 * i, 2 * i + 1);
193 }
194 }
195 }
196
197 public static class IntState_of_Int2_w2_d0 extends SizeState {
198 public InterfaceSum[] arr;
199 @Setup
200 public void setup() {
201 arr = new InterfaceSum[size * 2];
202 for (int i = 0; i < arr.length; i++) {
203 arr[i] = new ValueInt2_w2_d0(2 * i, 2 * i + 1);
204 }
205 }
206 }
207
208 public static class ValState_of_Int2_w2_d1 extends SizeState {
209 public ValueInt2_w2_d1[] arr;
210 @Setup
211 public void setup() {
212 arr = (ValueInt2_w2_d1[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt2_w2_d1.class, size * 4, new ValueInt2_w2_d1(0, 0));
213 for (int i = 0; i < arr.length; i++) {
214 arr[i] = new ValueInt2_w2_d1(2 * i, 2 * i + 1);
215 }
216 }
217 }
218
219 public static class IntState_of_Int2_w2_d1 extends SizeState {
220 public InterfaceSum[] arr;
221 @Setup
222 public void setup() {
223 arr = new InterfaceSum[size * 2];
224 for (int i = 0; i < arr.length; i++) {
225 arr[i] = new ValueInt2_w2_d1(2 * i, 2 * i + 1);
226 }
227 }
228 }
229
230 public static class ValState_of_Int4_w4_d0 extends SizeState {
231 public ValueInt4_w4_d0[] arr;
232 @Setup
233 public void setup() {
234 arr = (ValueInt4_w4_d0[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt4_w4_d0.class, size, new ValueInt4_w4_d0(0, 0, 0, 0));
235 for (int i = 0; i < arr.length; i++) {
236 arr[i] = new ValueInt4_w4_d0(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
237 }
238 }
239 }
240
241 public static class IntState_of_Int4_w4_d0 extends SizeState {
242 public InterfaceSum[] arr;
243 @Setup
244 public void setup() {
245 arr = new InterfaceSum[size];
246 for (int i = 0; i < arr.length; i++) {
247 arr[i] = new ValueInt4_w4_d0(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
248 }
249 }
250 }
251
252 public static class ValState_of_Int4_w4_d1 extends SizeState {
253 public ValueInt4_w4_d1[] arr;
254 @Setup
255 public void setup() {
256 arr = (ValueInt4_w4_d1[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt4_w4_d1.class, size, new ValueInt4_w4_d1(0, 0, 0, 0));
257 for (int i = 0; i < arr.length; i++) {
258 arr[i] = new ValueInt4_w4_d1(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
259 }
260 }
261 }
262
263 public static class IntState_of_Int4_w4_d1 extends SizeState {
264 public InterfaceSum[] arr;
265 @Setup
266 public void setup() {
267 arr = new InterfaceSum[size];
268 for (int i = 0; i < arr.length; i++) {
269 arr[i] = new ValueInt4_w4_d1(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
270 }
271 }
272 }
273
274 public static class ValState_of_Int4_w2_d1 extends SizeState {
275 public ValueInt4_w2_d1[] arr;
276 @Setup
277 public void setup() {
278 arr = (ValueInt4_w2_d1[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt4_w2_d1.class, size, new ValueInt4_w2_d1(0, 0, 0, 0));
279 for (int i = 0; i < arr.length; i++) {
280 arr[i] = new ValueInt4_w2_d1(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
281 }
282 }
283 }
284
285 public static class IntState_of_Int4_w2_d1 extends SizeState {
286 public InterfaceSum[] arr;
287 @Setup
288 public void setup() {
289 arr = new InterfaceSum[size];
290 for (int i = 0; i < arr.length; i++) {
291 arr[i] = new ValueInt4_w2_d1(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
292 }
293 }
294 }
295
296 public static class ValState_of_Int4_w2_d2 extends SizeState {
297 public ValueInt4_w2_d2[] arr;
298 @Setup
299 public void setup() {
300 arr = (ValueInt4_w2_d2[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt4_w2_d2.class, size, new ValueInt4_w2_d2(0, 0, 0, 0));
301 for (int i = 0; i < arr.length; i++) {
302 arr[i] = new ValueInt4_w2_d2(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
303 }
304 }
305 }
306
307 public static class IntState_of_Int4_w2_d2 extends SizeState {
308 public InterfaceSum[] arr;
309 @Setup
310 public void setup() {
311 arr = new InterfaceSum[size];
312 for (int i = 0; i < arr.length; i++) {
313 arr[i] = new ValueInt4_w2_d2(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
314 }
315 }
316 }
317
318 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
319 public int sum_interface(InterfaceSum[] src) {
320 int s = 0;
321 for (var v : src) {
322 s += v.sum();
323 }
324 return s;
325 }
326
327 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
328 public int sum_val_of_Int(ValueInt[] src) {
329 int s = 0;
330 for (var v : src) {
331 s += v.sum();
332 }
333 return s;
334 }
335
336 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
337 public int sum_val_of_Int2_w2_d0(ValueInt2_w2_d0[] src) {
338 int s = 0;
339 for (var v : src) {
340 s += v.sum();
341 }
342 return s;
343 }
344
345 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
346 public int sum_val_of_Int2_w2_d1(ValueInt2_w2_d1[] src) {
347 int s = 0;
348 for (var v : src) {
349 s += v.sum();
350 }
351 return s;
352 }
353
354 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
355 public int sum_val_of_Int4_w4_d0(ValueInt4_w4_d0[] src) {
356 int s = 0;
357 for (var v : src) {
358 s += v.sum();
359 }
360 return s;
361 }
362
363 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
364 public int sum_val_of_Int4_w4_d1(ValueInt4_w4_d1[] src) {
365 int s = 0;
366 for (var v : src) {
367 s += v.sum();
368 }
369 return s;
370 }
371
372 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
373 public int sum_val_of_Int4_w2_d1(ValueInt4_w2_d1[] src) {
374 int s = 0;
375 for (var v : src) {
376 s += v.sum();
377 }
378 return s;
379 }
380
381 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
382 public int sum_val_of_Int4_w2_d2(ValueInt4_w2_d2[] src) {
383 int s = 0;
384 for (var v : src) {
385 s += v.sum();
386 }
387 return s;
388 }
389
390 @Benchmark
391 public int sum_interface_of_Int(IntState_of_Int st) {
392 return sum_interface(st.arr);
393 }
394
395 @Benchmark
396 public int sum_interface_of_Int2_w2_d0(IntState_of_Int2_w2_d0 st) {
397 return sum_interface(st.arr);
398 }
399
400 @Benchmark
401 public int sum_interface_of_Int2_w2_d1(IntState_of_Int2_w2_d1 st) {
402 return sum_interface(st.arr);
403 }
404
405 @Benchmark
406 public int sum_interface_of_Int4_w4_d0(IntState_of_Int4_w4_d0 st) {
407 return sum_interface(st.arr);
408 }
409
410 @Benchmark
411 public int sum_interface_of_Int4_w4_d1(IntState_of_Int4_w4_d1 st) {
412 return sum_interface(st.arr);
413 }
414
415 @Benchmark
416 public int sum_interface_of_Int4_w2_d1(IntState_of_Int4_w2_d1 st) {
417 return sum_interface(st.arr);
418 }
419
420 @Benchmark
421 public int sum_interface_of_Int4_w2_d2(IntState_of_Int4_w2_d2 st) {
422 return sum_interface(st.arr);
423 }
424
425 @Benchmark
426 public int sum_val_of_Int(ValState_of_Int st) {
427 return sum_val_of_Int(st.arr);
428 }
429
430 @Benchmark
431 public int sum_val_of_Int2_w2_d0(ValState_of_Int2_w2_d0 st) {
432 return sum_val_of_Int2_w2_d0(st.arr);
433 }
434
435 @Benchmark
436 public int sum_val_of_Int2_w2_d1(ValState_of_Int2_w2_d1 st) {
437 return sum_val_of_Int2_w2_d1(st.arr);
438 }
439
440 @Benchmark
441 public int sum_val_of_Int4_w4_d0(ValState_of_Int4_w4_d0 st) {
442 return sum_val_of_Int4_w4_d0(st.arr);
443 }
444
445 @Benchmark
446 public int sum_val_of_Int4_w4_d1(ValState_of_Int4_w4_d1 st) {
447 return sum_val_of_Int4_w4_d1(st.arr);
448 }
449
450 @Benchmark
451 public int sum_val_of_Int4_w2_d1(ValState_of_Int4_w2_d1 st) {
452 return sum_val_of_Int4_w2_d1(st.arr);
453 }
454
455 @Benchmark
456 public int sum_val_of_Int4_w2_d2(ValState_of_Int4_w2_d2 st) {
457 return sum_val_of_Int4_w2_d2(st.arr);
458 }
459
460 }