Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of Scala emitting bytecode for the JVM 1.7

As per Scala 2.10, what are the advantages (if any) of emitting bytecode for the JVM 1.7, when compared to the default of emitting for the 1.6?

like image 299
Hugo Sereno Ferreira Avatar asked Jan 11 '13 20:01

Hugo Sereno Ferreira


People also ask

How Scala works on JVM?

Scala is compiled into Java Byte Code which is executed by the Java Virtual Machine (JVM). This means that Scala and Java have a common runtime platform. If you or your organization has standardized on Java, Scala is not going to be a total stranger in your architecture. It's a different language, but the same runtime.

Can Scala be run only on JVM?

Can I run my scala program without JVM using scala-native? Yes, the goal of Scala Native is to support compilation of Scala programs without any kind of VM.

Does Scala code compile to Java?

Although Scala compiles to Java bytecode, it is designed to improve on many of the perceived shortcomings of the Java language.

Which is byte code in Java?

What Is the Bytecode? Bytecode is the intermediate representation of a Java program, allowing a JVM to translate a program into machine-level assembly instructions. When a Java program is compiled, bytecode is generated in the form of a . class file.


1 Answers

Previous Scala versions emitted version 49.0 of the byte code, which corresponded to Java 5. With Scala 2.10 version the default was changed to version 50.0, corresponding to Java 6 and which has the principal advantage of activating the faster verifier introduced with that version, so it should lead to (slightly) better run time performance.

As you note, with 2.10 it became possible to emit version 51.0 byte code, which corresponds to Java 7. There are a few differences between version 50.0 and version 51.0: the biggest is the inclusion of the invokedynamic instruction, with the plumbing that goes with it (see the class file format definition for the gory details).

As far as Scala usage of the 51.0 byte code goes, even though the technical parts are in place it is my understanding that work is still at an experimental stage in using this feature. See this EPFL presentation and this thread which shows that the team is working on getting the performance benefits of method handles without having to introduce a dependency on Java 7.

Scala 2.11 maintained the default of emitting version 50.0 bytecode, but the official plan is now to jump straight to Java 8 bytecode with Scala 2.12. In the meantime, there is a new back-end available for Scala 2.11 that will let you try out some of the features that are being prototyped for Scala 2.12, and which will become the default back-end with Scala 2.12.

Anyway, the long-awaited proposed benefits all come from using the invokedynamic bytecode (and its associated MethodHandle structures). They include:

  • reducing code size and execution time for closures
  • improving specialization of generic datatypes
  • potentially faster structural types

(Spoiler: Using MethodHandles to implement closures in the experimental backend is currently slower than the present optimised closure creation!)

like image 54
rxg Avatar answered Oct 03 '22 23:10

rxg