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. 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.java.lang.invoke; 24 25 import org.openjdk.jmh.annotations.Benchmark; 26 import org.openjdk.jmh.annotations.BenchmarkMode; 27 import org.openjdk.jmh.annotations.Mode; 28 import org.openjdk.jmh.annotations.OutputTimeUnit; 29 import org.openjdk.jmh.annotations.Scope; 30 import org.openjdk.jmh.annotations.Setup; 31 import org.openjdk.jmh.annotations.State; 32 33 import java.lang.invoke.CallSite; 34 import java.lang.invoke.MethodHandle; 35 import java.lang.invoke.MethodHandles; 36 import java.lang.invoke.MethodType; 37 import java.lang.invoke.MutableCallSite; 38 import java.lang.invoke.SwitchPoint; 39 import java.util.concurrent.TimeUnit; 40 41 /** 42 * Benchmark assessing SwitchPoint performance. 43 */ 44 @BenchmarkMode(Mode.AverageTime) 45 @OutputTimeUnit(TimeUnit.NANOSECONDS) 46 @State(Scope.Thread) 47 public class SwitchPointAdhoc { 48 49 /* 50 * Implementation notes: 51 * - this test asserts SwitchPoint performance in both valid and invalid cases 52 * - this test does not assert invalidation performance (hard to do with irreversible SwitchPoint) 53 * - raw baseline gives the idea for MethodHandle invocation cost 54 * - CS baseline gives the idea for additional dereference cost 55 */ 56 57 private MethodHandle body1, body2; 58 private int i; 59 private java.lang.invoke.SwitchPoint sw1, sw2; 60 private CallSite cs; 61 62 @Setup 63 public void setup() throws NoSuchMethodException, IllegalAccessException { 64 sw1 = new java.lang.invoke.SwitchPoint(); 65 sw2 = new java.lang.invoke.SwitchPoint(); 66 SwitchPoint.invalidateAll(new SwitchPoint[]{sw2}); 67 body1 = MethodHandles.lookup().findVirtual(SwitchPointAdhoc.class, "body1", MethodType.methodType(int.class, int.class)); 68 body2 = MethodHandles.lookup().findVirtual(SwitchPointAdhoc.class, "body2", MethodType.methodType(int.class, int.class)); 69 cs = new MutableCallSite(body1); 70 } 71 72 @Benchmark 73 public void baselineRaw() throws Throwable { 74 i = (int) body1.invoke(this, i); 75 } 76 77 @Benchmark 78 public void baselineCS() throws Throwable { 79 i = (int) cs.getTarget().invoke(this, i); 80 } 81 82 @Benchmark 83 public void testValid() throws Throwable { 84 MethodHandle handle = sw1.guardWithTest(body1, body2); 85 i = (int) handle.invoke(this, i); 86 } 87 88 @Benchmark 89 public void testInvalid() throws Throwable { 90 MethodHandle handle = sw2.guardWithTest(body1, body2); 91 i = (int) handle.invoke(this, i); 92 } 93 94 public int body1(int i) { 95 return i + 1; 96 } 97 98 public int body2(int i) { 99 return i + 1; 100 } 101 102 103 }