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