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