1 /*
  2  * Copyright (c) 2025, 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 jdk.internal.value.ValueClass;
 26 import org.openjdk.jmh.annotations.Benchmark;
 27 import org.openjdk.jmh.annotations.BenchmarkMode;
 28 import org.openjdk.jmh.annotations.CompilerControl;
 29 import org.openjdk.jmh.annotations.Fork;
 30 import org.openjdk.jmh.annotations.Measurement;
 31 import org.openjdk.jmh.annotations.Mode;
 32 import org.openjdk.jmh.annotations.OperationsPerInvocation;
 33 import org.openjdk.jmh.annotations.OutputTimeUnit;
 34 import org.openjdk.jmh.annotations.Scope;
 35 import org.openjdk.jmh.annotations.Setup;
 36 import org.openjdk.jmh.annotations.State;
 37 import org.openjdk.jmh.annotations.Warmup;
 38 
 39 import java.util.BitSet;
 40 import java.util.Random;
 41 import java.util.concurrent.TimeUnit;
 42 
 43 /*
 44  *  For proper results it should be executed:
 45  *  java -jar target/benchmarks.jar org.openjdk.bench.valhalla.acmp.array.Value  -wmb "org.openjdk.bench.valhalla.acmp.array.Value.*050"
 46  */
 47 
 48 @Fork(value = 3, jvmArgsAppend = {"--enable-preview", "--add-exports", "java.base/jdk.internal.value=ALL-UNNAMED"})
 49 @Warmup(iterations = 3, time = 1)
 50 @Measurement(iterations = 5, time = 1)
 51 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 52 @BenchmarkMode(Mode.AverageTime)
 53 @State(Scope.Thread)
 54 public class Value128NullFree {
 55 
 56     public static final int SIZE = 100;
 57 
 58     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 59     private static int cmp_branch_val(ValueInt4[] objects1, ValueInt4[] objects2) {
 60         int s = 0;
 61         for (int i = 0; i < SIZE; i++) {
 62             if (objects1[i] == objects2[i]) {
 63                 s += 1;
 64             } else {
 65                 s -= 1;
 66             }
 67         }
 68         return s;
 69     }
 70 
 71     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 72     private static boolean cmp_result_val(ValueInt4[] objects1, ValueInt4[] objects2) {
 73         boolean s = false;
 74         for (int i = 0; i < SIZE; i++) {
 75             s ^= objects1[i] == objects2[i];
 76         }
 77         return s;
 78     }
 79 
 80     @Benchmark
 81     @OperationsPerInvocation(SIZE)
 82     @CompilerControl(CompilerControl.Mode.INLINE)
 83     public int branch_val_equals000(ValState00 st) {
 84         return cmp_branch_val(st.arr1, st.arr2);
 85     }
 86 
 87     @Benchmark
 88     @OperationsPerInvocation(SIZE)
 89     @CompilerControl(CompilerControl.Mode.INLINE)
 90     public int branch_val_equals025(ValState25 st) {
 91         return cmp_branch_val(st.arr1, st.arr2);
 92     }
 93 
 94     @Benchmark
 95     @OperationsPerInvocation(SIZE)
 96     @CompilerControl(CompilerControl.Mode.INLINE)
 97     public int branch_val_equals050(ValState50 st) {
 98         return cmp_branch_val(st.arr1, st.arr2);
 99     }
100 
101     @Benchmark
102     @OperationsPerInvocation(SIZE)
103     @CompilerControl(CompilerControl.Mode.INLINE)
104     public int branch_val_equals075(ValState75 st) {
105         return cmp_branch_val(st.arr1, st.arr2);
106     }
107 
108     @Benchmark
109     @OperationsPerInvocation(SIZE)
110     @CompilerControl(CompilerControl.Mode.INLINE)
111     public int branch_val_equals100(ValState100 st) {
112         return cmp_branch_val(st.arr1, st.arr2);
113     }
114 
115     @Benchmark
116     @OperationsPerInvocation(SIZE)
117     @CompilerControl(CompilerControl.Mode.INLINE)
118     public boolean result_val_equals000(ValState00 st) {
119         return cmp_result_val(st.arr1, st.arr2);
120     }
121 
122     @Benchmark
123     @OperationsPerInvocation(SIZE)
124     @CompilerControl(CompilerControl.Mode.INLINE)
125     public boolean result_val_equals025(ValState25 st) {
126         return cmp_result_val(st.arr1, st.arr2);
127     }
128 
129     @Benchmark
130     @OperationsPerInvocation(SIZE)
131     @CompilerControl(CompilerControl.Mode.INLINE)
132     public boolean result_val_equals050(ValState50 st) {
133         return cmp_result_val(st.arr1, st.arr2);
134     }
135 
136     @Benchmark
137     @OperationsPerInvocation(SIZE)
138     @CompilerControl(CompilerControl.Mode.INLINE)
139     public boolean result_val_equals075(ValState75 st) {
140         return cmp_result_val(st.arr1, st.arr2);
141     }
142 
143     @Benchmark
144     @OperationsPerInvocation(SIZE)
145     @CompilerControl(CompilerControl.Mode.INLINE)
146     public boolean result_val_equals100(ValState100 st) {
147         return cmp_result_val(st.arr1, st.arr2);
148     }
149 
150     public static value class ValueInt4 {
151 
152         public final int v0;
153         public final int v1;
154         public final int v2;
155         public final int v3;
156 
157         public ValueInt4(int v) {
158             this.v0 = v;
159             this.v1 = v + 1;
160             this.v2 = v + 2;
161             this.v3 = v + 3;
162         }
163 
164     }
165 
166     private static void populate(Object[] arr1, Object[] arr2, int eq) {
167         if (eq <= 0) {
168             for (int i = 0; i < SIZE; i++) {
169                 arr1[i] = new ValueInt4(2 * i);
170                 arr2[i] = new ValueInt4(2 * i + 1);
171             }
172         } else if (eq >= 100) {
173             for (int i = 0; i < SIZE; i++) {
174                 arr2[i] = arr1[i] = new ValueInt4(i);
175             }
176         } else {
177             BitSet eqset = new Random(42).ints(0, SIZE).distinct().limit(eq * SIZE / 100).collect(BitSet::new, BitSet::set, BitSet::or);
178             for (int i = 0; i < SIZE; i++) {
179                 if (eqset.get(i)) {
180                     arr2[i] = arr1[i] = new ValueInt4(i);
181                 } else {
182                     arr1[i] = new ValueInt4(2 * i);
183                     arr2[i] = new ValueInt4(2 * i + 1);
184                 }
185             }
186         }
187     }
188 
189     @State(Scope.Thread)
190     public abstract static class ValState {
191         ValueInt4[] arr1, arr2;
192 
193         public void setup(int eq) {
194             arr1 = (ValueInt4[]) ValueClass.newNullRestrictedAtomicArray(ValueInt4.class, SIZE, new ValueInt4(0));
195             arr2 = (ValueInt4[]) ValueClass.newNullRestrictedAtomicArray(ValueInt4.class, SIZE, new ValueInt4(0));
196             populate(arr1, arr2, eq);
197         }
198     }
199 
200     public static class ValState00 extends ValState {
201         @Setup
202         public void setup() {
203             setup(0);
204         }
205     }
206 
207     public static class ValState25 extends ValState {
208         @Setup
209         public void setup() {
210             setup(25);
211         }
212     }
213 
214     public static class ValState50 extends ValState {
215         @Setup
216         public void setup() {
217             setup(50);
218         }
219     }
220 
221     public static class ValState75 extends ValState {
222         @Setup
223         public void setup() {
224             setup(75);
225         }
226     }
227 
228     public static class ValState100 extends ValState {
229         @Setup
230         public void setup() {
231             setup(100);
232         }
233     }
234 
235 }