# Wednesday, 21 May 2008
« New Development Snapshot | Main | Invokedynamic Proof of Concept Part 2 »
Invokedynamic Proof of Concept

A few days ago the JSR 292 expert group released an early draft review for the invokedynamic instruction.

To investigate how this would work out for IKVM, I cooked up a proof of concept in C#.

class InvokeDynamicPoC
  private static InvokeDynamic<object, CallSite, object, object[]> __bootstrapMethod =
  private static CallSiteImpl<InvokeDynamicVoid<string, int, int>> __site1 =
    new CallSiteImpl<InvokeDynamicVoid<string, int, int>>(
      new StaticContextImpl(typeof(InvokeDynamicPoC), "printSubstring"));

  private static void Main()
    for (int j = 0; j < 2; j++)
      int start = Environment.TickCount;
      for (int i = 0; i < 10/*0000000/**/; i++)
        string obj = "invokedynamic";
        int arg1 = 3;
        int arg2 = 2;
        MethodHandleImpl<InvokeDynamicVoid<string, int, int>> mh = __site1.mh;
        if (mh == null)
          __bootstrapMethod(__site1, obj, new object[] { java.lang.Integer.valueOf(arg1),
                                                         java.lang.Integer.valueOf(arg2) });
          mh.d(obj, arg1, arg2);
      int end = Environment.TickCount;
      Console.WriteLine(end - start);

  private static void printSubstring(string s, int startIndex, int length)
    Console.WriteLine(s.Substring(startIndex, length));

  private static object bootstrapInvokeDynamic(CallSite cs, object receiver, object[] arguments)
    java.lang.Class c = typeof(InvokeDynamicPoC);
    java.lang.reflect.Method m = c.getDeclaredMethod(cs.getStaticContext().getName(),
                                                     typeof(string), typeof(int), typeof(int));
    MethodHandle mh = MethodHandles.unreflect(m);
    return m.invoke(null, receiver, arguments[0], arguments[1]);

The full (compilable and working) source is available here.

Wednesday, 21 May 2008 09:56:49 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
Home page

I apologize for the lameness of this, but the comment spam was driving me nuts. In order to be able to post a comment, you need to answer a simple question. Hopefully this question is easy enough not to annoy serious commenters, but hard enough to keep the spammers away.

Anti-Spam Question: What method on java.lang.System returns an object's original hashcode (i.e. the one that would be returned by java.lang.Object.hashCode() if it wasn't overridden)? (case is significant)

Comment (HTML not allowed)  

Live Comment Preview