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.traversal;
 24 
 25 import org.openjdk.bench.valhalla.util.SizeBase;
 26 import org.openjdk.bench.valhalla.types.A64long;
 27 import org.openjdk.bench.valhalla.types.Int64;
 28 import org.openjdk.bench.valhalla.types.R64long;
 29 import org.openjdk.bench.valhalla.util.Utils;
 30 import org.openjdk.jmh.annotations.Benchmark;
 31 import org.openjdk.jmh.annotations.CompilerControl;
 32 import org.openjdk.jmh.annotations.Setup;
 33 
 34 public class Identity64 extends SizeBase {
 35 
 36     public static abstract class IntState extends SizeState {
 37         public Int64[] arr;
 38         void fill() {
 39             int[] a = Utils.makeRandomRing(arr.length);
 40             for (int i = 0; i < a.length; i++) {
 41                 arr[i] = new R64long(a[i]);
 42             }
 43         }
 44     }
 45 
 46     public static abstract class AbsState extends SizeState {
 47         public A64long[] arr;
 48         void fill() {
 49             int[] a = Utils.makeRandomRing(arr.length);
 50             for (int i = 0; i < a.length; i++) {
 51                 arr[i] = new R64long(a[i]);
 52             }
 53         }
 54     }
 55 
 56     public static abstract class RefState extends SizeState {
 57         public R64long[] arr;
 58         void fill() {
 59             int[] a = Utils.makeRandomRing(arr.length);
 60             for (int i = 0; i < a.length; i++) {
 61                 arr[i] = new R64long(a[i]);
 62             }
 63         }
 64     }
 65 
 66 
 67     public static class Int_as_Int extends IntState {
 68         @Setup
 69         public void setup() {
 70             arr = new Int64[size];
 71             fill();
 72         }
 73     }
 74 
 75     public static class Abs_as_Int extends IntState {
 76         @Setup
 77         public void setup() {
 78             arr = new A64long[size];
 79             fill();
 80         }
 81     }
 82 
 83     public static class Ref_as_Int extends IntState {
 84         @Setup
 85         public void setup() {
 86             arr = new R64long[size];
 87             fill();
 88         }
 89     }
 90 
 91     public static class Abs_as_Abs extends AbsState {
 92         @Setup
 93         public void setup() {
 94             arr = new A64long[size];
 95             fill();
 96         }
 97     }
 98 
 99     public static class Ref_as_Abs extends AbsState {
100         @Setup
101         public void setup() {
102             arr = new R64long[size];
103             fill();
104         }
105     }
106 
107     public static class Ref_as_Ref extends RefState {
108         @Setup
109         public void setup() {
110             arr = new R64long[size];
111             fill();
112         }
113     }
114 
115     @Benchmark
116     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
117     public int Int_as_Int_walk(Int_as_Int s) {
118         int steps = 0;
119         Int64[] values = s.arr;
120         for (int i = values[0].intValue(); i != 0; i = values[i].intValue()) steps++;
121         return steps;
122     }
123 
124     @Benchmark
125     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
126     public int Abs_as_Int_walk(Abs_as_Int s) {
127         int steps = 0;
128         Int64[] values = s.arr;
129         for (int i = values[0].intValue(); i != 0; i = values[i].intValue()) steps++;
130         return steps;
131     }
132 
133     @Benchmark
134     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
135     public int Ref_as_Int_walk(Ref_as_Int s) {
136         int steps = 0;
137         Int64[] values = s.arr;
138         for (int i = values[0].intValue(); i != 0; i = values[i].intValue()) steps++;
139         return steps;
140     }
141 
142     @Benchmark
143     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
144     public int Abs_as_Abs_walk(Abs_as_Abs s) {
145         int steps = 0;
146         A64long[] values = s.arr;
147         for (int i = values[0].intValue(); i != 0; i = values[i].intValue()) steps++;
148         return steps;
149     }
150 
151     @Benchmark
152     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
153     public int Ref_as_Abs_walk(Ref_as_Abs s) {
154         int steps = 0;
155         A64long[] values = s.arr;
156         for (int i = values[0].intValue(); i != 0; i = values[i].intValue()) steps++;
157         return steps;
158     }
159 
160     @Benchmark
161     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
162     public int Ref_as_Ref_walk(Ref_as_Ref s) {
163         int steps = 0;
164         A64long[] values = s.arr;
165         for (int i = values[0].intValue(); i != 0; i = values[i].intValue()) steps++;
166         return steps;
167     }
168 
169 }