1 /* 2 * Copyright (c) 2014 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.openjdk.bench.java.lang.invoke; 26 27 import org.openjdk.jmh.annotations.Benchmark; 28 import org.openjdk.jmh.annotations.BenchmarkMode; 29 import org.openjdk.jmh.annotations.Mode; 30 import org.openjdk.jmh.annotations.OutputTimeUnit; 31 import org.openjdk.jmh.annotations.Scope; 32 import org.openjdk.jmh.annotations.State; 33 34 import java.lang.invoke.ConstantCallSite; 35 import java.lang.invoke.MethodHandle; 36 import java.lang.invoke.MethodHandles; 37 import java.lang.invoke.MethodType; 38 import java.lang.invoke.MutableCallSite; 39 import java.lang.invoke.VolatileCallSite; 40 import java.util.concurrent.TimeUnit; 41 42 /** 43 * This benchmark evaluates INDY performance when call sites are not changed. 44 */ 45 @BenchmarkMode(Mode.AverageTime) 46 @OutputTimeUnit(TimeUnit.NANOSECONDS) 47 @State(Scope.Thread) 48 public class CallSiteStable { 49 50 /* 51 * Implementation notes: 52 * - Test is calling simple method via INDY 53 * - Additional baseline includes "raw" test, calling target method directly in virtual and static modes 54 */ 55 56 private static java.lang.invoke.CallSite cs; 57 58 private static MethodHandle doCallMH; 59 60 static { 61 try { 62 doCallMH = MethodHandles.lookup().findVirtual(CallSiteStable.class, "doCall", MethodType.methodType(int.class, int.class)); 63 } catch (NoSuchMethodException | IllegalAccessException e) { 64 throw new IllegalStateException(e); 65 } 66 } 67 68 private int i; 69 70 public int doCall(int value) { 71 return value + 1; 72 } 73 74 public static int doCallStatic(int value) { 75 return value + 1; 76 } 77 78 @Benchmark 79 public void baselineVirtual() { 80 i = doCall(i); 81 } 82 83 @Benchmark 84 public void baselineStatic() { 85 i = doCallStatic(i); 86 } 87 88 @Benchmark 89 public void testConstant() throws Throwable { 90 i = (int) INDY_Constant().invokeExact(this, i); 91 } 92 93 @Benchmark 94 public void testMutable() throws Throwable { 95 i = (int) INDY_Mutable().invokeExact(this, i); 96 } 97 98 @Benchmark 99 public void testVolatile() throws Throwable { 100 i = (int) INDY_Volatile().invokeExact(this, i); 101 } 102 103 /* =========================== INDY TRAMPOLINES ============================== */ 104 105 private static MethodType MT_bsm() { 106 shouldNotCallThis(); 107 return MethodType.methodType(java.lang.invoke.CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class); 108 } 109 110 private static MethodHandle MH_bsm_Constant() throws ReflectiveOperationException { 111 shouldNotCallThis(); 112 return MethodHandles.lookup().findStatic(MethodHandles.lookup().lookupClass(), "bsm_Constant", MT_bsm()); 113 } 114 115 private static MethodHandle MH_bsm_Mutable() throws ReflectiveOperationException { 116 shouldNotCallThis(); 117 return MethodHandles.lookup().findStatic(MethodHandles.lookup().lookupClass(), "bsm_Mutable", MT_bsm()); 118 } 119 120 private static MethodHandle MH_bsm_Volatile() throws ReflectiveOperationException { 121 shouldNotCallThis(); 122 return MethodHandles.lookup().findStatic(MethodHandles.lookup().lookupClass(), "bsm_Volatile", MT_bsm()); 123 } 124 125 private static MethodHandle INDY_Constant() throws Throwable { 126 shouldNotCallThis(); 127 return ((java.lang.invoke.CallSite) MH_bsm_Constant().invoke(MethodHandles.lookup(), "doCall", MethodType.methodType(int.class, CallSiteStable.class, int.class))).dynamicInvoker(); 128 } 129 private static MethodHandle INDY_Mutable() throws Throwable { 130 shouldNotCallThis(); 131 return ((java.lang.invoke.CallSite) MH_bsm_Mutable().invoke(MethodHandles.lookup(), "doCall", MethodType.methodType(int.class, CallSiteStable.class, int.class))).dynamicInvoker(); 132 } 133 private static MethodHandle INDY_Volatile() throws Throwable { 134 shouldNotCallThis(); 135 return ((java.lang.invoke.CallSite) MH_bsm_Volatile().invoke(MethodHandles.lookup(), "doCall", MethodType.methodType(int.class, CallSiteStable.class, int.class))).dynamicInvoker(); 136 } 137 138 public static java.lang.invoke.CallSite bsm_Constant(MethodHandles.Lookup lookup, String name, MethodType type) { 139 synchronized (CallSiteStable.class) { 140 if (cs == null) 141 cs = new ConstantCallSite(doCallMH); 142 return cs; 143 } 144 } 145 146 public static java.lang.invoke.CallSite bsm_Mutable(MethodHandles.Lookup lookup, String name, MethodType type) { 147 synchronized (CallSiteStable.class) { 148 if (cs == null) 149 cs = new MutableCallSite(doCallMH); 150 return cs; 151 } 152 } 153 154 public static java.lang.invoke.CallSite bsm_Volatile(MethodHandles.Lookup lookup, String name, MethodType type) { 155 synchronized (CallSiteStable.class) { 156 if (cs == null) 157 cs = new VolatileCallSite(doCallMH); 158 return cs; 159 } 160 } 161 162 private static void shouldNotCallThis() { 163 // if this gets called, the transformation has not taken place 164 throw new AssertionError("this code should be statically transformed away by Indify"); 165 } 166 167 }