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