Re: Why is "checkcast MethodHandle" necessary in methodHandleInvokeLinkerMethod?
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, MethodHandle mh) throws Throwable { for (int i=0; i> 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 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
Re: Why is "checkcast MethodHandle" necessary in methodHandleInvokeLinkerMethod?
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 Object, hence making a checkcast necessary to cast it to a MethodHandle. HTH, Attila. On 2018. Apr 9., at 21:49, Ioi Lamwrote: 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.lookup(); MethodType mt = MethodType.methodType(void.class, float.class); MethodHandle mh = lookup.findStatic(HelloMH.class, "callme", mt); mh.invokeExact(4.0f); } private static void callme(float x) { System.out.println("Hello MH.invoke: " + x); Thread.dumpStack(); } } $ javac HelloMH.java $ java -Djava.lang.invoke.MethodHandle.DUMP_CLASS_FILES=true \ -XX:+UnlockDiagnosticVMOptions -XX:+ShowHiddenFrames -cp . HelloMH [...] Hello MH.invoke: 4.0 java.lang.Exception: Stack trace at java.base/java.lang.Thread.dumpStack(Thread.java:1434) at HelloMH.callme(HelloMH.java:13) at java.base/java.lang.invoke.LambdaForm$DM.invokeStatic_LF_V() at java.base/java.lang.invoke.LambdaForm$MH.invokeExact_MT_LFL_V() at HelloMH.main(HelloMH.java:8) $ javap -c 'DUMP_CLASS_FILES/java/lang/invoke/LambdaForm$MH.class' final class java.lang.invoke.LambdaForm$MH { static void invokeExact_MT000_LFL_V(java.lang.Object, float, java.lang.Object); Code: 0: aload_0 1: checkcast #14// class java/lang/invoke/MethodHandle 4: dup ... Why is the checkcast necessary here? I thought the verifier would ensure that local#0 must of type MethodHandle. Is this checkcast needed only for reflective invocation of MethodHandle.invokeExact()? Thanks - Ioi ___ 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 ___ mlvm-dev mailing list mlvm-dev@openjdk.java.net http://mail.openjdk.java.net/mailman/listinfo/mlvm-dev
Re: Why is "checkcast MethodHandle" necessary in methodHandleInvokeLinkerMethod?
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 Lamwrote: > > 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.lookup(); > MethodType mt = MethodType.methodType(void.class, float.class); > MethodHandle mh = lookup.findStatic(HelloMH.class, "callme", mt); > mh.invokeExact(4.0f); > } > > private static void callme(float x) { > System.out.println("Hello MH.invoke: " + x); > Thread.dumpStack(); > } > } > > $ javac HelloMH.java > > $ java -Djava.lang.invoke.MethodHandle.DUMP_CLASS_FILES=true \ > -XX:+UnlockDiagnosticVMOptions -XX:+ShowHiddenFrames -cp . HelloMH > [...] > Hello MH.invoke: 4.0 > java.lang.Exception: Stack trace > at java.base/java.lang.Thread.dumpStack(Thread.java:1434) > at HelloMH.callme(HelloMH.java:13) > at java.base/java.lang.invoke.LambdaForm$DM.invokeStatic_LF_V() > at java.base/java.lang.invoke.LambdaForm$MH.invokeExact_MT_LFL_V() > at HelloMH.main(HelloMH.java:8) > > > $ javap -c 'DUMP_CLASS_FILES/java/lang/invoke/LambdaForm$MH.class' > final class java.lang.invoke.LambdaForm$MH { > static void invokeExact_MT000_LFL_V(java.lang.Object, float, > java.lang.Object); > Code: >0: aload_0 >1: checkcast #14// class java/lang/invoke/MethodHandle >4: dup >... > > > Why is the checkcast necessary here? > > I thought the verifier would ensure that local#0 must of type MethodHandle. > > Is this checkcast needed only for reflective invocation of > MethodHandle.invokeExact()? > > Thanks > - Ioi > > ___ > 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