1/* 2 * Copyright (c) 2015, 2015, 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 */ 23package micro.benchmarks; 24 25import org.openjdk.jmh.annotations.Benchmark; 26import org.openjdk.jmh.annotations.Level; 27import org.openjdk.jmh.annotations.Scope; 28import org.openjdk.jmh.annotations.Setup; 29import org.openjdk.jmh.annotations.State; 30import org.openjdk.jmh.annotations.Warmup; 31 32import org.graalvm.compiler.microbenchmarks.graal.GraalBenchmark; 33 34/** 35 * Benchmarks cost of guarded intrinsics at indirect call sites. 36 */ 37public class GuardedIntrinsicBenchmark extends GraalBenchmark { 38 39 public static class HashcodeState { 40 public Object val1; 41 public Object val2; 42 43 public HashcodeState(Object val1, Object val2) { 44 this.val1 = val1; 45 this.val2 = val2; 46 } 47 48 int getNextHashCode() { 49 return val1.hashCode(); 50 } 51 52 protected void swap() { 53 Object tmp = val1; 54 val1 = val2; 55 val2 = tmp; 56 } 57 } 58 59 /** 60 * Objects that all override {@link Object#hashCode()}. The objects used have hashCode 61 * implementations that are basically getters as we want to measure the overhead of hashCode 62 * dispatch, not the cost of the hashCode implementation. 63 */ 64 @State(Scope.Benchmark) 65 public static class OverrideHashcode extends HashcodeState { 66 public OverrideHashcode() { 67 super(Short.valueOf((short) 100), Integer.valueOf(42)); 68 } 69 70 @Setup(Level.Invocation) 71 public void beforeInvocation() { 72 swap(); 73 } 74 } 75 76 @Benchmark 77 @Warmup(iterations = 10) 78 public int overrideHashCode(OverrideHashcode state) { 79 return state.getNextHashCode(); 80 } 81 82 /** 83 * Objects that do not override {@link Object#hashCode()}. 84 */ 85 @State(Scope.Benchmark) 86 public static class InheritHashcode extends HashcodeState { 87 public InheritHashcode() { 88 super(Class.class, Runtime.getRuntime()); 89 } 90 91 @Setup(Level.Invocation) 92 public void beforeInvocation() { 93 swap(); 94 } 95 } 96 97 @Benchmark 98 @Warmup(iterations = 10) 99 public int inheritHashCode(InheritHashcode state) { 100 return state.getNextHashCode(); 101 } 102 103 /** 104 * Some objects that override {@link Object#hashCode()} and some that don't. 105 */ 106 @State(Scope.Benchmark) 107 public static class MixedHashcode extends HashcodeState { 108 public MixedHashcode() { 109 super(Short.valueOf((short) 100), Runtime.getRuntime()); 110 } 111 112 @Setup(Level.Invocation) 113 public void beforeInvocation() { 114 swap(); 115 } 116 } 117 118 @Benchmark 119 @Warmup(iterations = 10) 120 public int mixedHashCode(MixedHashcode state) { 121 return state.getNextHashCode(); 122 } 123} 124