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