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.Setup;
  33 import org.openjdk.jmh.annotations.State;
  34 
  35 import java.lang.invoke.MethodHandle;
  36 import java.lang.invoke.MethodHandles;
  37 import java.lang.invoke.MethodType;
  38 import java.util.concurrent.TimeUnit;
  39 
  40 /**
  41  * Benchmark assesses MethodHandles.catchException() performance
  42  */
  43 @BenchmarkMode(Mode.AverageTime)
  44 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  45 @State(Scope.Thread)
  46 public class MethodHandlesCatchException {
  47 
  48     /**
  49      * Implementation notes:
  50      *   - emulating instance method handles because of current issue with instance methods
  51      *       (http://monaco.us.oracle.com/detail.jsf?cr=7179751)
  52      *   - exception is cached to harness the MH code, not exception instantiation
  53      *   - measuring two modes: a) always going through normal code path; b) always going through exceptional one
  54      *   - baselines do the same thing in pure Java
  55      */
  56 
  57     private static final MyException MY_EXCEPTION = new MyException();
  58 
  59     private int i1;
  60     private int i2;
  61 
  62     private static MethodHandle methNormal;
  63     private static MethodHandle methExceptional;
  64 
  65     @Setup
  66     public void setup() throws Throwable {
  67         MethodHandle bodyNormal = MethodHandles.lookup().findStatic(MethodHandlesCatchException.class, "doWorkNormal", MethodType.methodType(void.class, MethodHandlesCatchException.class));
  68         MethodHandle bodyExceptional = MethodHandles.lookup().findStatic(MethodHandlesCatchException.class, "doWorkExceptional", MethodType.methodType(void.class, MethodHandlesCatchException.class));
  69         MethodHandle fallback = MethodHandles.lookup().findStatic(MethodHandlesCatchException.class, "fallback", MethodType.methodType(void.class, MyException.class, MethodHandlesCatchException.class));
  70 
  71         methNormal = MethodHandles.catchException(bodyNormal, MyException.class, fallback);
  72         methExceptional = MethodHandles.catchException(bodyExceptional, MyException.class, fallback);
  73     }
  74 
  75     @Benchmark
  76     public void baselineNormal() {
  77         try {
  78             doWorkNormal(this);
  79         } catch (MyException e) {
  80             fallback(e, this);
  81         }
  82     }
  83 
  84     @Benchmark
  85     public void baselineExceptional() {
  86         try {
  87             doWorkExceptional(this);
  88         } catch (MyException e) {
  89             fallback(e, this);
  90         }
  91     }
  92 
  93     @Benchmark
  94     public void testNormal() throws Throwable {
  95         methNormal.invokeExact(this);
  96     }
  97 
  98     @Benchmark
  99     public void testExceptional() throws Throwable {
 100         methExceptional.invokeExact(this);
 101     }
 102 
 103 
 104     public static void doWorkNormal(MethodHandlesCatchException inst) throws MyException {
 105         inst.i1++;
 106     }
 107 
 108     public static void doWorkExceptional(MethodHandlesCatchException inst) throws MyException {
 109         inst.i1++;
 110         throw MY_EXCEPTION;
 111     }
 112 
 113     public static void fallback(MyException ex, MethodHandlesCatchException inst) {
 114         inst.i2++;
 115     }
 116 
 117     public static class MyException extends Exception {
 118 
 119     }
 120 
 121 }