Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Compiling Lambda Expressions: Scala vs Java 8
Takipi www.takipi.com
Introduction
• Most modern languages adopted Lambda expression as a
fundamental part of functional programming
• JVM based...
Sample Expression
A simple Lambda expression that converts a list of Strings to a list of
their lengths:
• In Java –
• In ...
Let’s start with Scala
Takipi
www.takipi.com
The Code
Bytecode contents of the .class produced by the Scala compiler
This is what
the JVM will
actually
execute
Takipi
...
Cont.
• A new instance of a synthetic class
is created and initialized
• JVM’s assumes that this object
holds the Lambda m...
The Lambda Object
• The Lambda class is derived
from scala.runtime.AbstractFunction1.The map() function
polymorphically in...
Output
A lot of bytecode is generated
• an additional class and
• a bunch of new methods.
• It shows the complexity behind...
Contd.
• We were expecting Java 8 to implement this the same way, but were
quite surprised to see that they took another a...
Java 8 – A new approach
Takipi
www.takipi.com
The Code
• The bytecode here is a bit shorter
• Begins by loading the names var and invokes its .stream() method
• It uses...
Cont.
• Dynamic Linking
invokedynamic () stores name and signature of the Lambda in a
separate table in the .class to whic...
The Lambda Code
• This is the code for the actual Lambda expression
Advantages :
• Compiled as a static function to avoid ...
Summary
• It’s fascinating to see how Java, the most “strict” of modern
languages is now using dynamic linkage to power it...
Originally Posted on
Here is
the link
Takipi Blog
Takipi
www.takipi.com
Upcoming SlideShare
Loading in …5
×

Compiling Lambda Expressions: Scala vs. Java 8

40,525 views

Published on

We compared the Scala and Java compilers implement Lambda expressions and results were pretty surprising!!!

Published in: Software
  • Be the first to comment

Compiling Lambda Expressions: Scala vs. Java 8

  1. 1. Compiling Lambda Expressions: Scala vs Java 8 Takipi www.takipi.com
  2. 2. Introduction • Most modern languages adopted Lambda expression as a fundamental part of functional programming • JVM based languages such as Scala, Groovy and Clojure and recently Java 8 have integrated them as key part of the language • What’s so interesting about Lambda expressions? • Completely invisible from the JVM’s perspective • JVM only knows bytecode which is a strict OO specification • We compared the Scala and Java compilers implement Lambda expressions and results were pretty surprising! Takipi www.takipi.com www.takipi.com
  3. 3. Sample Expression A simple Lambda expression that converts a list of Strings to a list of their lengths: • In Java – • In Scala – Don’t be tricked by its simplicity – behind the scenes some complex stuff is going on. Takipi www.takipi.com
  4. 4. Let’s start with Scala Takipi www.takipi.com
  5. 5. The Code Bytecode contents of the .class produced by the Scala compiler This is what the JVM will actually execute Takipi www.takipi.com
  6. 6. Cont. • A new instance of a synthetic class is created and initialized • JVM’s assumes that this object holds the Lambda method what’s going on inside that Lambda object? Note : Lambda is defined as an integral part of method but it resides completely outside of class Takipi www.takipi.com
  7. 7. The Lambda Object • The Lambda class is derived from scala.runtime.AbstractFunction1.The map() function polymorphically invokes the overridden apply() whose code is below – • Method .length() gets executed automatically Takipi www.takipi.com
  8. 8. Output A lot of bytecode is generated • an additional class and • a bunch of new methods. • It shows the complexity behind these constructs Phew… it was a quite long way to get here  Takipi www.takipi.com
  9. 9. Contd. • We were expecting Java 8 to implement this the same way, but were quite surprised to see that they took another approach completely Takipi www.takipi.com
  10. 10. Java 8 – A new approach Takipi www.takipi.com
  11. 11. The Code • The bytecode here is a bit shorter • Begins by loading the names var and invokes its .stream() method • It uses the new invokeDynamic instruction Takipi www.takipi.com
  12. 12. Cont. • Dynamic Linking invokedynamic () stores name and signature of the Lambda in a separate table in the .class to which the #0 parameter passed to the instruction points This new table changed and improved the structure of the bytecode specification Takipi www.takipi.com
  13. 13. The Lambda Code • This is the code for the actual Lambda expression Advantages : • Compiled as a static function to avoid passing an additional this object like we saw in Scala • invokedynamic approach allows us to invoke the method which is polymorphic from the .map() function’s perspective without allocating a wrapper object or invoke a virtual override method Takipi www.takipi.com
  14. 14. Summary • It’s fascinating to see how Java, the most “strict” of modern languages is now using dynamic linkage to power its new Lambda expressions. It’s also an efficient approach, as no additional class loading and compilation is needed – the Lambda method is simply another private method in our class • Java 8 has really done a very elegant job in using new technology introduced in Java 7 to implement Lambda expressions in what is a very straightforward way. It’s pleasant in a way to see that even a “venerable” lady such as Java can teach us all some new tricks Takipi www.takipi.com
  15. 15. Originally Posted on Here is the link Takipi Blog Takipi www.takipi.com

×