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 Value4 { 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 abstract static class InvocationLogic { 108 public abstract ValueInt1 compute(ValueInt1 v1, ValueInt1 v2, ValueInt1 v3, ValueInt1 v4); 109 public abstract ValueInt2 compute(ValueInt2 v1, ValueInt2 v2); 110 public abstract ValueInt4 compute(ValueInt4 v1); 111 } 112 113 public static class InvokeImpl1 extends InvocationLogic { 114 @Override 115 public ValueInt1 compute(ValueInt1 v1, ValueInt1 v2, ValueInt1 v3, ValueInt1 v4) { 116 return v1; 117 } 118 @Override 119 public ValueInt2 compute(ValueInt2 v1, ValueInt2 v2) { 120 return v1; 121 } 122 @Override 123 public ValueInt4 compute(ValueInt4 v1) { 124 return v1; 125 } 126 } 127 128 public static class InvokeImpl2 extends InvocationLogic { 129 @Override 130 public ValueInt1 compute(ValueInt1 v1, ValueInt1 v2, ValueInt1 v3, ValueInt1 v4) { 131 return v1; 132 } 133 @Override 134 public ValueInt2 compute(ValueInt2 v1, ValueInt2 v2) { 135 return v1; 136 } 137 @Override 138 public ValueInt4 compute(ValueInt4 v1) { 139 return v1; 140 } 141 } 142 143 public static class InvokeImpl3 extends InvocationLogic { 144 @Override 145 public ValueInt1 compute(ValueInt1 v1, ValueInt1 v2, ValueInt1 v3, ValueInt1 v4) { 146 return v1; 147 } 148 @Override 149 public ValueInt2 compute(ValueInt2 v1, ValueInt2 v2) { 150 return v1; 151 } 152 @Override 153 public ValueInt4 compute(ValueInt4 v1) { 154 return v1; 155 } 156 } 157 158 @State(Scope.Thread) 159 public static class StateTargets { 160 InvocationLogic[] arr; 161 162 @Setup 163 public void setup() { 164 arr = new InvocationLogic[SIZE]; 165 Arrays.setAll(arr, i -> getImpl(i, 3)); 166 } 167 168 private InvocationLogic getImpl(int i, int targets) { 169 return switch (i % targets) { 170 case 0 -> new InvokeImpl1(); 171 case 1 -> new InvokeImpl2(); 172 default -> new InvokeImpl3(); 173 }; 174 } 175 } 176 177 ValueInt1 a0 = ValueInt1.valueOf(42); 178 ValueInt1 a1 = ValueInt1.valueOf(43); 179 ValueInt1 a2 = ValueInt1.valueOf(44); 180 ValueInt1 a3 = ValueInt1.valueOf(45); 181 182 ValueInt2 b0 = ValueInt2.valueOf(42, 43); 183 ValueInt2 b1 = ValueInt2.valueOf(44, 45); 184 185 ValueInt4 c0 = ValueInt4.valueOf(42, 43, 44, 45); 186 187 @Benchmark 188 @OperationsPerInvocation(SIZE) 189 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 190 public void value_args4(Blackhole bh, StateTargets st) { 191 ValueInt1 v0 = a0; 192 ValueInt1 v1 = a1; 193 ValueInt1 v2 = a2; 194 ValueInt1 v3 = a3; 195 InvocationLogic[] arr = st.arr; 196 for (InvocationLogic t : arr) { 197 bh.consume(t.compute(v0, v1, v2, v3)); 198 } 199 } 200 201 @Benchmark 202 @OperationsPerInvocation(SIZE) 203 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 204 public void value_args2(Blackhole bh, StateTargets st) { 205 ValueInt2 v0 = b0; 206 ValueInt2 v1 = b1; 207 InvocationLogic[] arr = st.arr; 208 for (InvocationLogic t : arr) { 209 bh.consume(t.compute(v0, v1)); 210 } 211 } 212 213 @Benchmark 214 @OperationsPerInvocation(SIZE) 215 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 216 public void value_args1(Blackhole bh, StateTargets st) { 217 ValueInt4 v0 = c0; 218 InvocationLogic[] arr = st.arr; 219 for (InvocationLogic t : arr) { 220 bh.consume(t.compute(v0)); 221 } 222 } 223 224 }