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.set;
 24 
 25 import org.openjdk.jmh.annotations.Benchmark;
 26 import org.openjdk.jmh.annotations.CompilerControl;
 27 import org.openjdk.jmh.annotations.Setup;
 28 
 29 public class Identity extends SetBase {
 30 
 31     public interface InterfaceInt {
 32         public int value();
 33     }
 34 
 35     public static class IdentityInt implements InterfaceInt {
 36         public final int value;
 37         public IdentityInt(int value) {
 38             this.value = value;
 39         }
 40         public int value() {
 41             return value;
 42         }
 43     }
 44 
 45 
 46     public static class RefState extends SizeState {
 47         public IdentityInt[] arr;
 48 
 49         @Setup
 50         public void setup() {
 51             arr = new IdentityInt[size];
 52             for (int i = 0; i < size; i++) {
 53                 arr[i] = new IdentityInt(i);
 54             }
 55         }
 56     }
 57 
 58     public static class IntState extends SizeState {
 59         public InterfaceInt[] arr;
 60 
 61         @Setup
 62         public void setup() {
 63             arr = new InterfaceInt[size];
 64             for (int i = 0; i < size; i++) {
 65                 arr[i] = new IdentityInt(i);
 66             }
 67         }
 68     }
 69 
 70     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 71     public IdentityInt get_ref(int i) {
 72         return new IdentityInt(i);
 73     }
 74 
 75     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 76     public InterfaceInt get_int(int i) {
 77         return new IdentityInt(i);
 78     }
 79 
 80     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 81     public void set_new_ref(IdentityInt[] dst) {
 82         for (int i = 0; i < dst.length; i++) {
 83             dst[i] = new IdentityInt(i);
 84         }
 85     }
 86 
 87     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 88     public void set_new_int(InterfaceInt[] dst) {
 89         for (int i = 0; i < dst.length; i++) {
 90             dst[i] = new IdentityInt(i);
 91         }
 92     }
 93 
 94     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 95     public void set_call_ref(IdentityInt[] dst) {
 96         for (int i = 0; i < dst.length; i++) {
 97             dst[i] = get_ref(i);
 98         }
 99     }
100 
101     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
102     public void set_call_int(InterfaceInt[] dst) {
103         for (int i = 0; i < dst.length; i++) {
104             dst[i] = get_int(i);
105         }
106     }
107 
108     @Benchmark
109     public void set_new_ref_as_ref(RefState st1) {
110         set_new_ref(st1.arr);
111     }
112 
113     @Benchmark
114     public void set_new_int_as_int(IntState st1) {
115         set_new_int(st1.arr);
116     }
117 
118     @Benchmark
119     public void set_call_ref_as_ref(RefState st1) {
120         set_call_ref(st1.arr);
121     }
122 
123     @Benchmark
124     public void set_call_int_as_int(IntState st1) {
125         set_call_int(st1.arr);
126     }
127 
128 }