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.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 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 ValueOopNullFreeNonAtomic 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     @LooselyConsistentValue
 55     public static value class ValueRef implements InterfaceOop {
 56 
 57         public final IdentityInt value;
 58 
 59         public ValueRef(IdentityInt value) {
 60             this.value = value;
 61         }
 62 
 63         public IdentityInt value() {
 64             return value;
 65         }
 66 
 67     }
 68 
 69     public static class ValState extends SizeState {
 70         public ValueRef[] arr;
 71 
 72         @Setup
 73         public void setup() {
 74             arr = (ValueRef[]) ValueClass.newNullRestrictedAtomicArray(ValueRef.class, size, new ValueRef(new IdentityInt(0)));
 75             for (int i = 0; i < size; i++) {
 76                 arr[i] = new ValueRef(new IdentityInt(i));
 77             }
 78         }
 79     }
 80 
 81     public static class StaticHolder {
 82         @NullRestricted
 83         public static ValueRef VALUE = new ValueRef(new IdentityInt(42));
 84     }
 85 
 86     @State(Scope.Thread)
 87     public static class InstanceHolder {
 88         @NullRestricted
 89         public ValueRef VALUE;
 90 
 91         public InstanceHolder() {
 92             VALUE = new ValueRef(new IdentityInt(42));
 93             super();
 94         }
 95     }
 96 
 97     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 98     public ValueRef get_val(int i) {
 99         return new ValueRef(new IdentityInt(i));
100     }
101 
102     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
103     public void fill_new_val(ValueRef[] dst) {
104         for (int i = 0; i < dst.length; i++) {
105             dst[i] = new ValueRef(new IdentityInt(42));
106         }
107     }
108 
109     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
110     public void arrayfill_new_val(ValueRef[] dst) {
111         Arrays.fill(dst, new ValueRef(new IdentityInt(42)));
112     }
113 
114     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
115     public void fill_local_val(ValueRef[] dst) {
116         ValueRef local = get_val(42);
117         for (int i = 0; i < dst.length; i++) {
118             dst[i] = local;
119         }
120     }
121 
122     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
123     public void arrayfill_local_val(ValueRef[] dst) {
124         Arrays.fill(dst, get_val(42));
125     }
126 
127     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
128     public void fill_static_val(ValueRef[] dst) {
129         for (int i = 0; i < dst.length; i++) {
130             dst[i] = StaticHolder.VALUE;
131         }
132     }
133 
134     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
135     public void arrayfill_static_val(ValueRef[] dst) {
136         Arrays.fill(dst, StaticHolder.VALUE);
137     }
138 
139     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
140     public void fill_instance_val(ValueRef[] dst, InstanceHolder ih) {
141         for (int i = 0; i < dst.length; i++) {
142             dst[i] = ih.VALUE;
143         }
144     }
145 
146     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
147     public void arrayfill_instance_val(ValueRef[] dst, InstanceHolder ih) {
148         Arrays.fill(dst, ih.VALUE);
149     }
150 
151     @Benchmark
152     public void fill_new(ValState st1) {
153         fill_new_val(st1.arr);
154     }
155 
156     @Benchmark
157     public void arrayfill_new(ValState st1) {
158         arrayfill_new_val(st1.arr);
159     }
160 
161     @Benchmark
162     public void fill_local(ValState st1) {
163         fill_local_val(st1.arr);
164     }
165 
166     @Benchmark
167     public void arrayfill_local(ValState st1) {
168         arrayfill_local_val(st1.arr);
169     }
170 
171     @Benchmark
172     public void fill_static(ValState st1) {
173         fill_static_val(st1.arr);
174     }
175 
176     @Benchmark
177     public void arrayfill_static(ValState st1) {
178         arrayfill_static_val(st1.arr);
179     }
180 
181     @Benchmark
182     public void fill_instance(ValState st1, InstanceHolder ih) {
183         fill_instance_val(st1.arr, ih);
184     }
185 
186     @Benchmark
187     public void arrayfill_instance(ValState st1, InstanceHolder ih) {
188         arrayfill_instance_val(st1.arr, ih);
189     }
190 
191 }