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