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