1 /*
  2  * Copyright (c) 2020, 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 IdentityField {
 47 
 48     public static final int SIZE = 128;
 49 
 50     public interface MyInterface {
 51         public int my_method();
 52     }
 53 
 54     public static class Ref1 implements MyInterface {
 55         public final int f0;
 56         public Ref1(int f0) {
 57             this.f0 = f0;
 58         }
 59         @Override
 60         public int my_method() {
 61             return f0;
 62         }
 63     }
 64 
 65     public static class Ref2 implements MyInterface {
 66         public final int f0;
 67         public Ref2(int f0) {
 68             this.f0 = f0;
 69         }
 70         @Override
 71         public int my_method() {
 72             return f0;
 73         }
 74     }
 75 
 76     public static class Ref3 implements MyInterface {
 77         public final int f0;
 78         public Ref3(int f0) {
 79             this.f0 = f0;
 80         }
 81         @Override
 82         public int my_method() {
 83             return f0;
 84         }
 85     }
 86 
 87     public static class Ref1Wrapper {
 88         public Ref1 f;
 89 
 90         public Ref1Wrapper(Ref1 f) {
 91             this.f = f;
 92         }
 93     }
 94 
 95     public static class IntWrapper {
 96         public MyInterface f;
 97 
 98         public IntWrapper(MyInterface f) {
 99             this.f = f;
100         }
101     }
102 
103     @State(Scope.Thread)
104     public static class Ref1State {
105         public Ref1Wrapper[] arr;
106         @Setup
107         public void setup() {
108             arr = new Ref1Wrapper[SIZE];
109             for (int i = 0; i < arr.length; i++) {
110                 arr[i] = new Ref1Wrapper(new Ref1(i));
111             }
112         }
113     }
114 
115     @State(Scope.Thread)
116     public static class IntStateBase {
117         public IntWrapper[] arr;
118     }
119 
120     public static class Int1State extends IntStateBase {
121         @Setup
122         public void setup() {
123             arr = new IntWrapper[SIZE];
124             for (int i = 0; i < arr.length; i++) {
125                 arr[i] = new IntWrapper(new Ref1(i));
126             }
127         }
128     }
129 
130     public static class Int2State extends IntStateBase {
131         @Setup
132         public void setup() {
133             arr = new IntWrapper[SIZE];
134             for (int i = 0; i < arr.length; i++) {
135                 arr[i] = new IntWrapper(new Ref2(i));
136             }
137         }
138     }
139 
140     public static class Int3State 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 Ref3(i));
146             }
147         }
148     }
149 
150     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
151     public int reduceRef(Ref1Wrapper[] arr) {
152         int r = 0;
153         for (int i = 0; i < arr.length; i++) {
154             r += arr[i].f.my_method();
155         }
156         return r;
157     }
158 
159     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
160     public int reduceInt(IntWrapper[] arr) {
161         int r = 0;
162         for (int i = 0; i < arr.length; i++) {
163             r += arr[i].f.my_method();
164         }
165         return r;
166     }
167 
168     @Benchmark
169     @OperationsPerInvocation(SIZE * 6)
170     @CompilerControl(CompilerControl.Mode.INLINE)
171     public int target1_Ref(Ref1State st0, Ref1State st1, Ref1State st2, Ref1State st3, Ref1State st4, Ref1State st5) {
172         return reduceRef(st0.arr) +
173                 reduceRef(st1.arr) +
174                 reduceRef(st2.arr) +
175                 reduceRef(st3.arr) +
176                 reduceRef(st4.arr) +
177                 reduceRef(st5.arr);
178     }
179 
180     @Benchmark
181     @OperationsPerInvocation(SIZE * 6)
182     @CompilerControl(CompilerControl.Mode.INLINE)
183     public int target1_Int(Int1State st0, Int1State st1, Int1State st2, Int1State st3, Int1State st4, Int1State st5) {
184         return reduceInt(st0.arr) +
185                 reduceInt(st1.arr) +
186                 reduceInt(st2.arr) +
187                 reduceInt(st3.arr) +
188                 reduceInt(st4.arr) +
189                 reduceInt(st5.arr);
190     }
191 
192     @Benchmark
193     @OperationsPerInvocation(SIZE * 6)
194     @CompilerControl(CompilerControl.Mode.INLINE)
195     public int target2_Int(Int1State st0, Int2State st1, Int1State st2, Int2State st3, Int1State st4, Int2State st5) {
196         return reduceInt(st0.arr) +
197                 reduceInt(st1.arr) +
198                 reduceInt(st2.arr) +
199                 reduceInt(st3.arr) +
200                 reduceInt(st4.arr) +
201                 reduceInt(st5.arr);
202     }
203 
204     @Benchmark
205     @OperationsPerInvocation(SIZE * 6)
206     @CompilerControl(CompilerControl.Mode.INLINE)
207     public int target3_Int(Int1State st0, Int2State st1, Int3State st2, Int1State st3, Int2State st4, Int3State st5) {
208         return reduceInt(st0.arr) +
209                 reduceInt(st1.arr) +
210                 reduceInt(st2.arr) +
211                 reduceInt(st3.arr) +
212                 reduceInt(st4.arr) +
213                 reduceInt(st5.arr);
214     }
215 
216 
217 
218 }