1 /*
  2  * Copyright (c) 2025, 2026, 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.field;
 24 
 25 import jdk.internal.vm.annotation.NullRestricted;
 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.field.Value  -wmb "org.openjdk.bench.valhalla.acmp.field.Value.*050"
 46  */
 47 
 48 @Fork(value = 3, jvmArgsAppend = {"--enable-preview"})
 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(ValWrapper[] objects1, ValWrapper[] objects2) {
 60         int s = 0;
 61         for (int i = 0; i < SIZE; i++) {
 62             if (objects1[i].f == objects2[i].f) {
 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(ValWrapper[] objects1, ValWrapper[] objects2) {
 73         boolean s = false;
 74         for (int i = 0; i < SIZE; i++) {
 75             s ^= objects1[i].f == objects2[i].f;
 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 
116     @Benchmark
117     @OperationsPerInvocation(SIZE)
118     @CompilerControl(CompilerControl.Mode.INLINE)
119     public boolean result_val_equals000(ValState00 st) {
120         return cmp_result_val(st.arr1, st.arr2);
121     }
122 
123     @Benchmark
124     @OperationsPerInvocation(SIZE)
125     @CompilerControl(CompilerControl.Mode.INLINE)
126     public boolean result_val_equals025(ValState25 st) {
127         return cmp_result_val(st.arr1, st.arr2);
128     }
129 
130     @Benchmark
131     @OperationsPerInvocation(SIZE)
132     @CompilerControl(CompilerControl.Mode.INLINE)
133     public boolean result_val_equals050(ValState50 st) {
134         return cmp_result_val(st.arr1, st.arr2);
135     }
136 
137     @Benchmark
138     @OperationsPerInvocation(SIZE)
139     @CompilerControl(CompilerControl.Mode.INLINE)
140     public boolean result_val_equals075(ValState75 st) {
141         return cmp_result_val(st.arr1, st.arr2);
142     }
143 
144     @Benchmark
145     @OperationsPerInvocation(SIZE)
146     @CompilerControl(CompilerControl.Mode.INLINE)
147     public boolean result_val_equals100(ValState100 st) {
148         return cmp_result_val(st.arr1, st.arr2);
149     }
150 
151     public static value class ValueInt4 {
152 
153         public final int v0;
154         public final int v1;
155         public final int v2;
156         public final int v3;
157 
158         public ValueInt4(int v) {
159             this.v0 = v;
160             this.v1 = v + 1;
161             this.v2 = v + 2;
162             this.v3 = v + 3;
163         }
164 
165     }
166 
167     private static void populate(ValWrapper[] arr1, ValWrapper[] arr2, int eq) {
168         if (eq <= 0) {
169             for (int i = 0; i < SIZE; i++) {
170                 arr1[i] = new ValWrapper(new ValueInt4(2 * i));
171                 arr2[i] = new ValWrapper(new ValueInt4(2 * i + 1));
172             }
173         } else if (eq >= 100) {
174             for (int i = 0; i < SIZE; i++) {
175                 ValueInt4 x = new ValueInt4(i);
176                 arr2[i] = new ValWrapper(x);
177                 arr1[i] = new ValWrapper(x);
178             }
179         } else {
180             BitSet eqset = new Random(42).ints(0, SIZE).distinct().limit(eq * SIZE / 100).collect(BitSet::new, BitSet::set, BitSet::or);
181             for (int i = 0; i < SIZE; i++) {
182                 if (eqset.get(i)) {
183                     ValueInt4 x = new ValueInt4(i);
184                     arr2[i] = new ValWrapper(x);
185                     arr1[i] = new ValWrapper(x);
186                 } else {
187                     arr1[i] = new ValWrapper(new ValueInt4(2 * i));
188                     arr2[i] = new ValWrapper(new ValueInt4(2 * i + 1));
189                 }
190             }
191         }
192     }
193 
194     public static class ValWrapper {
195 
196         @NullRestricted
197         public final ValueInt4 f;
198 
199         public ValWrapper(ValueInt4 f) {
200             this.f = f;
201             super();
202         }
203     }
204 
205 
206     @State(Scope.Thread)
207     public abstract static class ValState {
208         ValWrapper[] arr1, arr2;
209 
210         public void setup(int eq) {
211             arr1 = new ValWrapper[SIZE];
212             arr2 = new ValWrapper[SIZE];
213             populate(arr1, arr2, eq);
214         }
215     }
216 
217     public static class ValState00 extends ValState {
218         @Setup
219         public void setup() {
220             setup(0);
221         }
222     }
223 
224     public static class ValState25 extends ValState {
225         @Setup
226         public void setup() {
227             setup(25);
228         }
229     }
230 
231     public static class ValState50 extends ValState {
232         @Setup
233         public void setup() {
234             setup(50);
235         }
236     }
237 
238     public static class ValState75 extends ValState {
239         @Setup
240         public void setup() {
241             setup(75);
242         }
243     }
244 
245     public static class ValState100 extends ValState {
246         @Setup
247         public void setup() {
248             setup(100);
249         }
250     }
251 
252 }