Java 8 brings dynamic advantage

I am trying to verify that the dynamic call function is used http://blog.headius.com/2008/09/first-taste-of-invokedynamic.html

public class HelloWorld { public static void main(String[] args) { GreetingLambda lamda=()->System.out.println("Hello"); lamda.greet(); GreetingLambda lamda2=()->System.out.println("Hello"); lamda2.greet(); } } interface GreetingLambda{ void greet(); } 

Bytecode for the same

 public class HelloWorld { /* compiled from HelloWorld.java */ /* inner class */ /* public final static Lookup */ public HelloWorld() { /* L2 */ 0 aload_0; /* this */ 1 invokespecial 8; /* java.lang.Object() */ 4 return; } void print(GreetingLambda arg0) { /* L5 */ 0 aload_1; /* l */ 1 invokeinterface 16 1; /* void greet() */ /* L6 */ 6 return; } public static void main(java.lang.String[] args) { /* L10 */ 0 invokedynamic 27; /* GreetingLambda greet() */ 3 nop; 4 nop; 5 astore_1; /* lamda */ /* L11 */ 6 aload_1; /* lamda */ 7 invokeinterface 16 1; /* void greet() */ /* L12 */ 12 invokedynamic 28; /* GreetingLambda greet() */ 15 nop; 16 nop; 17 astore_2; /* lamda2 */ /* L13 */ 18 aload_2; /* lamda2 */ 19 invokeinterface 16 1; /* void greet() */ /* L17 */ 24 return; } private static void lambda$0() { /* L10 */ 0 getstatic 34; /* java.lang.System.out */ 3 ldc 40; /* "Hello" */ 5 invokevirtual 42; /* void println(java.lang.String arg0) */ 8 return; } private static void lambda$1() { /* L12 */ 0 getstatic 34; /* java.lang.System.out */ 3 ldc 40; /* "Hello" */ 5 invokevirtual 42; /* void println(java.lang.String arg0) */ 8 return; } } 

The call to the lambda expression is replaced by invokedynamic . But I can’t understand what is the advantage of using this approach.

  • As invokedynamic is useful or better than generating an anonymous class.
  • how it differs from invokeinterface, which is generated from under the code, since it also performs a validation check.

      List<String> a = new ArrayList<String>() ; 
+6
source share
2 answers

For the first question:

Simply put: invokedynamic has been very carefully designed (so it took so long to get it in Java); and one of the side effects of this: extremely effective performance.

See here for further reading. Quote from there:

invokedynamic also benefits dynamic language developers by supporting dynamically changing site targets β€” the call site, or rather, the dynamic call site β€” is invokedynamic. Furthermore, since the JVM internally supports invokedynamic, this command can be better optimized by the JIT compiler.

So, a long story: at least theoretically, when you can write code that uses invokedynamic instead of some "old school", a way to solve the same problem, a new invokedynamic solution has certain chances to be "faster". Like in: faster to use invokedynamic compared to the old way of creating an anonymous inner class and use it.

+2
source

There are many more articles on jruby optimization in the same blog you are citing. For example, here , Charles Nutter discusses the benefits of invokedynamic.

In short, it’s an optimization that allows jvm to cache and optimize something that would otherwise be useless for finding and accessing a method. An added benefit is that the resulting code is friendlier for the access point optimizer, which means that many optimizations that work with Java bytecode now also work for jruby and other jvm languages ​​that use invokedynamic.

This function was added specifically for jvm to support dynamic languages, where the exact code for binding processing methods is not known until runtime (unlike static languages).

+2
source

Source: https://habr.com/ru/post/1013911/


All Articles