Why is compilation very slow for Scala programs?

后端 未结 2 538
北荒
北荒 2021-02-04 09:18

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 \"

相关标签:
2条回答
  • 2021-02-04 09:56

    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.

    0 讨论(0)
  • 2021-02-04 09:59

    Well, this is kind of a duplicate question, but since it contains another, more practical part, I'll try to give an answer nevertheless.

    First things first: compilation is slow because the language is rich with features. Of course, there are examples where languages with approximately the same set of features compile faster, but we have what we have. First few things that come to my mind are:

    • type inference
    • implicit resolution
    • macro expansion
    • type-level programming that results in tremendous number of specific (anonymous classes), which all need to be accounted for
    • ...

    For a much more elaborate answer I refer you to Scala's creator Dr. Odersky himself:.

    As for the second part of your question, that depends on how you compile:

    • If you use bare scalac, you can't achieve greater speed since you are bound to separete launches over and over.
    • If you use sbt, the built-in incremental compiler (server) comes to your service: http://www.scala-sbt.org/0.13.5/docs/Detailed-Topics/Understanding-incremental-recompilation.html
    • If you use IntelliJ IDEA, it has it's incremental compiler as well: http://blog.jetbrains.com/scala/2012/12/28/a-new-way-to-compile/
    • If you use Typesafe Activator (i.e. essentially you use sbt), it uses sbt server as well: https://www.typesafe.com/blog/typesafe-activator-130-released-contains-new-sbt-server-and-ui

    I think in your case sticking to Activator is the best choice since you already use Play. But remember that this is not a silver bullet: you still get a slow first-time compilation and subsequent runs' speed depends on how much changes you introduced to the code.

    Ah, and there's also a hardware side to it. It goes without saying that a powerful CPU, enough RAM and a fast (preferably SSD) disk drive will certainly reduce compilation speed.

    0 讨论(0)
提交回复
热议问题