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