Re:An Opinionated Guide to Modern Java Development, Part 1
by spullara on Thursday May 01, 2014 @11:52PM
(#7684047)
I don't think that person understands how it works at all.
Re:An Opinionated Guide to Modern Java Development, Part 1
by llemiengre on Friday May 02, 2014 @03:48AM
(#7684539)
He knows what he is talking about & his description matches the explanation by Brian Goetz.
So unless I'm missing something, please be specific.
Re:An Opinionated Guide to Modern Java Development, Part 1
by spullara on Friday May 02, 2014 @11:44AM
(#7686361)
Well, for one, Lambdas are desugared into methods, not anonymous classes. Early on they used anonymous classes because it was convenient but that wasn't the final translation strategy.
http://cr.openjdk.java.net/~briangoetz/lambda/lambda-transla...
Re:An Opinionated Guide to Modern Java Development, Part 1
by georgewfraser on Friday May 02, 2014 @12:53PM
(#7686782)
My impression from the openjdk code is that each lambda generates:
1. A method in its parent class. 2. An invokedynamic instruction at the call site.
The invokedynamic instruction calls LambdaMetafactory, which compiles an anonymous class at runtime that calls method #1. So the only benefit of using invokedynamic is fewer class files, by deferring generating them until runtime.
Re:An Opinionated Guide to Modern Java Development, Part 1
by pron on Friday May 02, 2014 @01:41PM
(#7687040)
Non-capturing lambdas also don't instantiate a new object at each call site. But this is also doable without invokedynamic.
Re:An Opinionated Guide to Modern Java Development, Part 1
by frowaway001 on Friday May 02, 2014 @12:46PM
(#7686735)
Yes, and these methods create an anonymous class at runtime. What's so hard to understand about this?
Re:An Opinionated Guide to Modern Java Development, Part 1
by spullara on Friday May 02, 2014 @07:39PM
(#7688805)
It was a misunderstanding on my part about where they were claiming the anonymous class was generated. Early versions of Lambda were nothing more than sugar on top of anonymous inner classes and the claim above sounded very similar to that. However, I see that inside the LambdaMetaFactory anonymous classes are generated based on the call site. Probably the biggest difference is that Java 8 can avoid an object allocation when the lambda doesn't capture. Not sure if Scala supports this but I'm sure that it could do it.
"However, the key thing about invokedynamic is that it's essentially a JVM-level macro that defers lambda translation strategy to LambdaMetaFactory which is a library class. If Java 9 or 10 gets more direct (and performant) support for lambdas that won't require classes and object allocations then it can swap implementation of LambdaMetaFactory and all _existing_ code written for Java 8 will get performance boost. That is the brilliance of using invokedynamic in context of translating lambdas. We'll have to wait for future versions of Java to experience those benefits, though."