I have been reading this presentation about java8 lambdas implementation http://fr.slideshare.net/czechscala/java-8-under-the-hood
LambdaMetaFactory cont
invokedynamic is a bytecode operation used to call arbitrary method within JVM. The exact method to be called and executed is unknown at a compile-time. Instead it is computed by object implementing CallSite. Thus the dynamic in invokedynamic.
CallSite objects, as any other, have to be instantiated. Boostrap Method is a method which instantiates CallSite objects.
Each invokedynamic has a known bootstrap method given as its compile-time parameter. Whenever a invokedynamic is processed for a first time, appropriate bootstrap method is invoked. As result of boostrap method execution a CallSite object is created. This CallSite object is then cached and associated by JVM to a given invokedynamic operation. From now on, whenever particular invokedynamic call is to be executed, a cached CallSite instance is used to resolve called method.
Majority of boostrap methods are not written directly by end Java programmer. However that doesn't mean they are some rare obscure mechanism. They are created by javac compiler whenever particular java statements are used within source. String concatenation or lambda expression come to mind.
For example lambda expression could be implemented as inner classes. For matter of fact, lambdas are presented to programmers 'as shorthand' to using inner classes. However actual javac implementation, for performance reasons, avoids inner classes by generating lambda code under a static method and using invokedynamic to invoke this method.
For more direct, more impressive, usage of invokedynmaic I recommend Charles Nutter blog on how he optimizes JRubby calls sites with this mechanism. While writing RubyVM is not usual Java Programmer activity it is a really an eye opener on how to appropriately use ivokedynamic.
There are just two methods in the class: https://docs.oracle.com/javase/8/docs/api/java/lang/invoke/LambdaMetafactory.html
metafactory()
and altMetafactory()
. Both mention to be a "bootstrap method for invokedynamic call sites".
My understanding is that code which implements the handling of the invokedynamic
op code eventually uses one of the two when the target of the invocation is a lambda expression.
The term "bootstrapping" in this context means that it prepares everything necessary to actually execute the job later.