Goal:
Improve start-up time by storing pre-generated LambdaForm handlers in an application-specific CDS archive.
Background:
In JDK-8086045 Improve the java.lang.invoke first initialization costs, the JDK can reduce the number of dynamically generated LambdaForm classes. Here's an example:
# Do this in your JDK build directory. # The exploded JDK build: $ jdk/bin/javac -J-verbose ~/tmp/HelloWorld.java | grep LambdaForm | grep __JVM_LookupDefineClass__ [0.142s][info][class,load] java.lang.invoke.LambdaForm$MH/0x0000000800066c40 source: __JVM_LookupDefineClass__ [0.143s][info][class,load] java.lang.invoke.LambdaForm$DMH/0x0000000800066040 source: __JVM_LookupDefineClass__ [0.143s][info][class,load] java.lang.invoke.LambdaForm$MH/0x0000000800066440 source: __JVM_LookupDefineClass__ [snip] $ jdk/bin/javac -J-verbose ~/tmp/HelloWorld.java | grep LambdaForm | grep __JVM_LookupDefineClass__ | wc 102 408 11400 # The final JDK image: $ images/jdk/bin/javac -J-verbose ~/tmp/HelloWorld.java | grep LambdaForm | grep __JVM_LookupDefineClass__ | wc 34 136 3800
Here's the difference between the exploded build and the final image:
$ jdk/bin/javap 'java.lang.invoke.DirectMethodHandle$Holder' Compiled from "DirectMethodHandle.java" final class java.lang.invoke.DirectMethodHandle$Holder { final java.lang.invoke.DirectMethodHandle this$0; java.lang.invoke.DirectMethodHandle$Holder(java.lang.invoke.DirectMethodHandle); } $ images/jdk/bin/javap 'java.lang.invoke.DirectMethodHandle$Holder' Compiled from "DirectMethodHandle$Holder" final class java.lang.invoke.DirectMethodHandle$Holder { static int invokeInterface(java.lang.Object, java.lang.Object, java.lang.Object); static java.lang.Object invokeSpecial(java.lang.Object, java.lang.Object, java.lang.Object, int); static java.lang.Object invokeSpecial(java.lang.Object, java.lang.Object, java.lang.Object, long); [~150 more lines ...] }
The JDK image's version of the java.lang.invoke.DirectMethodHandle$Holder class is generated here in GenerateJLIClassesPlugin.java. This plugin is executed when we generate the file images/jdk/lib/modules.
private static final String DIRECT_HOLDER = "java/lang/invoke/DirectMethodHandle$Holder"; ... byte[] bytes = JLIA.generateDirectMethodHandleHolderClassBytes( DIRECT_HOLDER, directMethodTypes, dmhTypes);