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 Value8 {
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 static value class ValueInt4 {
88
89 public final int v0, v1, v2, v3;
90
91 public ValueInt4(int v0, int v1, int v2, int v3) {
92 this.v0 = v0;
93 this.v1 = v1;
94 this.v2 = v2;
95 this.v3 = v3;
96 }
97
98 public int value0() {
99 return v0;
100 }
101
102 public static ValueInt4 valueOf(int v0, int v1, int v2, int v3) {
103 return new ValueInt4(v0, v1, v2, v3);
104 }
105 }
106
107 public static value class ValueInt8 {
108
109 public final int v0, v1, v2, v3, v4, v5, v6, v7;
110
111 public ValueInt8(int v0, int v1, int v2, int v3, int v4, int v5, int v6, int v7) {
112 this.v0 = v0;
113 this.v1 = v1;
114 this.v2 = v2;
115 this.v3 = v3;
116 this.v4 = v4;
117 this.v5 = v5;
118 this.v6 = v6;
119 this.v7 = v7;
120 }
121
122 public int value0() {
123 return v0;
124 }
125
126 public static ValueInt8 valueOf(int v0, int v1, int v2, int v3, int v4, int v5, int v6, int v7) {
127 return new ValueInt8(v0, v1, v2, v3, v4, v5, v6, v7);
128 }
129 }
130
131 public abstract static class InvocationLogic {
132 public abstract ValueInt1 compute(ValueInt1 v1, ValueInt1 v2, ValueInt1 v3, ValueInt1 v4, ValueInt1 v5, ValueInt1 v6, ValueInt1 v7, ValueInt1 v8);
133 public abstract ValueInt2 compute(ValueInt2 v1, ValueInt2 v2, ValueInt2 v3, ValueInt2 v4);
134 public abstract ValueInt4 compute(ValueInt4 v1, ValueInt4 v2);
135 public abstract ValueInt8 compute(ValueInt8 v1);
136 }
137
138 public static class InvokeImpl1 extends InvocationLogic {
139 @Override
140 public ValueInt1 compute(ValueInt1 v1, ValueInt1 v2, ValueInt1 v3, ValueInt1 v4, ValueInt1 v5, ValueInt1 v6, ValueInt1 v7, ValueInt1 v8) {
141 return v1;
142 }
143 @Override
144 public ValueInt2 compute(ValueInt2 v1, ValueInt2 v2, ValueInt2 v3, ValueInt2 v4) {
145 return v1;
146 }
147 @Override
148 public ValueInt4 compute(ValueInt4 v1, ValueInt4 v2) {
149 return v1;
150 }
151 @Override
152 public ValueInt8 compute(ValueInt8 v1) {
153 return v1;
154 }
155 }
156
157 public static class InvokeImpl2 extends InvocationLogic {
158 @Override
159 public ValueInt1 compute(ValueInt1 v1, ValueInt1 v2, ValueInt1 v3, ValueInt1 v4, ValueInt1 v5, ValueInt1 v6, ValueInt1 v7, ValueInt1 v8) {
160 return v1;
161 }
162 @Override
163 public ValueInt2 compute(ValueInt2 v1, ValueInt2 v2, ValueInt2 v3, ValueInt2 v4) {
164 return v1;
165 }
166 @Override
167 public ValueInt4 compute(ValueInt4 v1, ValueInt4 v2) {
168 return v1;
169 }
170 @Override
171 public ValueInt8 compute(ValueInt8 v1) {
172 return v1;
173 }
174 }
175
176 public static class InvokeImpl3 extends InvocationLogic {
177 @Override
178 public ValueInt1 compute(ValueInt1 v1, ValueInt1 v2, ValueInt1 v3, ValueInt1 v4, ValueInt1 v5, ValueInt1 v6, ValueInt1 v7, ValueInt1 v8) {
179 return v1;
180 }
181 @Override
182 public ValueInt2 compute(ValueInt2 v1, ValueInt2 v2, ValueInt2 v3, ValueInt2 v4) {
183 return v1;
184 }
185 @Override
186 public ValueInt4 compute(ValueInt4 v1, ValueInt4 v2) {
187 return v1;
188 }
189 @Override
190 public ValueInt8 compute(ValueInt8 v1) {
191 return v1;
192 }
193 }
194
195 @State(Scope.Thread)
196 public static class StateTargets {
197 InvocationLogic[] arr;
198
199 @Setup
200 public void setup() {
201 arr = new InvocationLogic[SIZE];
202 Arrays.setAll(arr, i -> getImpl(i, 3));
203 }
204
205 private InvocationLogic getImpl(int i, int targets) {
206 return switch (i % targets) {
207 case 0 -> new InvokeImpl1();
208 case 1 -> new InvokeImpl2();
209 default -> new InvokeImpl3();
210 };
211 }
212 }
213
214 ValueInt1 a0 = ValueInt1.valueOf(42);
215 ValueInt1 a1 = ValueInt1.valueOf(43);
216 ValueInt1 a2 = ValueInt1.valueOf(44);
217 ValueInt1 a3 = ValueInt1.valueOf(45);
218 ValueInt1 a4 = ValueInt1.valueOf(46);
219 ValueInt1 a5 = ValueInt1.valueOf(47);
220 ValueInt1 a6 = ValueInt1.valueOf(48);
221 ValueInt1 a7 = ValueInt1.valueOf(49);
222
223 ValueInt2 b0 = ValueInt2.valueOf(42, 43);
224 ValueInt2 b1 = ValueInt2.valueOf(44, 45);
225 ValueInt2 b2 = ValueInt2.valueOf(46, 47);
226 ValueInt2 b3 = ValueInt2.valueOf(48, 49);
227
228 ValueInt4 c0 = ValueInt4.valueOf(42, 43, 44, 45);
229 ValueInt4 c1 = ValueInt4.valueOf(46, 47, 48, 49);
230
231 ValueInt8 d0 = ValueInt8.valueOf(42, 43, 44, 45, 46, 47, 48, 49);
232
233 @Benchmark
234 @OperationsPerInvocation(SIZE)
235 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
236 public void value_args8(Blackhole bh, StateTargets st) {
237 ValueInt1 v0 = a0;
238 ValueInt1 v1 = a1;
239 ValueInt1 v2 = a2;
240 ValueInt1 v3 = a3;
241 ValueInt1 v4 = a4;
242 ValueInt1 v5 = a5;
243 ValueInt1 v6 = a6;
244 ValueInt1 v7 = a7;
245 InvocationLogic[] arr = st.arr;
246 for (InvocationLogic t : arr) {
247 bh.consume(t.compute(v0, v1, v2, v3, v4, v5, v6, v7));
248 }
249 }
250
251 @Benchmark
252 @OperationsPerInvocation(SIZE)
253 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
254 public void value_args4(Blackhole bh, StateTargets st) {
255 ValueInt2 v0 = b0;
256 ValueInt2 v1 = b1;
257 ValueInt2 v2 = b2;
258 ValueInt2 v3 = b3;
259 InvocationLogic[] arr = st.arr;
260 for (InvocationLogic t : arr) {
261 bh.consume(t.compute(v0, v1, v2, v3));
262 }
263 }
264
265 @Benchmark
266 @OperationsPerInvocation(SIZE)
267 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
268 public void value_args2(Blackhole bh, StateTargets st) {
269 ValueInt4 v0 = c0;
270 ValueInt4 v1 = c1;
271 InvocationLogic[] arr = st.arr;
272 for (InvocationLogic t : arr) {
273 bh.consume(t.compute(v0, v1));
274 }
275 }
276
277 @Benchmark
278 @OperationsPerInvocation(SIZE)
279 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
280 public void value_args1(Blackhole bh, StateTargets st) {
281 ValueInt8 v0 = d0;
282 InvocationLogic[] arr = st.arr;
283 for (InvocationLogic t : arr) {
284 bh.consume(t.compute(v0));
285 }
286 }
287
288 }