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