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(value = 3, jvmArgsAppend = {"--enable-preview"})
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 Value2 {
49
50 public static final int SIZE = 96; // must be divisible by 2 and 3 and around 100
51
52 public static value class ValueInt1 {
53
54 public final int v0;
55
56 public ValueInt1(int v0) {
57 this.v0 = v0;
58 }
59
60 public int value() {
61 return v0;
62 }
63
64 public static ValueInt1 valueOf(int value) {
65 return new ValueInt1(value);
66 }
67 }
68
69 public static value class ValueInt2 {
70
71 public final int v0, v1;
72
73 public ValueInt2(int v0, int v1) {
74 this.v0 = v0;
75 this.v1 = v1;
76 }
77
78 public int value0() {
79 return v0;
80 }
81
82 public static ValueInt2 valueOf(int v0, int v1) {
83 return new ValueInt2(v0, v1);
84 }
85 }
86
87 public abstract static class InvocationLogic {
88 public abstract ValueInt1 compute(ValueInt1 v1, ValueInt1 v2);
89 public abstract ValueInt2 compute(ValueInt2 v1);
90 }
91
92 public static class InvokeImpl1 extends InvocationLogic {
93 @Override
94 public ValueInt1 compute(ValueInt1 v1, ValueInt1 v2) {
95 return v1;
96 }
97 @Override
98 public ValueInt2 compute(ValueInt2 v1) {
99 return v1;
100 }
101 }
102
103 public static class InvokeImpl2 extends InvocationLogic {
104 @Override
105 public ValueInt1 compute(ValueInt1 v1, ValueInt1 v2) {
106 return v1;
107 }
108 @Override
109 public ValueInt2 compute(ValueInt2 v1) {
110 return v1;
111 }
112 }
113
114 public static class InvokeImpl3 extends InvocationLogic {
115 @Override
116 public ValueInt1 compute(ValueInt1 v1, ValueInt1 v2) {
117 return v1;
118 }
119 @Override
120 public ValueInt2 compute(ValueInt2 v1) {
121 return v1;
122 }
123 }
124
125 @State(Scope.Thread)
126 public static class StateTargets {
127 InvocationLogic[] arr;
128
129 @Setup
130 public void setup() {
131 arr = new InvocationLogic[SIZE];
132 Arrays.setAll(arr, i -> getImpl(i, 3));
133 }
134
135 private InvocationLogic getImpl(int i, int targets) {
136 return switch (i % targets) {
137 case 0 -> new InvokeImpl1();
138 case 1 -> new InvokeImpl2();
139 default -> new InvokeImpl3();
140 };
141 }
142 }
143
144 ValueInt1 a0 = ValueInt1.valueOf(42);
145 ValueInt1 a1 = ValueInt1.valueOf(43);
146 ValueInt2 b0 = ValueInt2.valueOf(42, 43);
147
148 @Benchmark
149 @OperationsPerInvocation(SIZE)
150 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
151 public void value_args2(Blackhole bh, StateTargets st) {
152 ValueInt1 v0 = a0;
153 ValueInt1 v1 = a1;
154 InvocationLogic[] arr = st.arr;
155 for (InvocationLogic t : arr) {
156 bh.consume(t.compute(v0, v1));
157 }
158 }
159
160 @Benchmark
161 @OperationsPerInvocation(SIZE)
162 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
163 public void value_args1(Blackhole bh, StateTargets st) {
164 ValueInt2 v0 = b0;
165 InvocationLogic[] arr = st.arr;
166 for (InvocationLogic t : arr) {
167 bh.consume(t.compute(v0));
168 }
169 }
170
171 }