Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is compilation very slow for Scala programs?

I have been using Scala for the last two months. I am also using the Play Framework for a small app. I have observed that compilation is very slow even for a program to print "Hello World". Why is it so slow? Are there any tips for decreasing the time?

like image 316
Goku__ Avatar asked May 02 '15 17:05

Goku__


People also ask

How does Scala compiler work?

Scala has both a compiler and an interpreter which can execute Scala code. The Scala compiler compiles your Scala code into Java Byte Code which can then be executed by the scala command. The scala command is similar to the java command, in that it executes your compiled Scala code.

Does Scala compile to Java?

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


1 Answers

How fast is compilation in your case?

scalac's speed is bounded by two factors:

  1. It's a rather large program that runs on the JVM. So startup times are not great because one has to (1) start the JVM (2) load scalac into it (3) JIT compile much of it to gain speed. Startup times of 2-4 seconds are typical, and the first couple runs of scalac are not super fast. If your setup is correct this will be mitigated by scalac being kept resident and "warm" in a running JVM. sbt does that, as do all IDEs. I recommend to use one of these options, or else, if you must compile from the command line, use "fsc" which also keeps the compiler resident. (Case in point: people generally do not complain about the speed of the REPL, yet the REPL uses the same scalac as everybody else. The difference is just that the compiler is kept resident).

  2. Even a fully warmed-up scalac has to contend with programs that sometimes require complex type inference. So it cannot be as fast as compilers for languages with very simple type systems such as Go. I see on my 3 year old Macbook Pro compile speeds of 500-800 lines / second. This beats no world record, but it is sufficient for incremental compilation which is what IDEs and sbt do now. My current project is about 50K lines of code but I basically never wait for the compiler in my IDE (Scala IDE for Eclipse) because incremental compilation is fast enough. Some people see compile speeds that are lower than that. That's usually because they use constructs (often imported from a library) that are very expensive to compile, such as complicated implicit parameter hierarchies.

like image 173
Martin Odersky Avatar answered Sep 24 '22 07:09

Martin Odersky