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 InlineArray0 {
 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     @State(Scope.Thread)
 88     public static abstract class IntState {
 89         public MyInterface[] arr;
 90     }
 91 
 92     @State(Scope.Thread)
 93     public static abstract class Ref1State {
 94         public Val1[] arr;
 95     }
 96 
 97     @State(Scope.Thread)
 98     public static abstract class Val1State {
 99         public Val1[] arr;
100     }
101 
102     public static class Val1_as_Val extends Val1State {
103         @Setup
104         public void setup() {
105             arr = new Val1[SIZE];
106             for (int i = 0; i < arr.length; i++) {
107                 arr[i] = new Val1(i);
108             }
109         }
110     }
111 
112     public static class Val1_as_Ref extends Ref1State {
113         @Setup
114         public void setup() {
115             arr = new Val1[SIZE];
116             for (int i = 0; i < arr.length; i++) {
117                 arr[i] = new Val1(i);
118             }
119         }
120     }
121 
122     public static class Val1_as_Int extends IntState {
123         @Setup
124         public void setup() {
125             arr = new Val1[SIZE];
126             for (int i = 0; i < arr.length; i++) {
127                 arr[i] = new Val1(i);
128             }
129         }
130     }
131 
132     public static class Ref1_as_Ref extends Ref1State {
133         @Setup
134         public void setup() {
135             arr = new Val1[SIZE];
136             for (int i = 0; i < arr.length; i++) {
137                 arr[i] = new Val1(i);
138             }
139         }
140     }
141 
142     public static class Ref1_as_Int extends IntState {
143         @Setup
144         public void setup() {
145             arr = new Val1[SIZE];
146             for (int i = 0; i < arr.length; i++) {
147                 arr[i] = new Val1(i);
148             }
149         }
150     }
151 
152     public static class Int1_as_Int extends IntState {
153         @Setup
154         public void setup() {
155             arr = new MyInterface[SIZE];
156             for (int i = 0; i < arr.length; i++) {
157                 arr[i] = new Val1(i);
158             }
159         }
160     }
161 
162     public static class Val2_as_Int extends IntState {
163         @Setup
164         public void setup() {
165             arr = new Val2[SIZE];
166             for (int i = 0; i < arr.length; i++) {
167                 arr[i] = new Val2(i);
168             }
169         }
170     }
171 
172     public static class Ref2_as_Int extends IntState {
173         @Setup
174         public void setup() {
175             arr = new Val2[SIZE];
176             for (int i = 0; i < arr.length; i++) {
177                 arr[i] = new Val2(i);
178             }
179         }
180     }
181 
182     public static class Int2_as_Int extends IntState {
183         @Setup
184         public void setup() {
185             arr = new MyInterface[SIZE];
186             for (int i = 0; i < arr.length; i++) {
187                 arr[i] = new Val2(i);
188             }
189         }
190     }
191 
192     public static class Val3_as_Int extends IntState {
193         @Setup
194         public void setup() {
195             arr = new Val3[SIZE];
196             for (int i = 0; i < arr.length; i++) {
197                 arr[i] = new Val3(i);
198             }
199         }
200     }
201 
202     public static class Ref3_as_Int extends IntState {
203         @Setup
204         public void setup() {
205             arr = new Val3[SIZE];
206             for (int i = 0; i < arr.length; i++) {
207                 arr[i] = new Val3(i);
208             }
209         }
210     }
211 
212     public static class Int3_as_Int extends IntState {
213         @Setup
214         public void setup() {
215             arr = new MyInterface[SIZE];
216             for (int i = 0; i < arr.length; i++) {
217                 arr[i] = new Val3(i);
218             }
219         }
220     }
221 
222 
223     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
224     public int reduceInt(MyInterface[] arr) {
225         int r = 0;
226         for (int i = 0; i < arr.length; i++) {
227             r += arr[i].my_method();
228         }
229         return r;
230     }
231 
232     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
233     public int reduceRef(Val1[] arr) {
234         int r = 0;
235         for (int i = 0; i < arr.length; i++) {
236             r += arr[i].my_method();
237         }
238         return r;
239     }
240 
241     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
242     public int reduceVal(Val1[] arr) {
243         int r = 0;
244         for (int i = 0; i < arr.length; i++) {
245             r += arr[i].my_method();
246         }
247         return r;
248     }
249 
250     @Benchmark
251     @OperationsPerInvocation(SIZE * 6)
252     @CompilerControl(CompilerControl.Mode.INLINE)
253     public int target1_Val_v(Val1_as_Val st0, Val1_as_Val st1, Val1_as_Val st2, Val1_as_Val st3, Val1_as_Val st4, Val1_as_Val st5) {
254         return reduceVal(st0.arr) +
255                reduceVal(st1.arr) +
256                reduceVal(st2.arr) +
257                reduceVal(st3.arr) +
258                reduceVal(st4.arr) +
259                reduceVal(st5.arr);
260     }
261 
262     @Benchmark
263     @OperationsPerInvocation(SIZE * 6)
264     @CompilerControl(CompilerControl.Mode.INLINE)
265     public int target1_Ref_v(Val1_as_Ref st0, Val1_as_Ref st1, Val1_as_Ref st2, Val1_as_Ref st3, Val1_as_Ref st4, Val1_as_Ref st5) {
266         return reduceRef(st0.arr) +
267                reduceRef(st1.arr) +
268                reduceRef(st2.arr) +
269                reduceRef(st3.arr) +
270                reduceRef(st4.arr) +
271                reduceRef(st5.arr);
272     }
273 
274     @Benchmark
275     @OperationsPerInvocation(SIZE * 6)
276     @CompilerControl(CompilerControl.Mode.INLINE)
277     public int target1_Ref_r(Ref1_as_Ref st0, Ref1_as_Ref st1, Ref1_as_Ref st2, Ref1_as_Ref st3, Ref1_as_Ref st4, Ref1_as_Ref st5) {
278         return reduceRef(st0.arr) +
279                reduceRef(st1.arr) +
280                reduceRef(st2.arr) +
281                reduceRef(st3.arr) +
282                reduceRef(st4.arr) +
283                reduceRef(st5.arr);
284     }
285 
286     @Benchmark
287     @OperationsPerInvocation(SIZE * 6)
288     @CompilerControl(CompilerControl.Mode.INLINE)
289     public int target1_Ref_vr(Val1_as_Ref st0, Ref1_as_Ref st1, Val1_as_Ref st2, Ref1_as_Ref st3, Val1_as_Ref st4, Ref1_as_Ref st5) {
290         return reduceRef(st0.arr) +
291                reduceRef(st1.arr) +
292                reduceRef(st2.arr) +
293                reduceRef(st3.arr) +
294                reduceRef(st4.arr) +
295                reduceRef(st5.arr);
296     }
297 
298     @Benchmark
299     @OperationsPerInvocation(SIZE * 6)
300     @CompilerControl(CompilerControl.Mode.INLINE)
301     public int target1_Int_v(Val1_as_Int st0, Val1_as_Int st1, Val1_as_Int st2, Val1_as_Int st3, Val1_as_Int st4, Val1_as_Int st5) {
302         return reduceInt(st0.arr) +
303                reduceInt(st1.arr) +
304                reduceInt(st2.arr) +
305                reduceInt(st3.arr) +
306                reduceInt(st4.arr) +
307                reduceInt(st5.arr);
308     }
309 
310     @Benchmark
311     @OperationsPerInvocation(SIZE * 6)
312     @CompilerControl(CompilerControl.Mode.INLINE)
313     public int target1_Int_r(Ref1_as_Int st0, Ref1_as_Int st1, Ref1_as_Int st2, Ref1_as_Int st3, Ref1_as_Int st4, Ref1_as_Int st5) {
314         return reduceInt(st0.arr) +
315                reduceInt(st1.arr) +
316                reduceInt(st2.arr) +
317                reduceInt(st3.arr) +
318                reduceInt(st4.arr) +
319                reduceInt(st5.arr);
320     }
321 
322     @Benchmark
323     @OperationsPerInvocation(SIZE * 6)
324     @CompilerControl(CompilerControl.Mode.INLINE)
325     public int target1_Int_i(Int1_as_Int st0, Int1_as_Int st1, Int1_as_Int st2, Int1_as_Int st3, Int1_as_Int st4, Int1_as_Int st5) {
326         return reduceInt(st0.arr) +
327                reduceInt(st1.arr) +
328                reduceInt(st2.arr) +
329                reduceInt(st3.arr) +
330                reduceInt(st4.arr) +
331                reduceInt(st5.arr);
332     }
333 
334     @Benchmark
335     @OperationsPerInvocation(SIZE * 6)
336     @CompilerControl(CompilerControl.Mode.INLINE)
337     public int target1_Int_vr(Val1_as_Int st0, Ref1_as_Int st1, Val1_as_Int st2, Ref1_as_Int st3, Val1_as_Int st4, Ref1_as_Int st5) {
338         return reduceInt(st0.arr) +
339                 reduceInt(st1.arr) +
340                 reduceInt(st2.arr) +
341                 reduceInt(st3.arr) +
342                 reduceInt(st4.arr) +
343                 reduceInt(st5.arr);
344     }
345 
346     @Benchmark
347     @OperationsPerInvocation(SIZE * 6)
348     @CompilerControl(CompilerControl.Mode.INLINE)
349     public int target1_Int_vi(Val1_as_Int st0, Int1_as_Int st1, Val1_as_Int st2, Int1_as_Int st3, Val1_as_Int st4, Int1_as_Int st5) {
350         return reduceInt(st0.arr) +
351                 reduceInt(st1.arr) +
352                 reduceInt(st2.arr) +
353                 reduceInt(st3.arr) +
354                 reduceInt(st4.arr) +
355                 reduceInt(st5.arr);
356     }
357 
358     @Benchmark
359     @OperationsPerInvocation(SIZE * 6)
360     @CompilerControl(CompilerControl.Mode.INLINE)
361     public int target1_Int_ri(Ref1_as_Int st0, Int1_as_Int st1, Ref1_as_Int st2, Int1_as_Int st3, Ref1_as_Int st4, Int1_as_Int st5) {
362         return reduceInt(st0.arr) +
363                 reduceInt(st1.arr) +
364                 reduceInt(st2.arr) +
365                 reduceInt(st3.arr) +
366                 reduceInt(st4.arr) +
367                 reduceInt(st5.arr);
368     }
369 
370     @Benchmark
371     @OperationsPerInvocation(SIZE * 6)
372     @CompilerControl(CompilerControl.Mode.INLINE)
373     public int target1_Int_vri(Val1_as_Int st0, Ref1_as_Int st1, Int1_as_Int st2, Val1_as_Int st3, Ref1_as_Int st4, Int1_as_Int st5) {
374         return reduceInt(st0.arr) +
375                 reduceInt(st1.arr) +
376                 reduceInt(st2.arr) +
377                 reduceInt(st3.arr) +
378                 reduceInt(st4.arr) +
379                 reduceInt(st5.arr);
380     }
381 
382     @Benchmark
383     @OperationsPerInvocation(SIZE * 6)
384     @CompilerControl(CompilerControl.Mode.INLINE)
385     public int target2_Int_v(Val1_as_Int st0, Val2_as_Int st1, Val1_as_Int st2, Val2_as_Int st3, Val1_as_Int st4, Val2_as_Int st5) {
386         return reduceInt(st0.arr) +
387                 reduceInt(st1.arr) +
388                 reduceInt(st2.arr) +
389                 reduceInt(st3.arr) +
390                 reduceInt(st4.arr) +
391                 reduceInt(st5.arr);
392     }
393 
394     @Benchmark
395     @OperationsPerInvocation(SIZE * 6)
396     @CompilerControl(CompilerControl.Mode.INLINE)
397     public int target2_Int_r(Ref1_as_Int st0, Ref2_as_Int st1, Ref1_as_Int st2, Ref2_as_Int st3, Ref1_as_Int st4, Ref2_as_Int st5) {
398         return reduceInt(st0.arr) +
399                 reduceInt(st1.arr) +
400                 reduceInt(st2.arr) +
401                 reduceInt(st3.arr) +
402                 reduceInt(st4.arr) +
403                 reduceInt(st5.arr);
404     }
405 
406     @Benchmark
407     @OperationsPerInvocation(SIZE * 6)
408     @CompilerControl(CompilerControl.Mode.INLINE)
409     public int target2_Int_i(Int1_as_Int st0, Int2_as_Int st1, Int1_as_Int st2, Int2_as_Int st3, Int1_as_Int st4, Int2_as_Int st5) {
410         return reduceInt(st0.arr) +
411                 reduceInt(st1.arr) +
412                 reduceInt(st2.arr) +
413                 reduceInt(st3.arr) +
414                 reduceInt(st4.arr) +
415                 reduceInt(st5.arr);
416     }
417 
418     @Benchmark
419     @OperationsPerInvocation(SIZE * 6)
420     @CompilerControl(CompilerControl.Mode.INLINE)
421     public int target2_Int_vr(Val1_as_Int st0, Ref2_as_Int st1, Val2_as_Int st2, Ref1_as_Int st3, Val1_as_Int st4, Ref2_as_Int st5) {
422         return reduceInt(st0.arr) +
423                 reduceInt(st1.arr) +
424                 reduceInt(st2.arr) +
425                 reduceInt(st3.arr) +
426                 reduceInt(st4.arr) +
427                 reduceInt(st5.arr);
428     }
429 
430     @Benchmark
431     @OperationsPerInvocation(SIZE * 6)
432     @CompilerControl(CompilerControl.Mode.INLINE)
433     public int target2_Int_vi(Val1_as_Int st0, Int2_as_Int st1, Val2_as_Int st2, Int1_as_Int st3, Val1_as_Int st4, Int2_as_Int st5) {
434         return reduceInt(st0.arr) +
435                 reduceInt(st1.arr) +
436                 reduceInt(st2.arr) +
437                 reduceInt(st3.arr) +
438                 reduceInt(st4.arr) +
439                 reduceInt(st5.arr);
440     }
441 
442     @Benchmark
443     @OperationsPerInvocation(SIZE * 6)
444     @CompilerControl(CompilerControl.Mode.INLINE)
445     public int target2_Int_ri(Ref1_as_Int st0, Int1_as_Int st1, Ref2_as_Int st2, Int1_as_Int st3, Ref1_as_Int st4, Int2_as_Int st5) {
446         return reduceInt(st0.arr) +
447                 reduceInt(st1.arr) +
448                 reduceInt(st2.arr) +
449                 reduceInt(st3.arr) +
450                 reduceInt(st4.arr) +
451                 reduceInt(st5.arr);
452     }
453 
454     @Benchmark
455     @OperationsPerInvocation(SIZE * 6)
456     @CompilerControl(CompilerControl.Mode.INLINE)
457     public int target2_Int_vri(Val1_as_Int st0, Ref1_as_Int st1, Int1_as_Int st2, Val2_as_Int st3, Ref2_as_Int st4, Int2_as_Int st5) {
458         return reduceInt(st0.arr) +
459                 reduceInt(st1.arr) +
460                 reduceInt(st2.arr) +
461                 reduceInt(st3.arr) +
462                 reduceInt(st4.arr) +
463                 reduceInt(st5.arr);
464     }
465 
466 
467     @Benchmark
468     @OperationsPerInvocation(SIZE * 6)
469     @CompilerControl(CompilerControl.Mode.INLINE)
470     public int target3_Int_v(Val1_as_Int st0, Val2_as_Int st1, Val3_as_Int st2, Val1_as_Int st3, Val2_as_Int st4, Val3_as_Int st5) {
471         return reduceInt(st0.arr) +
472                 reduceInt(st1.arr) +
473                 reduceInt(st2.arr) +
474                 reduceInt(st3.arr) +
475                 reduceInt(st4.arr) +
476                 reduceInt(st5.arr);
477     }
478 
479     @Benchmark
480     @OperationsPerInvocation(SIZE * 6)
481     @CompilerControl(CompilerControl.Mode.INLINE)
482     public int target3_Int_r(Ref1_as_Int st0, Ref2_as_Int st1, Ref3_as_Int st2, Ref1_as_Int st3, Ref2_as_Int st4, Ref3_as_Int st5) {
483         return reduceInt(st0.arr) +
484                 reduceInt(st1.arr) +
485                 reduceInt(st2.arr) +
486                 reduceInt(st3.arr) +
487                 reduceInt(st4.arr) +
488                 reduceInt(st5.arr);
489     }
490 
491     @Benchmark
492     @OperationsPerInvocation(SIZE * 6)
493     @CompilerControl(CompilerControl.Mode.INLINE)
494     public int target3_Int_i(Int1_as_Int st0, Int2_as_Int st1, Int3_as_Int st2, Int1_as_Int st3, Int2_as_Int st4, Int3_as_Int st5) {
495         return reduceInt(st0.arr) +
496                 reduceInt(st1.arr) +
497                 reduceInt(st2.arr) +
498                 reduceInt(st3.arr) +
499                 reduceInt(st4.arr) +
500                 reduceInt(st5.arr);
501     }
502 
503     @Benchmark
504     @OperationsPerInvocation(SIZE * 6)
505     @CompilerControl(CompilerControl.Mode.INLINE)
506     public int target3_Int_vr(Val1_as_Int st0, Ref2_as_Int st1, Val3_as_Int st2, Ref1_as_Int st3, Val2_as_Int st4, Ref3_as_Int st5) {
507         return reduceInt(st0.arr) +
508                 reduceInt(st1.arr) +
509                 reduceInt(st2.arr) +
510                 reduceInt(st3.arr) +
511                 reduceInt(st4.arr) +
512                 reduceInt(st5.arr);
513     }
514 
515     @Benchmark
516     @OperationsPerInvocation(SIZE * 6)
517     @CompilerControl(CompilerControl.Mode.INLINE)
518     public int target3_Int_vi(Val1_as_Int st0, Int2_as_Int st1, Val3_as_Int st2, Int1_as_Int st3, Val3_as_Int st4, Int3_as_Int st5) {
519         return reduceInt(st0.arr) +
520                 reduceInt(st1.arr) +
521                 reduceInt(st2.arr) +
522                 reduceInt(st3.arr) +
523                 reduceInt(st4.arr) +
524                 reduceInt(st5.arr);
525     }
526 
527     @Benchmark
528     @OperationsPerInvocation(SIZE * 6)
529     @CompilerControl(CompilerControl.Mode.INLINE)
530     public int target3_Int_ri(Ref1_as_Int st0, Int2_as_Int st1, Ref3_as_Int st2, Int1_as_Int st3, Ref2_as_Int st4, Int3_as_Int st5) {
531         return reduceInt(st0.arr) +
532                 reduceInt(st1.arr) +
533                 reduceInt(st2.arr) +
534                 reduceInt(st3.arr) +
535                 reduceInt(st4.arr) +
536                 reduceInt(st5.arr);
537     }
538 
539     @Benchmark
540     @OperationsPerInvocation(SIZE * 6)
541     @CompilerControl(CompilerControl.Mode.INLINE)
542     public int target3_Int_vri(Val1_as_Int st0, Ref2_as_Int st1, Int3_as_Int st2, Val2_as_Int st3, Ref3_as_Int st4, Int1_as_Int st5) {
543         return reduceInt(st0.arr) +
544                 reduceInt(st1.arr) +
545                 reduceInt(st2.arr) +
546                 reduceInt(st3.arr) +
547                 reduceInt(st4.arr) +
548                 reduceInt(st5.arr);
549     }
550 
551 
552 }