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.NullRestricted;
27 import org.openjdk.jmh.annotations.Benchmark;
28 import org.openjdk.jmh.annotations.CompilerControl;
29 import org.openjdk.jmh.annotations.Fork;
30 import org.openjdk.jmh.annotations.Scope;
31 import org.openjdk.jmh.annotations.Setup;
32 import org.openjdk.jmh.annotations.State;
33
34 import java.util.Arrays;
35
36 @Fork(value = 3, jvmArgsAppend = {"--enable-preview", "--add-exports", "java.base/jdk.internal.value=ALL-UNNAMED"})
37 public class Value128NullFree extends FillBase {
38
39 public interface InterfaceInt {
40 public int value();
41 }
42
43 public static value class ValueInt4 implements InterfaceInt {
44
45 public final int prevalue0;
46 public final int prevalue1;
47 public final int prevalue2;
48
49 public final int value;
50
51 public ValueInt4(int value) {
52 this.prevalue0 = value;
53 this.prevalue1 = value;
54 this.prevalue2 = value;
55 this.value = value;
56 }
57
58 public int value() {
59 return value;
60 }
61
62 }
63
64 public static class ValState extends SizeState {
65 public ValueInt4[] arr;
66
67 @Setup
68 public void setup() {
69 arr = (ValueInt4[]) ValueClass.newNullRestrictedAtomicArray(ValueInt4.class, size, new ValueInt4(0));
70 for (int i = 0; i < size; i++) {
71 arr[i] = new ValueInt4(i);
72 }
73 }
74 }
75
76 public static class StaticHolder {
77 @NullRestricted
78 public static ValueInt4 VALUE = new ValueInt4(42);
79 }
80
81 @State(Scope.Thread)
82 public static class InstanceHolder {
83 @NullRestricted
84 public ValueInt4 VALUE;
85
86 public InstanceHolder() {
87 VALUE = new ValueInt4(42);
88 super();
89 }
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 }