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 org.openjdk.jmh.annotations.Benchmark; 26 import org.openjdk.jmh.annotations.CompilerControl; 27 import org.openjdk.jmh.annotations.Fork; 28 import org.openjdk.jmh.annotations.Setup; 29 30 @Fork(value = 3, jvmArgsAppend = {"--enable-preview"}) 31 public class Value extends SumBase { 32 33 public interface InterfaceSum { 34 public int sum(); 35 } 36 37 public static value class ValueInt implements InterfaceSum { 38 public final int v0; 39 public ValueInt(int value) { 40 this.v0 = value; 41 } 42 public int sum() { 43 return v0; 44 } 45 } 46 47 public static value class ValueInt2_by_int implements InterfaceSum { 48 public final int v0, v1; 49 50 public ValueInt2_by_int(int v0, int v1) { 51 this.v0 = v0; 52 this.v1 = v1; 53 } 54 55 public int sum() { 56 return v0 + v1; 57 } 58 } 59 60 public static value class ValueInt2_by_Int implements InterfaceSum { 61 public final ValueInt v0, v1; 62 63 public ValueInt2_by_Int(ValueInt v0, ValueInt v1) { 64 this.v0 = v0; 65 this.v1 = v1; 66 } 67 68 public ValueInt2_by_Int(int v0, int v1) { 69 this(new ValueInt(v0), new ValueInt(v1)); 70 } 71 72 public int sum() { 73 return v0.sum() + v1.sum(); 74 } 75 } 76 77 public static value class ValueInt4_by_int implements InterfaceSum { 78 public final int v0, v1, v2, v3; 79 80 public ValueInt4_by_int(int v0, int v1, int v2, int v3) { 81 this.v0 = v0; 82 this.v1 = v1; 83 this.v2 = v2; 84 this.v3 = v3; 85 } 86 87 public int sum() { 88 return v0 + v1 + v2 + v3; 89 } 90 } 91 92 public static value class ValueInt4_by_Int implements InterfaceSum { 93 public final ValueInt v0, v1, v2, v3; 94 95 public ValueInt4_by_Int(ValueInt v0, ValueInt v1, ValueInt v2, ValueInt v3) { 96 this.v0 = v0; 97 this.v1 = v1; 98 this.v2 = v2; 99 this.v3 = v3; 100 } 101 102 public ValueInt4_by_Int(int v0, int v1, int v2, int v3) { 103 this(new ValueInt(v0), new ValueInt(v1), new ValueInt(v2), new ValueInt(v3)); 104 } 105 106 public int sum() { 107 return v0.sum() + v1.sum() + v2.sum() + v3.sum(); 108 } 109 } 110 111 public static value class ValueInt4_by_int2 implements InterfaceSum { 112 public final ValueInt2_by_int v0, v1; 113 114 public ValueInt4_by_int2(ValueInt2_by_int v0, ValueInt2_by_int v1) { 115 this.v0 = v0; 116 this.v1 = v1; 117 } 118 119 public ValueInt4_by_int2(int v0, int v1, int v2, int v3) { 120 this(new ValueInt2_by_int(v0, v1), new ValueInt2_by_int(v2, v3)); 121 } 122 123 public int sum() { 124 return v0.sum() + v1.sum(); 125 } 126 } 127 128 public static value class ValueInt4_by_Int2 implements InterfaceSum { 129 130 public final ValueInt2_by_Int v0, v1; 131 132 public ValueInt4_by_Int2(ValueInt2_by_Int v0, ValueInt2_by_Int v1) { 133 this.v0 = v0; 134 this.v1 = v1; 135 } 136 137 public ValueInt4_by_Int2(int v0, int v1, int v2, int v3) { 138 this(new ValueInt2_by_Int(v0, v1), new ValueInt2_by_Int(v2, v3)); 139 } 140 public int sum() { 141 return v0.sum() + v1.sum(); 142 } 143 144 } 145 146 public static class ValState_of_Int extends SizeState { 147 public ValueInt[] arr; 148 @Setup 149 public void setup() { 150 arr = new ValueInt[size * 4]; 151 for (int i = 0; i < arr.length; i++) { 152 arr[i] = new ValueInt(i); 153 } 154 } 155 } 156 157 public static class IntState_of_Int extends SizeState { 158 public InterfaceSum[] arr; 159 @Setup 160 public void setup() { 161 arr = new InterfaceSum[size * 4]; 162 for (int i = 0; i < arr.length; i++) { 163 arr[i] = new ValueInt(i); 164 } 165 } 166 } 167 168 public static class ValState_of_Int2_by_int extends SizeState { 169 public ValueInt2_by_int[] arr; 170 @Setup 171 public void setup() { 172 arr = new ValueInt2_by_int[size * 2]; 173 for (int i = 0; i < arr.length; i++) { 174 arr[i] = new ValueInt2_by_int(2 * i, 2 * i + 1); 175 } 176 } 177 } 178 179 public static class IntState_of_Int2_by_int extends SizeState { 180 public InterfaceSum[] arr; 181 @Setup 182 public void setup() { 183 arr = new InterfaceSum[size * 2]; 184 for (int i = 0; i < arr.length; i++) { 185 arr[i] = new ValueInt2_by_int(2 * i, 2 * i + 1); 186 } 187 } 188 } 189 190 public static class ValState_of_Int2_by_Int extends SizeState { 191 public ValueInt2_by_Int[] arr; 192 @Setup 193 public void setup() { 194 arr = new ValueInt2_by_Int[size * 2]; 195 for (int i = 0; i < arr.length; i++) { 196 arr[i] = new ValueInt2_by_Int(2 * i, 2 * i + 1); 197 } 198 } 199 } 200 201 public static class IntState_of_Int2_by_Int extends SizeState { 202 public InterfaceSum[] arr; 203 @Setup 204 public void setup() { 205 arr = new InterfaceSum[size * 2]; 206 for (int i = 0; i < arr.length; i++) { 207 arr[i] = new ValueInt2_by_Int(2 * i, 2 * i + 1); 208 } 209 } 210 } 211 212 public static class ValState_of_Int4_by_int extends SizeState { 213 public ValueInt4_by_int[] arr; 214 @Setup 215 public void setup() { 216 arr = new ValueInt4_by_int[size]; 217 for (int i = 0; i < arr.length; i++) { 218 arr[i] = new ValueInt4_by_int(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3); 219 } 220 } 221 } 222 223 public static class IntState_of_Int4_by_int extends SizeState { 224 public InterfaceSum[] arr; 225 @Setup 226 public void setup() { 227 arr = new InterfaceSum[size]; 228 for (int i = 0; i < arr.length; i++) { 229 arr[i] = new ValueInt4_by_int(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3); 230 } 231 } 232 } 233 234 public static class ValState_of_Int4_by_Int extends SizeState { 235 public ValueInt4_by_Int[] arr; 236 @Setup 237 public void setup() { 238 arr = new ValueInt4_by_Int[size]; 239 for (int i = 0; i < arr.length; i++) { 240 arr[i] = new ValueInt4_by_Int(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3); 241 } 242 } 243 } 244 245 public static class IntState_of_Int4_by_Int extends SizeState { 246 public InterfaceSum[] arr; 247 @Setup 248 public void setup() { 249 arr = new InterfaceSum[size]; 250 for (int i = 0; i < arr.length; i++) { 251 arr[i] = new ValueInt4_by_Int(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3); 252 } 253 } 254 } 255 256 public static class ValState_of_Int4_by_int2 extends SizeState { 257 public ValueInt4_by_int2[] arr; 258 @Setup 259 public void setup() { 260 arr = new ValueInt4_by_int2[size]; 261 for (int i = 0; i < arr.length; i++) { 262 arr[i] = new ValueInt4_by_int2(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3); 263 } 264 } 265 } 266 267 public static class IntState_of_Int4_by_int2 extends SizeState { 268 public InterfaceSum[] arr; 269 @Setup 270 public void setup() { 271 arr = new InterfaceSum[size]; 272 for (int i = 0; i < arr.length; i++) { 273 arr[i] = new ValueInt4_by_int2(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3); 274 } 275 } 276 } 277 278 public static class ValState_of_Int4_by_Int2 extends SizeState { 279 public ValueInt4_by_Int2[] arr; 280 @Setup 281 public void setup() { 282 arr = new ValueInt4_by_Int2[size]; 283 for (int i = 0; i < arr.length; i++) { 284 arr[i] = new ValueInt4_by_Int2(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3); 285 } 286 } 287 } 288 289 public static class IntState_of_Int4_by_Int2 extends SizeState { 290 public InterfaceSum[] arr; 291 @Setup 292 public void setup() { 293 arr = new InterfaceSum[size]; 294 for (int i = 0; i < arr.length; i++) { 295 arr[i] = new ValueInt4_by_Int2(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3); 296 } 297 } 298 } 299 300 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 301 public int sum_interface(InterfaceSum[] src) { 302 int s = 0; 303 for (var v : src) { 304 s += v.sum(); 305 } 306 return s; 307 } 308 309 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 310 public int sum_val_of_Int(ValueInt[] src) { 311 int s = 0; 312 for (var v : src) { 313 s += v.sum(); 314 } 315 return s; 316 } 317 318 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 319 public int sum_val_of_Int2_by_int(ValueInt2_by_int[] 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_Int2_by_Int(ValueInt2_by_Int[] 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_Int4_by_int(ValueInt4_by_int[] 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_Int4_by_Int(ValueInt4_by_Int[] 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_by_int2(ValueInt4_by_int2[] 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_by_Int2(ValueInt4_by_Int2[] src) { 365 int s = 0; 366 for (var v : src) { 367 s += v.sum(); 368 } 369 return s; 370 } 371 372 @Benchmark 373 public int sum_interface_of_Int(IntState_of_Int st) { 374 return sum_interface(st.arr); 375 } 376 377 @Benchmark 378 public int sum_interface_of_Int2_by_int(IntState_of_Int2_by_int st) { 379 return sum_interface(st.arr); 380 } 381 382 @Benchmark 383 public int sum_interface_of_Int2_by_Int(IntState_of_Int2_by_Int st) { 384 return sum_interface(st.arr); 385 } 386 387 @Benchmark 388 public int sum_interface_of_Int4_by_int(IntState_of_Int4_by_int st) { 389 return sum_interface(st.arr); 390 } 391 392 @Benchmark 393 public int sum_interface_of_Int4_by_Int(IntState_of_Int4_by_Int st) { 394 return sum_interface(st.arr); 395 } 396 397 @Benchmark 398 public int sum_interface_of_Int4_by_int2(IntState_of_Int4_by_int2 st) { 399 return sum_interface(st.arr); 400 } 401 402 @Benchmark 403 public int sum_interface_of_Int4_by_Int2(IntState_of_Int4_by_Int2 st) { 404 return sum_interface(st.arr); 405 } 406 407 @Benchmark 408 public int sum_val_of_Int(ValState_of_Int st) { 409 return sum_val_of_Int(st.arr); 410 } 411 412 @Benchmark 413 public int sum_val_of_Int2_by_int(ValState_of_Int2_by_int st) { 414 return sum_val_of_Int2_by_int(st.arr); 415 } 416 417 @Benchmark 418 public int sum_val_of_Int2_by_Int(ValState_of_Int2_by_Int st) { 419 return sum_val_of_Int2_by_Int(st.arr); 420 } 421 422 @Benchmark 423 public int sum_val_of_Int4_by_int(ValState_of_Int4_by_int st) { 424 return sum_val_of_Int4_by_int(st.arr); 425 } 426 427 @Benchmark 428 public int sum_val_of_Int4_by_Int(ValState_of_Int4_by_Int st) { 429 return sum_val_of_Int4_by_Int(st.arr); 430 } 431 432 @Benchmark 433 public int sum_val_of_Int4_by_int2(ValState_of_Int4_by_int2 st) { 434 return sum_val_of_Int4_by_int2(st.arr); 435 } 436 437 @Benchmark 438 public int sum_val_of_Int4_by_Int2(ValState_of_Int4_by_Int2 st) { 439 return sum_val_of_Int4_by_Int2(st.arr); 440 } 441 442 }