Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good reference for how scala is mapped to jvm bytecode

Are there any good writeups of how the Scala compiler maps various Scala features to bytecode?

A quick google turned up the supporting material from David Pollak's 2009 talk

https://github.com/dpp/jvm_summit_2009/blob/master/scala_fancy_pants.pdf

But I suspect this may be both dated and incomplete.

I could try and gather this data myself via scalap javap, but it would be nice to benefit from someone elses effort and insight.

like image 907
henry Avatar asked Apr 08 '13 19:04

henry


2 Answers

on the Scala REPL, use :javap -c to see the generated bytecode.

For instance:

scala> class Bytes { def a = println("hello") }
defined class Bytes

scala> :javap -c Bytes
Compiled from "<console>"
public class Bytes extends java.lang.Object{
public void a();
  Code:
   0:   getstatic   #13; //Field scala/Predef$.MODULE$:Lscala/Predef$;
   3:   ldc #15; //String hello
   5:   invokevirtual   #19; //Method scala/Predef$.println:(Ljava/lang/Object;)V
   8:   return

public Bytes();
  Code:
   0:   aload_0
   1:   invokespecial   #24; //Method java/lang/Object."<init>":()V
   4:   return

}
like image 192
Jed Wesley-Smith Avatar answered Oct 24 '22 23:10

Jed Wesley-Smith


None that I'm aware of. Consider using a bytecode inspection tool to inspect the code that the compiler produces. It is often useful when writing performance-critical code to verify whether specialization has been properly applied, closures eliminated, arrays are being accessed directly via bytecode instructions, macros properly expanded, etc.

If you are using Sublime, there is a this plugin for viewing the bytecode.

like image 36
axel22 Avatar answered Oct 24 '22 23:10

axel22