Why is "checkcast MethodHandle" necessary in methodHandleInvokeLinkerMethod?

2018-04-09 Thread Ioi Lam
I am looking at the code generated by MethodHandleNatives.linkMethod: (note the output was edited for brevity) $ cat HelloMH.java import java.lang.invoke.*; public class HelloMH {     public static void main(String ...args) throws Throwable {     MethodHandles.Lookup lookup = MethodHandles.

Re: Why is "checkcast MethodHandle" necessary in methodHandleInvokeLinkerMethod?

2018-04-09 Thread Attila Szegedi
invokeExact_MT000_LFL_V is a static method and its first declared argument type is java.lang.Object, not MethodHandle. So aload_0 will load a reference with static type of Object, hence making a checkcast necessary to cast it to a MethodHandle. HTH, Attila. > On 2018. Apr 9., at 21:49, Ioi L

Re: Why is "checkcast MethodHandle" necessary in methodHandleInvokeLinkerMethod?

2018-04-09 Thread Ioi Lam
So why is this first argument not declared as a MethodHandle? Thanks - Ioi On 4/9/18 1:00 PM, Attila Szegedi wrote: invokeExact_MT000_LFL_V is a static method and its first declared argument type is java.lang.Object, not MethodHandle. So aload_0 will load a reference with static type of Obj

Re: Why is "checkcast MethodHandle" necessary in methodHandleInvokeLinkerMethod?

2018-04-09 Thread John Rose
On Apr 9, 2018, at 1:17 PM, Ioi Lam 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 ma

Re: Why is "checkcast MethodHandle" necessary in methodHandleInvokeLinkerMethod?

2018-04-09 Thread Ioi Lam
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 Basically     private static void loopMH(int loops, Met

Re: Why is "checkcast MethodHandle" necessary in methodHandleInvokeLinkerMethod?

2018-04-09 Thread Paul Sandoz
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