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 ValueOopNullFreeNonAtomic extends FillBase {
40
41 public static class IdentityInt {
42 public final int value;
43 public IdentityInt(int value) {
44 this.value = value;
45 }
46 public int value() {
47 return value;
48 }
49 }
50
51 public interface InterfaceOop {
52 public IdentityInt value();
53 }
54
55 @LooselyConsistentValue
56 public static value class ValueRef implements InterfaceOop {
57
58 public final IdentityInt value;
59
60 public ValueRef(IdentityInt value) {
61 this.value = value;
62 }
63
64 public IdentityInt value() {
65 return value;
66 }
67
68 }
69
70 public static class ValState extends SizeState {
71 public ValueRef[] arr;
72
73 @Setup
74 public void setup() {
75 arr = (ValueRef[]) ValueClass.newNullRestrictedAtomicArray(ValueRef.class, size, new ValueRef(new IdentityInt(0)));
76 for (int i = 0; i < size; i++) {
77 arr[i] = new ValueRef(new IdentityInt(i));
78 }
79 }
80 }
81
82 public static class StaticHolder {
83 @Strict
84 @NullRestricted
85 public static ValueRef VALUE = new ValueRef(new IdentityInt(42));
86 }
87
88 @State(Scope.Thread)
89 public static class InstanceHolder {
90 @Strict
91 @NullRestricted
92 public ValueRef VALUE = new ValueRef(new IdentityInt(42));
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 }