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.array.fill; 24 25 import jdk.internal.value.ValueClass; 26 import jdk.internal.vm.annotation.LooselyConsistentValue; 27 import jdk.internal.vm.annotation.NullRestricted; 28 import jdk.internal.vm.annotation.Strict; 29 import org.openjdk.jmh.annotations.Benchmark; 30 import org.openjdk.jmh.annotations.CompilerControl; 31 import org.openjdk.jmh.annotations.Fork; 32 import org.openjdk.jmh.annotations.Scope; 33 import org.openjdk.jmh.annotations.Setup; 34 import org.openjdk.jmh.annotations.State; 35 36 import java.util.Arrays; 37 38 @Fork(value = 3, jvmArgsAppend = {"--enable-preview", "--add-exports", "java.base/jdk.internal.value=ALL-UNNAMED"}) 39 public class Value032NullFreeNonAtomic extends FillBase { 40 41 public interface InterfaceInt { 42 public int value(); 43 } 44 45 @LooselyConsistentValue 46 public static value class ValueInt implements InterfaceInt { 47 48 public final int value; 49 50 public ValueInt(int value) { 51 this.value = value; 52 } 53 54 public int value() { 55 return value; 56 } 57 58 } 59 60 public static class ValState extends SizeState { 61 public ValueInt[] arr; 62 63 @Setup 64 public void setup() { 65 arr = (ValueInt[]) ValueClass.newNullRestrictedNonAtomicArray(ValueInt.class, size, new ValueInt(0)); 66 for (int i = 0; i < size; i++) { 67 arr[i] = new ValueInt(i); 68 } 69 } 70 } 71 72 public static class StaticHolder { 73 @Strict 74 @NullRestricted 75 public static ValueInt VALUE = new ValueInt(42); 76 } 77 78 @State(Scope.Thread) 79 public static class InstanceHolder { 80 @Strict 81 @NullRestricted 82 public ValueInt VALUE = new ValueInt(42); 83 } 84 85 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 86 public ValueInt get_val(int i) { 87 return new ValueInt(i); 88 } 89 90 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 91 public void fill_new_val(ValueInt[] dst) { 92 for (int i = 0; i < dst.length; i++) { 93 dst[i] = new ValueInt(42); 94 } 95 } 96 97 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 98 public void arrayfill_new_val(ValueInt[] dst) { 99 Arrays.fill(dst, new ValueInt(42)); 100 } 101 102 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 103 public void fill_local_val(ValueInt[] dst) { 104 ValueInt local = get_val(42); 105 for (int i = 0; i < dst.length; i++) { 106 dst[i] = local; 107 } 108 } 109 110 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 111 public void arrayfill_local_val(ValueInt[] dst) { 112 Arrays.fill(dst, get_val(42)); 113 } 114 115 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 116 public void fill_static_val(ValueInt[] dst) { 117 for (int i = 0; i < dst.length; i++) { 118 dst[i] = StaticHolder.VALUE; 119 } 120 } 121 122 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 123 public void arrayfill_static_val(ValueInt[] dst) { 124 Arrays.fill(dst, StaticHolder.VALUE); 125 } 126 127 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 128 public void fill_instance_val(ValueInt[] dst, InstanceHolder ih) { 129 for (int i = 0; i < dst.length; i++) { 130 dst[i] = ih.VALUE; 131 } 132 } 133 134 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 135 public void arrayfill_instance_val(ValueInt[] dst, InstanceHolder ih) { 136 Arrays.fill(dst, ih.VALUE); 137 } 138 139 @Benchmark 140 public void fill_new(ValState st1) { 141 fill_new_val(st1.arr); 142 } 143 144 @Benchmark 145 public void arrayfill_new(ValState st1) { 146 arrayfill_new_val(st1.arr); 147 } 148 149 @Benchmark 150 public void fill_local(ValState st1) { 151 fill_local_val(st1.arr); 152 } 153 154 @Benchmark 155 public void arrayfill_local(ValState st1) { 156 arrayfill_local_val(st1.arr); 157 } 158 159 @Benchmark 160 public void fill_static(ValState st1) { 161 fill_static_val(st1.arr); 162 } 163 164 @Benchmark 165 public void arrayfill_static(ValState st1) { 166 arrayfill_static_val(st1.arr); 167 } 168 169 @Benchmark 170 public void fill_instance(ValState st1, InstanceHolder ih) { 171 fill_instance_val(st1.arr, ih); 172 } 173 174 @Benchmark 175 public void arrayfill_instance(ValState st1, InstanceHolder ih) { 176 arrayfill_instance_val(st1.arr, ih); 177 } 178 179 }