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