Place the MH in a static final, then the JIT can constant fold. You might find the following helpful:
https://wiki.openjdk.java.net/display/HotSpot/Method+handles+and+invokedynamic <https://wiki.openjdk.java.net/display/HotSpot/Method+handles+and+invokedynamic> https://wiki.openjdk.java.net/display/HotSpot/Deconstructing+MethodHandles <https://wiki.openjdk.java.net/display/HotSpot/Deconstructing+MethodHandles> Paul. > On Apr 9, 2018, at 2:06 PM, Ioi Lam <ioi....@oracle.com> wrote: > > Hi John, > > That was my suspicion and thanks for confirming it. > > I wrote a simple benchmark to try to compare the speed of a 'real' method > invocation vs MH.invokeExact > > http://cr.openjdk.java.net/~iklam/misc/method_handle_bench/BenchMH.java > <http://cr.openjdk.java.net/~iklam/misc/method_handle_bench/BenchMH.java> > > Basically > > private static void loopMH(int loops, MethodHandle mh) throws Throwable { > for (int i=0; i<loops; i++) { > mh.invokeExact("yo!"); > } > } > > -vs- > > > private static void loopDirect(int loops) { > for (int i=0; i<loops; i++) { > callme("yo!"); > } > } > > $ java -cp . BenchMH 1000000000 > ... > MH: loops = 1000000000: elapsed = 10332 ms > direct: loops = 1000000000: elapsed = 7500 ms > > So it seems like the JIT still can't handle such a simple case ... :-( > > > On 4/9/18 1:32 PM, John Rose wrote: >> On Apr 9, 2018, at 1:17 PM, Ioi Lam <ioi....@oracle.com >> <mailto:ioi....@oracle.com>> wrote: >>> >>> So why is this first argument not declared as a MethodHandle? >>> >> >> The short answer is to simplify the plumbing of lambda forms. >> >> Method handles are strongly typed but the underlying IR of >> lambda forms is weakly typed. This keeps the IR simple. >> To make up the difference, casts are inserted where necessary. >> Most (not all) uses of MHs are inlined and/or customized, >> in which case the JIT simply drops the casts. >> >> — John >> >> >> _______________________________________________ mlvm-dev mailing list >> mlvm-dev@openjdk.java.net <mailto:mlvm-dev@openjdk.java.net> >> http://mail.openjdk.java.net/mailman/listinfo/mlvm-dev >> <http://mail.openjdk.java.net/mailman/listinfo/mlvm-dev> > > _______________________________________________ > mlvm-dev mailing list > mlvm-dev@openjdk.java.net > http://mail.openjdk.java.net/mailman/listinfo/mlvm-dev
_______________________________________________ mlvm-dev mailing list mlvm-dev@openjdk.java.net http://mail.openjdk.java.net/mailman/listinfo/mlvm-dev