Recently, I have been working with low-level Java, modifying the JVM bytecode and generating Class Files without compiling .java files. I just learned about invokedynamic, and I was able to generate my own InvokeDynamic constants and BootstrapMethod attributes using my own proprietary JVM bytecode tool to allow me to use invokedynamic properly.
After this, I made simple timing script in java that tests both invokestatic and invokedynamic on the same method. However, my script showed that invokedynamic was about 8% faster than invokestatic, something that I thought would be impossible.
Here is the code that I used to time each method:
import java.lang.invoke.*;
public class InvokeDynamicTest implements Runnable {
private static void doNothing(){
//do nothing
}
private static CallSite getCallSite(MethodHandles.Lookup s, String name, MethodType methodType){
try{
return new ConstantCallSite(s.findStatic(InvokeDynamicTest.class, name, methodType));
}catch(Throwable t){
throw new RuntimeException(t);
}
}
public static void main(String[] args) {
final Runnable r = (Runnable) new InvokeDynamicTest();
long time = System.currentTimeMillis();
int counter = 0;
while(System.currentTimeMillis() - time < 5000){
doNothing();
counter++;
}
System.out.println(counter);
time = System.currentTimeMillis();
counter = 0;
while(System.currentTimeMillis() - time < 5000){
r.run();
counter++;
}
System.out.println(counter);
}
public void run() {
//Use bytecode modifier to insert invokedynamic
}
}
Here is the output:
514048300
545970671
As for the modified bytecode after compilation, here are the new constants as dumped by javap:
#9 = Class #10 // InvokeDynamicTest
#10 = Utf8 InvokeDynamicTest
...
#70 = Utf8 getCallSite
#71 = Utf8 (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
#72 = NameAndType #70:#71 // getCallSite:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
#73 = Methodref #9.#72 // InvokeDynamicTest.getCallSite:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
#74 = MethodHandle 6:#73 // REF_invokeStatic InvokeDynamicTest.getCallSite:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
#75 = Utf8 doNothing
#76 = Utf8 ()V
#77 = NameAndType #75:#76 // doNothing:()V
#78 = InvokeDynamic #0:#77 // #0:doNothing:()V
Here is the new code for the run method:
public void run();
descriptor: ()V
flags: (0x0001) ACC_PUBLIC
Code:
stack=100, locals=100, args_size=1
0: invokedynamic #78, 0 // InvokeDynamic #0:doNothing:()V
5: return
Here is the BootstrapMethods table:
BootstrapMethods:
0: #74 REF_invokeStatic InvokeDynamicTest.getCallSite:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
Method arguments:
Am I missing something? I cannot possibly see how invokedynamic could be faster than invokestatic