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.callconv;
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 import org.openjdk.jmh.infra.Blackhole;
38
39 import java.util.Arrays;
40 import java.util.concurrent.TimeUnit;
41
42 @Fork(3)
43 @Warmup(iterations = 5, time = 1)
44 @Measurement(iterations = 5, time = 1)
45 @OutputTimeUnit(TimeUnit.NANOSECONDS)
46 @BenchmarkMode(Mode.AverageTime)
47 @State(Scope.Thread)
48 public class Primitive2 {
49
50 public static final int SIZE = 96; // must be divisible by 2 and 3 and around 100
51
52 public abstract static class InvocationLogic {
53 public abstract int compute(int v1, int v2);
54 }
55
56 public static class InvokeImpl1 extends InvocationLogic {
57 @Override
58 public int compute(int v1, int v2) {
59 return v1;
60 }
61 }
62
63 public static class InvokeImpl2 extends InvocationLogic {
64 @Override
65 public int compute(int v1, int v2) {
66 return v1;
67 }
68 }
69
70 public static class InvokeImpl3 extends InvocationLogic {
71 @Override
72 public int compute(int v1, int v2) {
73 return v1;
74 }
75 }
76
77 @State(Scope.Thread)
78 public static class StateTargets {
79 InvocationLogic[] arr;
80
81 @Setup
82 public void setup() {
83 arr = new InvocationLogic[SIZE];
84 Arrays.setAll(arr, i -> getImpl(i, 3));
85 }
86
87 private InvocationLogic getImpl(int i, int targets) {
88 return switch (i % targets) {
89 case 0 -> new InvokeImpl1();
90 case 1 -> new InvokeImpl2();
91 default -> new InvokeImpl3();
92 };
93 }
94 }
95
96 int a0 = 42;
97 int a1 = 43;
98
99 @Benchmark
100 @OperationsPerInvocation(SIZE)
101 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
102 public void primitive_args2(Blackhole bh, StateTargets st) {
103 InvocationLogic[] arr = st.arr;
104 for (InvocationLogic t : arr) {
105 bh.consume(t.compute(a0, a1));
106 }
107 }
108
109 }