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