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.copy;
 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 CopyBase {
 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 void copy_ref(IdentityInt[] dst, IdentityInt[] src) {
 72         for (int i = 0; i < src.length; i++) {
 73             dst[i] = src[i];
 74         }
 75     }
 76 
 77     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 78     public void copy_int(InterfaceInt[] dst, InterfaceInt[] src) {
 79         for (int i = 0; i < src.length; i++) {
 80             dst[i] = src[i];
 81         }
 82     }
 83 
 84     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 85     public void arraycopy_ref(IdentityInt[] dst, IdentityInt[] src) {
 86         System.arraycopy(src, 0, dst, 0, src.length);
 87     }
 88 
 89     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 90     public void arraycopy_int(InterfaceInt[] dst, InterfaceInt[] src) {
 91         System.arraycopy(src, 0, dst, 0, src.length);
 92     }
 93 
 94     @Benchmark
 95     public void copy_ref_as_ref(RefState st1, RefState st2) {
 96         copy_ref(st1.arr, st2.arr);
 97     }
 98 
 99     @Benchmark
100     public void arraycopy_ref_as_ref(RefState st1, RefState st2) {
101         arraycopy_ref(st1.arr, st2.arr);
102     }
103 
104 }