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