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