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