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