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 =
bootstrapInvokeDynamic;
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) });
}
else
{
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);
cs.setTarget(mh);
return m.invoke(null, receiver, arguments[0], arguments[1]);
}
}
The full (compilable and working) source is available
here.