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.walk; 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 WalkBase { 30 31 public interface InterfaceInt { 32 public int value(); 33 } 34 35 public static class IdentityInt implements InterfaceInt { 36 37 public final int value; 38 39 public IdentityInt(int value) { 40 this.value = value; 41 } 42 43 public int value() { 44 return value; 45 } 46 47 } 48 49 50 public static class RefState extends SizeState { 51 public IdentityInt[] arr; 52 53 @Setup 54 public void setup() { 55 arr = new IdentityInt[size]; 56 int[] a = makeRandomRing(arr.length); 57 for (int i = 0; i < a.length; i++) { 58 arr[i] = new IdentityInt(a[i]); 59 } 60 } 61 } 62 63 public static class IntState extends SizeState { 64 public InterfaceInt[] arr; 65 66 @Setup 67 public void setup() { 68 arr = new InterfaceInt[size]; 69 int[] a = makeRandomRing(arr.length); 70 for (int i = 0; i < a.length; i++) { 71 arr[i] = new IdentityInt(a[i]); 72 } 73 } 74 } 75 76 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 77 private static int walk_ref(IdentityInt[] values) { 78 int steps = 0; 79 for (int i = values[0].value(); i != 0; i = values[i].value()) steps++; 80 return steps; 81 } 82 83 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 84 private static int walk_int(InterfaceInt[] values) { 85 int steps = 0; 86 for (int i = values[0].value(); i != 0; i = values[i].value()) steps++; 87 return steps; 88 } 89 90 @Benchmark 91 public int walk_ref_as_ref(RefState st) { 92 return walk_ref(st.arr); 93 } 94 95 @Benchmark 96 public int walk_int_as_int(IntState st) { 97 return walk_int(st.arr); 98 } 99 100 }