1 /* 2 * Copyright (c) 2020, 2024, 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.invoke; 24 25 import org.openjdk.jmh.annotations.Benchmark; 26 import org.openjdk.jmh.annotations.BenchmarkMode; 27 import org.openjdk.jmh.annotations.CompilerControl; 28 import org.openjdk.jmh.annotations.Fork; 29 import org.openjdk.jmh.annotations.Measurement; 30 import org.openjdk.jmh.annotations.Mode; 31 import org.openjdk.jmh.annotations.OperationsPerInvocation; 32 import org.openjdk.jmh.annotations.OutputTimeUnit; 33 import org.openjdk.jmh.annotations.Scope; 34 import org.openjdk.jmh.annotations.Setup; 35 import org.openjdk.jmh.annotations.State; 36 import org.openjdk.jmh.annotations.Warmup; 37 38 import java.util.concurrent.TimeUnit; 39 40 @Fork(3) 41 @Warmup(iterations = 5, time = 1) 42 @Measurement(iterations = 5, time = 1) 43 @OutputTimeUnit(TimeUnit.NANOSECONDS) 44 @BenchmarkMode(Mode.AverageTime) 45 @State(Scope.Thread) 46 public class InlineArray1 { 47 48 public static final int SIZE = 128; 49 50 public interface MyInterface { 51 public int my_method(); 52 } 53 54 public static value class Val1 implements MyInterface { 55 public final int f0; 56 public Val1(int f0) { 57 this.f0 = f0; 58 } 59 @Override 60 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 61 public int my_method() { 62 return f0; 63 } 64 } 65 66 public static value class Val2 implements MyInterface { 67 public final int f0; 68 public Val2(int f0) { 69 this.f0 = f0; 70 } 71 @Override 72 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 73 public int my_method() { 74 return f0; 75 } 76 } 77 78 public static value class Val3 implements MyInterface { 79 public final int f0; 80 public Val3(int f0) { 81 this.f0 = f0; 82 } 83 @Override 84 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 85 public int my_method() { 86 return f0; 87 } 88 } 89 90 @State(Scope.Thread) 91 public static abstract class IntState { 92 public MyInterface[] arr; 93 } 94 95 @State(Scope.Thread) 96 public static abstract class Ref1State { 97 public Val1[] arr; 98 } 99 100 @State(Scope.Thread) 101 public static abstract class Val1State { 102 public Val1[] arr; 103 } 104 105 public static class Val1_as_Val extends Val1State { 106 @Setup 107 public void setup() { 108 arr = new Val1[SIZE]; 109 for (int i = 0; i < arr.length; i++) { 110 arr[i] = new Val1(i); 111 } 112 } 113 } 114 115 public static class Val1_as_Ref extends Ref1State { 116 @Setup 117 public void setup() { 118 arr = new Val1[SIZE]; 119 for (int i = 0; i < arr.length; i++) { 120 arr[i] = new Val1(i); 121 } 122 } 123 } 124 125 public static class Val1_as_Int extends IntState { 126 @Setup 127 public void setup() { 128 arr = new Val1[SIZE]; 129 for (int i = 0; i < arr.length; i++) { 130 arr[i] = new Val1(i); 131 } 132 } 133 } 134 135 public static class Ref1_as_Ref extends Ref1State { 136 @Setup 137 public void setup() { 138 arr = new Val1[SIZE]; 139 for (int i = 0; i < arr.length; i++) { 140 arr[i] = new Val1(i); 141 } 142 } 143 } 144 145 public static class Ref1_as_Int extends IntState { 146 @Setup 147 public void setup() { 148 arr = new Val1[SIZE]; 149 for (int i = 0; i < arr.length; i++) { 150 arr[i] = new Val1(i); 151 } 152 } 153 } 154 155 public static class Int1_as_Int extends IntState { 156 @Setup 157 public void setup() { 158 arr = new MyInterface[SIZE]; 159 for (int i = 0; i < arr.length; i++) { 160 arr[i] = new Val1(i); 161 } 162 } 163 } 164 165 public static class Val2_as_Int extends IntState { 166 @Setup 167 public void setup() { 168 arr = new Val2[SIZE]; 169 for (int i = 0; i < arr.length; i++) { 170 arr[i] = new Val2(i); 171 } 172 } 173 } 174 175 public static class Ref2_as_Int extends IntState { 176 @Setup 177 public void setup() { 178 arr = new Val2[SIZE]; 179 for (int i = 0; i < arr.length; i++) { 180 arr[i] = new Val2(i); 181 } 182 } 183 } 184 185 public static class Int2_as_Int extends IntState { 186 @Setup 187 public void setup() { 188 arr = new MyInterface[SIZE]; 189 for (int i = 0; i < arr.length; i++) { 190 arr[i] = new Val2(i); 191 } 192 } 193 } 194 195 public static class Val3_as_Int extends IntState { 196 @Setup 197 public void setup() { 198 arr = new Val3[SIZE]; 199 for (int i = 0; i < arr.length; i++) { 200 arr[i] = new Val3(i); 201 } 202 } 203 } 204 205 public static class Ref3_as_Int extends IntState { 206 @Setup 207 public void setup() { 208 arr = new Val3[SIZE]; 209 for (int i = 0; i < arr.length; i++) { 210 arr[i] = new Val3(i); 211 } 212 } 213 } 214 215 public static class Int3_as_Int extends IntState { 216 @Setup 217 public void setup() { 218 arr = new MyInterface[SIZE]; 219 for (int i = 0; i < arr.length; i++) { 220 arr[i] = new Val3(i); 221 } 222 } 223 } 224 225 226 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 227 public int reduceInt(MyInterface[] arr) { 228 int r = 0; 229 for (int i = 0; i < arr.length; i++) { 230 r += arr[i].my_method(); 231 } 232 return r; 233 } 234 235 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 236 public int reduceRef(Val1[] arr) { 237 int r = 0; 238 for (int i = 0; i < arr.length; i++) { 239 r += arr[i].my_method(); 240 } 241 return r; 242 } 243 244 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 245 public int reduceVal(Val1[] arr) { 246 int r = 0; 247 for (int i = 0; i < arr.length; i++) { 248 r += arr[i].my_method(); 249 } 250 return r; 251 } 252 253 @Benchmark 254 @OperationsPerInvocation(SIZE * 6) 255 @CompilerControl(CompilerControl.Mode.INLINE) 256 public int target1_Val_v(Val1_as_Val st0, Val1_as_Val st1, Val1_as_Val st2, Val1_as_Val st3, Val1_as_Val st4, Val1_as_Val st5) { 257 return reduceVal(st0.arr) + 258 reduceVal(st1.arr) + 259 reduceVal(st2.arr) + 260 reduceVal(st3.arr) + 261 reduceVal(st4.arr) + 262 reduceVal(st5.arr); 263 } 264 265 @Benchmark 266 @OperationsPerInvocation(SIZE * 6) 267 @CompilerControl(CompilerControl.Mode.INLINE) 268 public int target1_Ref_v(Val1_as_Ref st0, Val1_as_Ref st1, Val1_as_Ref st2, Val1_as_Ref st3, Val1_as_Ref st4, Val1_as_Ref st5) { 269 return reduceRef(st0.arr) + 270 reduceRef(st1.arr) + 271 reduceRef(st2.arr) + 272 reduceRef(st3.arr) + 273 reduceRef(st4.arr) + 274 reduceRef(st5.arr); 275 } 276 277 @Benchmark 278 @OperationsPerInvocation(SIZE * 6) 279 @CompilerControl(CompilerControl.Mode.INLINE) 280 public int target1_Ref_r(Ref1_as_Ref st0, Ref1_as_Ref st1, Ref1_as_Ref st2, Ref1_as_Ref st3, Ref1_as_Ref st4, Ref1_as_Ref st5) { 281 return reduceRef(st0.arr) + 282 reduceRef(st1.arr) + 283 reduceRef(st2.arr) + 284 reduceRef(st3.arr) + 285 reduceRef(st4.arr) + 286 reduceRef(st5.arr); 287 } 288 289 @Benchmark 290 @OperationsPerInvocation(SIZE * 6) 291 @CompilerControl(CompilerControl.Mode.INLINE) 292 public int target1_Ref_vr(Val1_as_Ref st0, Ref1_as_Ref st1, Val1_as_Ref st2, Ref1_as_Ref st3, Val1_as_Ref st4, Ref1_as_Ref st5) { 293 return reduceRef(st0.arr) + 294 reduceRef(st1.arr) + 295 reduceRef(st2.arr) + 296 reduceRef(st3.arr) + 297 reduceRef(st4.arr) + 298 reduceRef(st5.arr); 299 } 300 301 @Benchmark 302 @OperationsPerInvocation(SIZE * 6) 303 @CompilerControl(CompilerControl.Mode.INLINE) 304 public int target1_Int_v(Val1_as_Int st0, Val1_as_Int st1, Val1_as_Int st2, Val1_as_Int st3, Val1_as_Int st4, Val1_as_Int st5) { 305 return reduceInt(st0.arr) + 306 reduceInt(st1.arr) + 307 reduceInt(st2.arr) + 308 reduceInt(st3.arr) + 309 reduceInt(st4.arr) + 310 reduceInt(st5.arr); 311 } 312 313 @Benchmark 314 @OperationsPerInvocation(SIZE * 6) 315 @CompilerControl(CompilerControl.Mode.INLINE) 316 public int target1_Int_r(Ref1_as_Int st0, Ref1_as_Int st1, Ref1_as_Int st2, Ref1_as_Int st3, Ref1_as_Int st4, Ref1_as_Int st5) { 317 return reduceInt(st0.arr) + 318 reduceInt(st1.arr) + 319 reduceInt(st2.arr) + 320 reduceInt(st3.arr) + 321 reduceInt(st4.arr) + 322 reduceInt(st5.arr); 323 } 324 325 @Benchmark 326 @OperationsPerInvocation(SIZE * 6) 327 @CompilerControl(CompilerControl.Mode.INLINE) 328 public int target1_Int_i(Int1_as_Int st0, Int1_as_Int st1, Int1_as_Int st2, Int1_as_Int st3, Int1_as_Int st4, Int1_as_Int st5) { 329 return reduceInt(st0.arr) + 330 reduceInt(st1.arr) + 331 reduceInt(st2.arr) + 332 reduceInt(st3.arr) + 333 reduceInt(st4.arr) + 334 reduceInt(st5.arr); 335 } 336 337 @Benchmark 338 @OperationsPerInvocation(SIZE * 6) 339 @CompilerControl(CompilerControl.Mode.INLINE) 340 public int target1_Int_vr(Val1_as_Int st0, Ref1_as_Int st1, Val1_as_Int st2, Ref1_as_Int st3, Val1_as_Int st4, Ref1_as_Int st5) { 341 return reduceInt(st0.arr) + 342 reduceInt(st1.arr) + 343 reduceInt(st2.arr) + 344 reduceInt(st3.arr) + 345 reduceInt(st4.arr) + 346 reduceInt(st5.arr); 347 } 348 349 @Benchmark 350 @OperationsPerInvocation(SIZE * 6) 351 @CompilerControl(CompilerControl.Mode.INLINE) 352 public int target1_Int_vi(Val1_as_Int st0, Int1_as_Int st1, Val1_as_Int st2, Int1_as_Int st3, Val1_as_Int st4, Int1_as_Int st5) { 353 return reduceInt(st0.arr) + 354 reduceInt(st1.arr) + 355 reduceInt(st2.arr) + 356 reduceInt(st3.arr) + 357 reduceInt(st4.arr) + 358 reduceInt(st5.arr); 359 } 360 361 @Benchmark 362 @OperationsPerInvocation(SIZE * 6) 363 @CompilerControl(CompilerControl.Mode.INLINE) 364 public int target1_Int_ri(Ref1_as_Int st0, Int1_as_Int st1, Ref1_as_Int st2, Int1_as_Int st3, Ref1_as_Int st4, Int1_as_Int st5) { 365 return reduceInt(st0.arr) + 366 reduceInt(st1.arr) + 367 reduceInt(st2.arr) + 368 reduceInt(st3.arr) + 369 reduceInt(st4.arr) + 370 reduceInt(st5.arr); 371 } 372 373 @Benchmark 374 @OperationsPerInvocation(SIZE * 6) 375 @CompilerControl(CompilerControl.Mode.INLINE) 376 public int target1_Int_vri(Val1_as_Int st0, Ref1_as_Int st1, Int1_as_Int st2, Val1_as_Int st3, Ref1_as_Int st4, Int1_as_Int st5) { 377 return reduceInt(st0.arr) + 378 reduceInt(st1.arr) + 379 reduceInt(st2.arr) + 380 reduceInt(st3.arr) + 381 reduceInt(st4.arr) + 382 reduceInt(st5.arr); 383 } 384 385 @Benchmark 386 @OperationsPerInvocation(SIZE * 6) 387 @CompilerControl(CompilerControl.Mode.INLINE) 388 public int target2_Int_v(Val1_as_Int st0, Val2_as_Int st1, Val1_as_Int st2, Val2_as_Int st3, Val1_as_Int st4, Val2_as_Int st5) { 389 return reduceInt(st0.arr) + 390 reduceInt(st1.arr) + 391 reduceInt(st2.arr) + 392 reduceInt(st3.arr) + 393 reduceInt(st4.arr) + 394 reduceInt(st5.arr); 395 } 396 397 @Benchmark 398 @OperationsPerInvocation(SIZE * 6) 399 @CompilerControl(CompilerControl.Mode.INLINE) 400 public int target2_Int_r(Ref1_as_Int st0, Ref2_as_Int st1, Ref1_as_Int st2, Ref2_as_Int st3, Ref1_as_Int st4, Ref2_as_Int st5) { 401 return reduceInt(st0.arr) + 402 reduceInt(st1.arr) + 403 reduceInt(st2.arr) + 404 reduceInt(st3.arr) + 405 reduceInt(st4.arr) + 406 reduceInt(st5.arr); 407 } 408 409 @Benchmark 410 @OperationsPerInvocation(SIZE * 6) 411 @CompilerControl(CompilerControl.Mode.INLINE) 412 public int target2_Int_i(Int1_as_Int st0, Int2_as_Int st1, Int1_as_Int st2, Int2_as_Int st3, Int1_as_Int st4, Int2_as_Int st5) { 413 return reduceInt(st0.arr) + 414 reduceInt(st1.arr) + 415 reduceInt(st2.arr) + 416 reduceInt(st3.arr) + 417 reduceInt(st4.arr) + 418 reduceInt(st5.arr); 419 } 420 421 @Benchmark 422 @OperationsPerInvocation(SIZE * 6) 423 @CompilerControl(CompilerControl.Mode.INLINE) 424 public int target2_Int_vr(Val1_as_Int st0, Ref2_as_Int st1, Val2_as_Int st2, Ref1_as_Int st3, Val1_as_Int st4, Ref2_as_Int st5) { 425 return reduceInt(st0.arr) + 426 reduceInt(st1.arr) + 427 reduceInt(st2.arr) + 428 reduceInt(st3.arr) + 429 reduceInt(st4.arr) + 430 reduceInt(st5.arr); 431 } 432 433 @Benchmark 434 @OperationsPerInvocation(SIZE * 6) 435 @CompilerControl(CompilerControl.Mode.INLINE) 436 public int target2_Int_vi(Val1_as_Int st0, Int2_as_Int st1, Val2_as_Int st2, Int1_as_Int st3, Val1_as_Int st4, Int2_as_Int st5) { 437 return reduceInt(st0.arr) + 438 reduceInt(st1.arr) + 439 reduceInt(st2.arr) + 440 reduceInt(st3.arr) + 441 reduceInt(st4.arr) + 442 reduceInt(st5.arr); 443 } 444 445 @Benchmark 446 @OperationsPerInvocation(SIZE * 6) 447 @CompilerControl(CompilerControl.Mode.INLINE) 448 public int target2_Int_ri(Ref1_as_Int st0, Int1_as_Int st1, Ref2_as_Int st2, Int1_as_Int st3, Ref1_as_Int st4, Int2_as_Int st5) { 449 return reduceInt(st0.arr) + 450 reduceInt(st1.arr) + 451 reduceInt(st2.arr) + 452 reduceInt(st3.arr) + 453 reduceInt(st4.arr) + 454 reduceInt(st5.arr); 455 } 456 457 @Benchmark 458 @OperationsPerInvocation(SIZE * 6) 459 @CompilerControl(CompilerControl.Mode.INLINE) 460 public int target2_Int_vri(Val1_as_Int st0, Ref1_as_Int st1, Int1_as_Int st2, Val2_as_Int st3, Ref2_as_Int st4, Int2_as_Int st5) { 461 return reduceInt(st0.arr) + 462 reduceInt(st1.arr) + 463 reduceInt(st2.arr) + 464 reduceInt(st3.arr) + 465 reduceInt(st4.arr) + 466 reduceInt(st5.arr); 467 } 468 469 470 @Benchmark 471 @OperationsPerInvocation(SIZE * 6) 472 @CompilerControl(CompilerControl.Mode.INLINE) 473 public int target3_Int_v(Val1_as_Int st0, Val2_as_Int st1, Val3_as_Int st2, Val1_as_Int st3, Val2_as_Int st4, Val3_as_Int st5) { 474 return reduceInt(st0.arr) + 475 reduceInt(st1.arr) + 476 reduceInt(st2.arr) + 477 reduceInt(st3.arr) + 478 reduceInt(st4.arr) + 479 reduceInt(st5.arr); 480 } 481 482 @Benchmark 483 @OperationsPerInvocation(SIZE * 6) 484 @CompilerControl(CompilerControl.Mode.INLINE) 485 public int target3_Int_r(Ref1_as_Int st0, Ref2_as_Int st1, Ref3_as_Int st2, Ref1_as_Int st3, Ref2_as_Int st4, Ref3_as_Int st5) { 486 return reduceInt(st0.arr) + 487 reduceInt(st1.arr) + 488 reduceInt(st2.arr) + 489 reduceInt(st3.arr) + 490 reduceInt(st4.arr) + 491 reduceInt(st5.arr); 492 } 493 494 @Benchmark 495 @OperationsPerInvocation(SIZE * 6) 496 @CompilerControl(CompilerControl.Mode.INLINE) 497 public int target3_Int_i(Int1_as_Int st0, Int2_as_Int st1, Int3_as_Int st2, Int1_as_Int st3, Int2_as_Int st4, Int3_as_Int st5) { 498 return reduceInt(st0.arr) + 499 reduceInt(st1.arr) + 500 reduceInt(st2.arr) + 501 reduceInt(st3.arr) + 502 reduceInt(st4.arr) + 503 reduceInt(st5.arr); 504 } 505 506 @Benchmark 507 @OperationsPerInvocation(SIZE * 6) 508 @CompilerControl(CompilerControl.Mode.INLINE) 509 public int target3_Int_vr(Val1_as_Int st0, Ref2_as_Int st1, Val3_as_Int st2, Ref1_as_Int st3, Val2_as_Int st4, Ref3_as_Int st5) { 510 return reduceInt(st0.arr) + 511 reduceInt(st1.arr) + 512 reduceInt(st2.arr) + 513 reduceInt(st3.arr) + 514 reduceInt(st4.arr) + 515 reduceInt(st5.arr); 516 } 517 518 @Benchmark 519 @OperationsPerInvocation(SIZE * 6) 520 @CompilerControl(CompilerControl.Mode.INLINE) 521 public int target3_Int_vi(Val1_as_Int st0, Int2_as_Int st1, Val3_as_Int st2, Int1_as_Int st3, Val3_as_Int st4, Int3_as_Int st5) { 522 return reduceInt(st0.arr) + 523 reduceInt(st1.arr) + 524 reduceInt(st2.arr) + 525 reduceInt(st3.arr) + 526 reduceInt(st4.arr) + 527 reduceInt(st5.arr); 528 } 529 530 @Benchmark 531 @OperationsPerInvocation(SIZE * 6) 532 @CompilerControl(CompilerControl.Mode.INLINE) 533 public int target3_Int_ri(Ref1_as_Int st0, Int2_as_Int st1, Ref3_as_Int st2, Int1_as_Int st3, Ref2_as_Int st4, Int3_as_Int st5) { 534 return reduceInt(st0.arr) + 535 reduceInt(st1.arr) + 536 reduceInt(st2.arr) + 537 reduceInt(st3.arr) + 538 reduceInt(st4.arr) + 539 reduceInt(st5.arr); 540 } 541 542 @Benchmark 543 @OperationsPerInvocation(SIZE * 6) 544 @CompilerControl(CompilerControl.Mode.INLINE) 545 public int target3_Int_vri(Val1_as_Int st0, Ref2_as_Int st1, Int3_as_Int st2, Val2_as_Int st3, Ref3_as_Int st4, Int1_as_Int st5) { 546 return reduceInt(st0.arr) + 547 reduceInt(st1.arr) + 548 reduceInt(st2.arr) + 549 reduceInt(st3.arr) + 550 reduceInt(st4.arr) + 551 reduceInt(st5.arr); 552 } 553 554 555 }