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.Scope;
 28 import org.openjdk.jmh.annotations.Setup;
 29 import org.openjdk.jmh.annotations.State;
 30 
 31 import java.util.Arrays;
 32 
 33 public class Identity extends FillBase {
 34 
 35     public interface InterfaceInt {
 36         public int value();
 37     }
 38 
 39     public static class IdentityInt implements InterfaceInt {
 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 
 50     public static class RefState extends SizeState {
 51         public IdentityInt[] arr;
 52 
 53         @Setup
 54         public void setup() {
 55             arr = new IdentityInt[size];
 56             for (int i = 0; i < size; i++) {
 57                 arr[i] = new IdentityInt(i);
 58             }
 59         }
 60     }
 61 
 62     public static class StaticHolder {
 63         public static IdentityInt VALUE = new IdentityInt(42);
 64     }
 65 
 66     @State(Scope.Thread)
 67     public static class InstanceHolder {
 68         public IdentityInt VALUE = new IdentityInt(42);
 69     }
 70 
 71     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 72     public IdentityInt get_ref(int i) {
 73         return new IdentityInt(i);
 74     }
 75 
 76     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 77     public void fill_new_ref(IdentityInt[] dst) {
 78         for (int i = 0; i < dst.length; i++) {
 79             dst[i] = new IdentityInt(42);
 80         }
 81     }
 82 
 83     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 84     public void arrayfill_new_ref(IdentityInt[] dst) {
 85         Arrays.fill(dst, new IdentityInt(42));
 86     }
 87 
 88     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 89     public void fill_local_ref(IdentityInt[] dst) {
 90         IdentityInt local = get_ref(42);
 91         for (int i = 0; i < dst.length; i++) {
 92             dst[i] = local;
 93         }
 94     }
 95 
 96     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 97     public void arrayfill_local_ref(IdentityInt[] dst) {
 98         Arrays.fill(dst, get_ref(42));
 99     }
100 
101     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
102     public void fill_static_ref(IdentityInt[] dst) {
103         for (int i = 0; i < dst.length; i++) {
104             dst[i] = StaticHolder.VALUE;
105         }
106     }
107 
108     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
109     public void arrayfill_static_ref(IdentityInt[] dst) {
110         Arrays.fill(dst, StaticHolder.VALUE);
111     }
112 
113     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
114     public void fill_instance_ref(IdentityInt[] dst, InstanceHolder ih) {
115         for (int i = 0; i < dst.length; i++) {
116             dst[i] = ih.VALUE;
117         }
118     }
119 
120     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
121     public void arrayfill_instance_ref(IdentityInt[] dst, InstanceHolder ih) {
122         Arrays.fill(dst, ih.VALUE);
123     }
124 
125     @Benchmark
126     public void fill_new(RefState st1) {
127         fill_new_ref(st1.arr);
128     }
129 
130     @Benchmark
131     public void arrayfill_new(RefState st1) {
132         arrayfill_new_ref(st1.arr);
133     }
134 
135     @Benchmark
136     public void fill_local(RefState st1) {
137         fill_local_ref(st1.arr);
138     }
139 
140     @Benchmark
141     public void arrayfill_local(RefState st1) {
142         arrayfill_local_ref(st1.arr);
143     }
144 
145     @Benchmark
146     public void fill_static(RefState st1) {
147         fill_static_ref(st1.arr);
148     }
149 
150     @Benchmark
151     public void arrayfill_static(RefState st1) {
152         arrayfill_static_ref(st1.arr);
153     }
154 
155     @Benchmark
156     public void fill_instance(RefState st1, InstanceHolder ih) {
157         fill_instance_ref(st1.arr, ih);
158     }
159 
160     @Benchmark
161     public void arrayfill_instance(RefState st1, InstanceHolder ih) {
162         arrayfill_instance_ref(st1.arr, ih);
163     }
164 
165 }