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