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.State;
  31 
  32 import java.lang.invoke.ConstantCallSite;
  33 import java.lang.invoke.MethodHandle;
  34 import java.lang.invoke.MethodHandles;
  35 import java.lang.invoke.MethodType;
  36 import java.lang.invoke.MutableCallSite;
  37 import java.lang.invoke.VolatileCallSite;
  38 import java.util.concurrent.TimeUnit;
  39 
  40 /**
  41  * This benchmark evaluates INDY performance when call sites are not changed.
  42  */
  43 @BenchmarkMode(Mode.AverageTime)
  44 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  45 @State(Scope.Thread)
  46 public class CallSiteStable {
  47 
  48     /*
  49      * Implementation notes:
  50      *   - Test is calling simple method via INDY
  51      *   - Additional baseline includes "raw" test, calling target method directly in virtual and static modes
  52      */
  53 
  54     private static java.lang.invoke.CallSite cs;
  55 
  56     private static MethodHandle doCallMH;
  57 
  58     static {
  59         try {
  60             doCallMH = MethodHandles.lookup().findVirtual(CallSiteStable.class, "doCall", MethodType.methodType(int.class, int.class));
  61         } catch (NoSuchMethodException | IllegalAccessException e) {
  62             throw new IllegalStateException(e);
  63         }
  64     }
  65 
  66     private int i;
  67 
  68     public int doCall(int value) {
  69         return value + 1;
  70     }
  71 
  72     public static int doCallStatic(int value) {
  73         return value + 1;
  74     }
  75 
  76     @Benchmark
  77     public void baselineVirtual() {
  78         i = doCall(i);
  79     }
  80 
  81     @Benchmark
  82     public void baselineStatic() {
  83         i = doCallStatic(i);
  84     }
  85 
  86     @Benchmark
  87     public void testConstant() throws Throwable {
  88         i = (int) INDY_Constant().invokeExact(this, i);
  89     }
  90 
  91     @Benchmark
  92     public void testMutable() throws Throwable {
  93         i = (int) INDY_Mutable().invokeExact(this, i);
  94     }
  95 
  96     @Benchmark
  97     public void testVolatile() throws Throwable {
  98         i = (int) INDY_Volatile().invokeExact(this, i);
  99     }
 100 
 101     /* =========================== INDY TRAMPOLINES ============================== */
 102 
 103     private static MethodType MT_bsm() {
 104         shouldNotCallThis();
 105         return MethodType.methodType(java.lang.invoke.CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);
 106     }
 107 
 108     private static MethodHandle MH_bsm_Constant() throws ReflectiveOperationException {
 109         shouldNotCallThis();
 110         return MethodHandles.lookup().findStatic(MethodHandles.lookup().lookupClass(), "bsm_Constant", MT_bsm());
 111     }
 112 
 113     private static MethodHandle MH_bsm_Mutable() throws ReflectiveOperationException {
 114         shouldNotCallThis();
 115         return MethodHandles.lookup().findStatic(MethodHandles.lookup().lookupClass(), "bsm_Mutable", MT_bsm());
 116     }
 117 
 118     private static MethodHandle MH_bsm_Volatile() throws ReflectiveOperationException {
 119         shouldNotCallThis();
 120         return MethodHandles.lookup().findStatic(MethodHandles.lookup().lookupClass(), "bsm_Volatile", MT_bsm());
 121     }
 122 
 123     private static MethodHandle INDY_Constant() throws Throwable {
 124         shouldNotCallThis();
 125         return ((java.lang.invoke.CallSite) MH_bsm_Constant().invoke(MethodHandles.lookup(), "doCall", MethodType.methodType(int.class, CallSiteStable.class, int.class))).dynamicInvoker();
 126     }
 127     private static MethodHandle INDY_Mutable() throws Throwable {
 128         shouldNotCallThis();
 129         return ((java.lang.invoke.CallSite) MH_bsm_Mutable().invoke(MethodHandles.lookup(), "doCall", MethodType.methodType(int.class, CallSiteStable.class, int.class))).dynamicInvoker();
 130     }
 131     private static MethodHandle INDY_Volatile() throws Throwable {
 132         shouldNotCallThis();
 133         return ((java.lang.invoke.CallSite) MH_bsm_Volatile().invoke(MethodHandles.lookup(), "doCall", MethodType.methodType(int.class, CallSiteStable.class, int.class))).dynamicInvoker();
 134     }
 135 
 136     public static java.lang.invoke.CallSite bsm_Constant(MethodHandles.Lookup lookup, String name, MethodType type) {
 137         synchronized (CallSiteStable.class) {
 138             if (cs == null)
 139                 cs = new ConstantCallSite(doCallMH);
 140             return cs;
 141         }
 142     }
 143 
 144     public static java.lang.invoke.CallSite bsm_Mutable(MethodHandles.Lookup lookup, String name, MethodType type) {
 145         synchronized (CallSiteStable.class) {
 146             if (cs == null)
 147                 cs = new MutableCallSite(doCallMH);
 148             return cs;
 149         }
 150     }
 151 
 152     public static java.lang.invoke.CallSite bsm_Volatile(MethodHandles.Lookup lookup, String name, MethodType type) {
 153         synchronized (CallSiteStable.class) {
 154             if (cs == null)
 155                 cs = new VolatileCallSite(doCallMH);
 156             return cs;
 157         }
 158     }
 159 
 160     private static void shouldNotCallThis() {
 161         // if this gets called, the transformation has not taken place
 162         throw new AssertionError("this code should be statically transformed away by Indify");
 163     }
 164 
 165 }