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