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.field.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 static class RefWrapper {
32 public IdentityInt f;
33
34 public RefWrapper(IdentityInt f) {
35 this.f = f;
36 }
37 }
38
39 public static class IntWrapper {
40 public InterfaceInt f;
41
42 public IntWrapper(IdentityInt f) {
43 this.f = f;
44 }
45 }
46
47
48 public interface InterfaceInt {
49 public int value();
50 }
51
52 public static class IdentityInt implements InterfaceInt {
53 public final int value;
54 public IdentityInt(int value) {
55 this.value = value;
56 }
57 public int value() {
58 return value;
59 }
60 }
61
62
63 public static class RefState extends SizeState {
64 public RefWrapper[] arr;
65
66 @Setup
67 public void setup() {
68 arr = new RefWrapper[size];
69 for (int i = 0; i < size; i++) {
70 arr[i] = new RefWrapper(new IdentityInt(i));
71 }
72 }
73 }
74
75 public static class IntState extends SizeState {
76 public IntWrapper[] arr;
77
78 @Setup
79 public void setup() {
80 arr = new IntWrapper[size];
81 for (int i = 0; i < size; i++) {
82 arr[i] = new IntWrapper(new IdentityInt(i));
83 }
84 }
85 }
86
87 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
88 public void copy(RefWrapper[] dst, RefWrapper[] src) {
89 for (int i = 0; i < src.length; i++) {
90 dst[i].f = src[i].f;
91 }
92 }
93
94 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
95 public void copy_to_int(IntWrapper[] dst, RefWrapper[] src) {
96 for (int i = 0; i < src.length; i++) {
97 dst[i].f = src[i].f;
98 }
99 }
100
101 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
102 public void copy_to_ref(RefWrapper[] dst, IntWrapper[] src) {
103 for (int i = 0; i < src.length; i++) {
104 dst[i].f = (IdentityInt) src[i].f;
105 }
106 }
107
108 @Benchmark
109 public void copy_ref(RefState st1, RefState st2) {
110 copy(st1.arr, st2.arr);
111 }
112
113 @Benchmark
114 public void copy_ref_to_int(IntState st1, RefState st2) {
115 copy_to_int(st1.arr, st2.arr);
116 }
117
118 @Benchmark
119 public void copy_int_to_ref(RefState st1, IntState st2) {
120 copy_to_ref(st1.arr, st2.arr);
121 }
122
123 }