1 /*
  2  * Copyright (c) 2020, 2024, 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.invoke;
 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 
 38 import java.util.concurrent.TimeUnit;
 39 
 40 @Fork(3)
 41 @Warmup(iterations = 5, time = 1)
 42 @Measurement(iterations = 5, time = 1)
 43 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 44 @BenchmarkMode(Mode.AverageTime)
 45 @State(Scope.Thread)
 46 public class InlineField1 {
 47 
 48     public static final int SIZE = 128;
 49 
 50     public interface MyInterface {
 51         public int my_method();
 52     }
 53 
 54     public static value class Val1 implements MyInterface {
 55         public final int f0;
 56         public Val1(int f0) {
 57             this.f0 = f0;
 58         }
 59         @Override
 60         @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 61         public int my_method() {
 62             return f0;
 63         }
 64     }
 65 
 66     public static value class Val2 implements MyInterface {
 67         public final int f0;
 68         public Val2(int f0) {
 69             this.f0 = f0;
 70         }
 71         @Override
 72         @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 73         public int my_method() {
 74             return f0;
 75         }
 76     }
 77 
 78     public static value class Val3 implements MyInterface {
 79         public final int f0;
 80         public Val3(int f0) {
 81             this.f0 = f0;
 82         }
 83         @Override
 84         @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 85         public int my_method() {
 86             return f0;
 87         }
 88     }
 89 
 90     public static class Val1Wrapper {
 91         public Val1 f;
 92 
 93         public Val1Wrapper(Val1 f) {
 94             this.f = f;
 95         }
 96     }
 97 
 98     public static class Ref1Wrapper {
 99         public Val1 f;
100 
101         public Ref1Wrapper(Val1 f) {
102             this.f = f;
103         }
104     }
105 
106     public static class IntWrapper {
107         public MyInterface f;
108 
109         public IntWrapper(MyInterface f) {
110             this.f = f;
111         }
112     }
113 
114     @State(Scope.Thread)
115     public static class Val1State {
116         public Val1Wrapper[] arr;
117         @Setup
118         public void setup() {
119             arr = new Val1Wrapper[SIZE];
120             for (int i = 0; i < arr.length; i++) {
121                 arr[i] = new Val1Wrapper(new Val1(i));
122             }
123         }
124     }
125 
126     @State(Scope.Thread)
127     public static class Ref1State {
128         public Ref1Wrapper[] arr;
129         @Setup
130         public void setup() {
131             arr = new Ref1Wrapper[SIZE];
132             for (int i = 0; i < arr.length; i++) {
133                 arr[i] = new Ref1Wrapper(new Val1(i));
134             }
135         }
136     }
137 
138     @State(Scope.Thread)
139     public static class IntStateBase {
140         public IntWrapper[] arr;
141     }
142 
143     public static class Int1State extends IntStateBase {
144         @Setup
145         public void setup() {
146             arr = new IntWrapper[SIZE];
147             for (int i = 0; i < arr.length; i++) {
148                 arr[i] = new IntWrapper(new Val1(i));
149             }
150         }
151     }
152 
153     public static class Int2State extends IntStateBase {
154         @Setup
155         public void setup() {
156             arr = new IntWrapper[SIZE];
157             for (int i = 0; i < arr.length; i++) {
158                 arr[i] = new IntWrapper(new Val2(i));
159             }
160         }
161     }
162 
163     public static class Int3State extends IntStateBase {
164         @Setup
165         public void setup() {
166             arr = new IntWrapper[SIZE];
167             for (int i = 0; i < arr.length; i++) {
168                 arr[i] = new IntWrapper(new Val3(i));
169             }
170         }
171     }
172 
173     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
174     public int reduceVal(Val1Wrapper[] arr) {
175         int r = 0;
176         for (int i = 0; i < arr.length; i++) {
177             r += arr[i].f.my_method();
178         }
179         return r;
180     }
181 
182     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
183     public int reduceRef(Ref1Wrapper[] arr) {
184         int r = 0;
185         for (int i = 0; i < arr.length; i++) {
186             r += arr[i].f.my_method();
187         }
188         return r;
189     }
190 
191     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
192     public int reduceInt(IntWrapper[] arr) {
193         int r = 0;
194         for (int i = 0; i < arr.length; i++) {
195             r += arr[i].f.my_method();
196         }
197         return r;
198     }
199 
200     @Benchmark
201     @OperationsPerInvocation(SIZE * 6)
202     @CompilerControl(CompilerControl.Mode.INLINE)
203     public int target1_Val(Val1State st0, Val1State st1, Val1State st2, Val1State st3, Val1State st4, Val1State st5) {
204         return reduceVal(st0.arr) +
205                 reduceVal(st1.arr) +
206                 reduceVal(st2.arr) +
207                 reduceVal(st3.arr) +
208                 reduceVal(st4.arr) +
209                 reduceVal(st5.arr);
210     }
211 
212     @Benchmark
213     @OperationsPerInvocation(SIZE * 6)
214     @CompilerControl(CompilerControl.Mode.INLINE)
215     public int target1_Ref(Ref1State st0, Ref1State st1, Ref1State st2, Ref1State st3, Ref1State st4, Ref1State st5) {
216         return reduceRef(st0.arr) +
217                 reduceRef(st1.arr) +
218                 reduceRef(st2.arr) +
219                 reduceRef(st3.arr) +
220                 reduceRef(st4.arr) +
221                 reduceRef(st5.arr);
222     }
223 
224     @Benchmark
225     @OperationsPerInvocation(SIZE * 6)
226     @CompilerControl(CompilerControl.Mode.INLINE)
227     public int target1_Int(Int1State st0, Int1State st1, Int1State st2, Int1State st3, Int1State st4, Int1State st5) {
228         return reduceInt(st0.arr) +
229                 reduceInt(st1.arr) +
230                 reduceInt(st2.arr) +
231                 reduceInt(st3.arr) +
232                 reduceInt(st4.arr) +
233                 reduceInt(st5.arr);
234     }
235 
236     @Benchmark
237     @OperationsPerInvocation(SIZE * 6)
238     @CompilerControl(CompilerControl.Mode.INLINE)
239     public int target2_Int(Int1State st0, Int2State st1, Int1State st2, Int2State st3, Int1State st4, Int2State st5) {
240         return reduceInt(st0.arr) +
241                 reduceInt(st1.arr) +
242                 reduceInt(st2.arr) +
243                 reduceInt(st3.arr) +
244                 reduceInt(st4.arr) +
245                 reduceInt(st5.arr);
246     }
247 
248     @Benchmark
249     @OperationsPerInvocation(SIZE * 6)
250     @CompilerControl(CompilerControl.Mode.INLINE)
251     public int target3_Int(Int1State st0, Int2State st1, Int3State st2, Int1State st3, Int2State st4, Int3State st5) {
252         return reduceInt(st0.arr) +
253                 reduceInt(st1.arr) +
254                 reduceInt(st2.arr) +
255                 reduceInt(st3.arr) +
256                 reduceInt(st4.arr) +
257                 reduceInt(st5.arr);
258     }
259 
260 
261 
262 }