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 Value1 {
 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 abstract static class InvocationLogic {
 70         public abstract ValueInt1 compute(ValueInt1 v1);
 71     }
 72 
 73     public static class InvokeImpl1 extends InvocationLogic {
 74         @Override
 75         public ValueInt1 compute(ValueInt1 v1) {
 76             return v1;
 77         }
 78     }
 79 
 80     public static class InvokeImpl2 extends InvocationLogic {
 81         @Override
 82         public ValueInt1 compute(ValueInt1 v1) {
 83             return v1;
 84         }
 85     }
 86 
 87     public static class InvokeImpl3 extends InvocationLogic {
 88         @Override
 89         public ValueInt1 compute(ValueInt1 v1) {
 90             return v1;
 91         }
 92     }
 93 
 94     @State(Scope.Thread)
 95     public static class StateTargets {
 96         InvocationLogic[] arr;
 97 
 98         @Setup
 99         public void setup() {
100             arr = new InvocationLogic[SIZE];
101             Arrays.setAll(arr, i -> getImpl(i, 3));
102         }
103 
104         private  InvocationLogic getImpl(int i, int targets) {
105             return switch (i % targets) {
106                 case 0 -> new InvokeImpl1();
107                 case 1 -> new InvokeImpl2();
108                 default -> new InvokeImpl3();
109             };
110         }
111     }
112 
113     ValueInt1 a0 = ValueInt1.valueOf(42);
114 
115     @Benchmark
116     @OperationsPerInvocation(SIZE)
117     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
118     public void value_args1(Blackhole bh, StateTargets st) {
119         ValueInt1 v0 = a0;
120         InvocationLogic[] arr = st.arr;
121         for (InvocationLogic t : arr) {
122             bh.consume(t.compute(v0));
123         }
124     }
125 
126 }