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.MethodHandle; 34 import java.lang.invoke.MethodHandles; 35 import java.lang.invoke.MethodType; 36 import java.util.concurrent.TimeUnit; 37 38 /** 39 * Benchmark assesses MethodHandles.guardWithTest() performance 40 */ 41 @BenchmarkMode(Mode.AverageTime) 42 @OutputTimeUnit(TimeUnit.NANOSECONDS) 43 @State(Scope.Thread) 44 public class MethodHandlesGuardWithTest { 45 46 /** 47 * Implementation notes: 48 * - using volatile ints as arguments to prevent opportunistic optimizations 49 * - using Integers to limit argument conversion costs 50 * - tested method should perform no worse than the baseline 51 */ 52 53 private MethodHandle mhWork1; 54 private MethodHandle mhWork2; 55 private MethodHandle guard; 56 private boolean choice; 57 58 private volatile Integer arg1 = 42; 59 private volatile Integer arg2 = 43; 60 private volatile Integer arg3 = 44; 61 62 @Setup 63 public void setup() throws Throwable { 64 MethodType mt = MethodType.methodType(int.class, Integer.class, Integer.class, Integer.class); 65 mhWork1 = MethodHandles.lookup().findVirtual(MethodHandlesGuardWithTest.class, "doWork1", mt); 66 mhWork2 = MethodHandles.lookup().findVirtual(MethodHandlesGuardWithTest.class, "doWork2", mt); 67 68 MethodHandle chooser = MethodHandles.lookup().findVirtual(MethodHandlesGuardWithTest.class, "chooser", MethodType.methodType(boolean.class)); 69 guard = MethodHandles.guardWithTest(chooser, mhWork1, mhWork2); 70 } 71 72 @Benchmark 73 public int baselineManual() throws Throwable { 74 if (choice) { 75 return (int) mhWork1.invokeExact(this, arg1, arg2, arg3); 76 } else { 77 return (int) mhWork2.invokeExact(this, arg1, arg2, arg3); 78 } 79 } 80 81 @Benchmark 82 public int testInvoke() throws Throwable { 83 return (int) guard.invoke(this, arg1, arg2, arg3); 84 } 85 86 public boolean chooser() { 87 choice = !choice; 88 return choice; 89 } 90 91 public int doWork1(Integer a, Integer b, Integer c) { 92 return 31*(31*(31*a + b) + c); 93 } 94 95 public int doWork2(Integer a, Integer b, Integer c) { 96 return 31*(31*(31*c + b) + a); 97 } 98 }