1 /*
  2  * Copyright (c) 2020, 2024, 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.bench.valhalla.array.util.StatesR32int;
 26 import org.openjdk.bench.valhalla.types.R32int;
 27 import org.openjdk.jmh.annotations.Benchmark;
 28 import org.openjdk.jmh.annotations.CompilerControl;
 29 import org.openjdk.jmh.annotations.Scope;
 30 import org.openjdk.jmh.annotations.State;
 31 
 32 import java.util.Arrays;
 33 
 34 public class Identity32int extends StatesR32int {
 35 
 36     public static class RefStaticField {
 37         static R32int f = new R32int(42);
 38     }
 39 
 40     @State(Scope.Thread)
 41     public static class RefInstanceField {
 42         R32int f = new R32int(42);
 43     }
 44 
 45     @Benchmark
 46     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 47     public void Def_to_Ref_as_Ref_fill(Ref_as_Ref st) {
 48         R32int[] arr = st.arr;
 49         for (int i = 0; i < arr.length; i++) {
 50             arr[i] = new R32int();
 51         }
 52     }
 53 
 54     @Benchmark
 55     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 56     public void New_to_Ref_as_Ref_fill(Ref_as_Ref st) {
 57         R32int[] arr = st.arr;
 58         R32int v = new R32int(42);
 59         for (int i = 0; i < arr.length; i++) {
 60             arr[i] = v;
 61         }
 62     }
 63 
 64     @Benchmark
 65     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 66     public void Ref_to_Ref_as_Ref_fill_fillstat(Ref_as_Ref st) {
 67         R32int[] arr = st.arr;
 68         for (int i = 0; i < arr.length; i++) {
 69             arr[i] = RefStaticField.f;
 70         }
 71     }
 72 
 73     @Benchmark
 74     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 75     public void Ref_to_Ref_as_Ref_fillinst(Ref_as_Ref st, RefInstanceField f) {
 76         R32int[] arr = st.arr;
 77         for (int i = 0; i < arr.length; i++) {
 78             arr[i] = f.f;
 79         }
 80     }
 81 
 82     @Benchmark
 83     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 84     public void Def_to_Ref_as_Ref_arrayfill(Ref_as_Ref st) {
 85         Arrays.fill(st.arr, new R32int()  );
 86     }
 87 
 88     @Benchmark
 89     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 90     public void New_to_Ref_as_Ref_arrayfill(Ref_as_Ref st) {
 91         Arrays.fill(st.arr, new R32int(42));
 92     }
 93 
 94     @Benchmark
 95     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 96     public void Ref_to_Ref_as_Ref_arrayfillstat(Ref_as_Ref st) {
 97         Arrays.fill(st.arr, RefStaticField.f);
 98     }
 99 
100     @Benchmark
101     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
102     public void Ref_to_Ref_as_Ref_arrayfillinst(Ref_as_Ref st, RefInstanceField f) {
103         Arrays.fill(st.arr, f.f);
104     }
105 
106 }