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.acmp.array;
 24 
 25 import org.openjdk.bench.valhalla.types.R64long;
 26 import org.openjdk.jmh.annotations.Scope;
 27 import org.openjdk.jmh.annotations.Setup;
 28 import org.openjdk.jmh.annotations.State;
 29 
 30 import java.util.BitSet;
 31 import java.util.Random;
 32 
 33 public class StatesR64long {
 34 
 35     public static final int SIZE = 100;
 36 
 37     private static BitSet indices(Random rnd, int bound, int size) {
 38         return rnd.ints(0, bound)
 39                 .distinct()
 40                 .limit(size)
 41                 .collect(BitSet::new, BitSet::set, BitSet::or);
 42     }
 43 
 44     private static void populate(Object[] arr1, Object[] arr2, int eq) {
 45         BitSet eqset = (eq > 0 && eq < 100) ? indices(new Random(42), SIZE, (eq * SIZE) / 100) : null;
 46         int samenulls = 0;
 47         int distinctnulls = 0;
 48         for (int i = 0; i < SIZE; i++) {
 49             if (eq > 0 && (eq >= 100 || eqset.get(i))) {
 50                 if (samenulls > 0) {
 51                     arr2[i] = arr1[i] = new R64long(i);
 52                 } else {
 53                     arr2[i] = arr1[i] = null;
 54                     samenulls++;
 55                 }
 56             } else {
 57                 if (distinctnulls > 1) {
 58                     arr1[i] = new R64long(2 * i);
 59                     arr2[i] = new R64long(2 * i + 1);
 60                 } else {
 61                     if (distinctnulls == 0) {
 62                         arr1[i] = null;
 63                         arr2[i] = new R64long(2 * i + 1);
 64                     } else {
 65                         arr1[i] = new R64long(2 * i);
 66                         arr2[i] = null;
 67                     }
 68                     distinctnulls++;
 69                 }
 70             }
 71         }
 72     }
 73 
 74     @State(Scope.Thread)
 75     public abstract static class ObjState {
 76         Object[] arr1, arr2;
 77     }
 78 
 79     @State(Scope.Thread)
 80     public abstract static class RefState {
 81         R64long[] arr1, arr2;
 82     }
 83 
 84     @State(Scope.Thread)
 85     public abstract static class IdState {
 86         IdentityObject[] arr1, arr2;
 87     }
 88 
 89     public static class ObjState00 extends ObjState {
 90         @Setup
 91         public void setup() {
 92             arr1 = new Object[SIZE];
 93             arr2 = new Object[SIZE];
 94             populate(arr1, arr2, 0);
 95         }
 96 
 97     }
 98 
 99     public static class ObjState25 extends ObjState {
100         @Setup
101         public void setup() {
102             arr1 = new Object[SIZE];
103             arr2 = new Object[SIZE];
104             populate(arr1, arr2, 25);
105         }
106 
107     }
108 
109     public static class ObjState50 extends ObjState {
110         @Setup
111         public void setup() {
112             arr1 = new Object[SIZE];
113             arr2 = new Object[SIZE];
114             populate(arr1, arr2, 50);
115         }
116 
117     }
118 
119     public static class ObjState75 extends ObjState {
120         @Setup
121         public void setup() {
122             arr1 = new Object[SIZE];
123             arr2 = new Object[SIZE];
124             populate(arr1, arr2, 75);
125         }
126 
127     }
128 
129     public static class ObjState100 extends ObjState {
130         @Setup
131         public void setup() {
132             arr1 = new Object[SIZE];
133             arr2 = new Object[SIZE];
134             populate(arr1, arr2, 100);
135         }
136     }
137 
138     public static class RefState00 extends RefState {
139         @Setup
140         public void setup() {
141             arr1 = new R64long[SIZE];
142             arr2 = new R64long[SIZE];
143             populate(arr1, arr2, 0);
144         }
145     }
146 
147     public static class RefState25 extends RefState {
148         @Setup
149         public void setup() {
150             arr1 = new R64long[SIZE];
151             arr2 = new R64long[SIZE];
152             populate(arr1, arr2, 25);
153         }
154     }
155 
156     public static class RefState50 extends RefState {
157         @Setup
158         public void setup() {
159             arr1 = new R64long[SIZE];
160             arr2 = new R64long[SIZE];
161             populate(arr1, arr2, 50);
162         }
163     }
164 
165     public static class RefState75 extends RefState {
166         @Setup
167         public void setup() {
168             arr1 = new R64long[SIZE];
169             arr2 = new R64long[SIZE];
170             populate(arr1, arr2, 75);
171         }
172     }
173 
174     public static class RefState100 extends RefState {
175         @Setup
176         public void setup() {
177             arr1 = new R64long[SIZE];
178             arr2 = new R64long[SIZE];
179             populate(arr1, arr2, 100);
180         }
181     }
182 
183     public static class IdState00 extends IdState {
184         @Setup
185         public void setup() {
186             arr1 = new IdentityObject[SIZE];
187             arr2 = new IdentityObject[SIZE];
188             populate(arr1, arr2, 0);
189         }
190     }
191 
192     public static class IdState25 extends IdState {
193         @Setup
194         public void setup() {
195             arr1 = new IdentityObject[SIZE];
196             arr2 = new IdentityObject[SIZE];
197             populate(arr1, arr2, 25);
198         }
199     }
200 
201     public static class IdState50 extends IdState {
202         @Setup
203         public void setup() {
204             arr1 = new IdentityObject[SIZE];
205             arr2 = new IdentityObject[SIZE];
206             populate(arr1, arr2, 50);
207         }
208     }
209 
210     public static class IdState75 extends IdState {
211         @Setup
212         public void setup() {
213             arr1 = new IdentityObject[SIZE];
214             arr2 = new IdentityObject[SIZE];
215             populate(arr1, arr2, 75);
216         }
217     }
218 
219     public static class IdState100 extends IdState {
220         @Setup
221         public void setup() {
222             arr1 = new IdentityObject[SIZE];
223             arr2 = new IdentityObject[SIZE];
224             populate(arr1, arr2, 100);
225         }
226     }
227 
228 }