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