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 Value032NullFree { 55 56 public static final int SIZE = 100; 57 58 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 59 private static int cmp_branch_val(ValueInt[] objects1, ValueInt[] 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(ValueInt[] objects1, ValueInt[] 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 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(Object[] arr1, Object[] arr2, int eq) { 165 if (eq <= 0) { 166 for (int i = 0; i < SIZE; i++) { 167 arr1[i] = new ValueInt(2 * i); 168 arr2[i] = new ValueInt(2 * i + 1); 169 } 170 } else if (eq >= 100) { 171 for (int i = 0; i < SIZE; i++) { 172 arr2[i] = arr1[i] = new ValueInt(i); 173 } 174 } else { 175 BitSet eqset = new Random(42).ints(0, SIZE).distinct().limit(eq * SIZE / 100).collect(BitSet::new, BitSet::set, BitSet::or); 176 for (int i = 0; i < SIZE; i++) { 177 if (eqset.get(i)) { 178 arr2[i] = arr1[i] = new ValueInt(i); 179 } else { 180 arr1[i] = new ValueInt(2 * i); 181 arr2[i] = new ValueInt(2 * i + 1); 182 } 183 } 184 } 185 } 186 187 @State(Scope.Thread) 188 public abstract static class ValState { 189 ValueInt[] arr1, arr2; 190 191 public void setup(int eq) { 192 arr1 = (ValueInt[]) ValueClass.newNullRestrictedAtomicArray(ValueInt.class, SIZE, new ValueInt(0)); 193 arr2 = (ValueInt[]) ValueClass.newNullRestrictedAtomicArray(ValueInt.class, SIZE, new ValueInt(0)); 194 populate(arr1, arr2, eq); 195 } 196 } 197 198 public static class ValState00 extends ValState { 199 @Setup 200 public void setup() { 201 setup(0); 202 } 203 } 204 205 public static class ValState25 extends ValState { 206 @Setup 207 public void setup() { 208 setup(25); 209 } 210 } 211 212 public static class ValState50 extends ValState { 213 @Setup 214 public void setup() { 215 setup(50); 216 } 217 } 218 219 public static class ValState75 extends ValState { 220 @Setup 221 public void setup() { 222 setup(75); 223 } 224 } 225 226 public static class ValState100 extends ValState { 227 @Setup 228 public void setup() { 229 setup(100); 230 } 231 } 232 233 }