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 Value032NullFree {
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 @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 ValueInt {
151
152 public final int v0;
153
154 public ValueInt(int v0) {
155 this.v0 = v0;
156 }
157
158 public int value() {
159 return v0;
160 }
161
162 }
163
164 private static void populate(ValWrapper[] arr1, ValWrapper[] arr2, int eq) {
165 if (eq <= 0) {
166 for (int i = 0; i < SIZE; i++) {
167 arr1[i] = new ValWrapper(new ValueInt(2 * i));
168 arr2[i] = new ValWrapper(new ValueInt(2 * i + 1));
169 }
170 } else if (eq >= 100) {
171 for (int i = 0; i < SIZE; i++) {
172 ValueInt x = new ValueInt(i);
173 arr2[i] = new ValWrapper(x);
174 arr1[i] = new ValWrapper(x);
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 ValueInt x = new ValueInt(i);
181 arr2[i] = new ValWrapper(x);
182 arr1[i] = new ValWrapper(x);
183 } else {
184 arr1[i] = new ValWrapper(new ValueInt(2 * i));
185 arr2[i] = new ValWrapper(new ValueInt(2 * i + 1));
186 }
187 }
188 }
189 }
190
191 public static class ValWrapper {
192
193 @NullRestricted
194 public final ValueInt f;
195
196 public ValWrapper(ValueInt f) {
197 this.f = f;
198 super();
199 }
200 }
201
202 @State(Scope.Thread)
203 public abstract static class ValState {
204 ValWrapper[] arr1, arr2;
205
206 public void setup(int eq) {
207 arr1 = new ValWrapper[SIZE];
208 arr2 = new ValWrapper[SIZE];
209 populate(arr1, arr2, eq);
210 }
211 }
212
213 public static class ValState00 extends ValState {
214 @Setup
215 public void setup() {
216 setup(0);
217 }
218 }
219
220 public static class ValState25 extends ValState {
221 @Setup
222 public void setup() {
223 setup(25);
224 }
225 }
226
227 public static class ValState50 extends ValState {
228 @Setup
229 public void setup() {
230 setup(50);
231 }
232 }
233
234 public static class ValState75 extends ValState {
235 @Setup
236 public void setup() {
237 setup(75);
238 }
239 }
240
241 public static class ValState100 extends ValState {
242 @Setup
243 public void setup() {
244 setup(100);
245 }
246 }
247
248 }