Why is compilation very slow for Scala programs?

后端 未结 2 537
北荒
北荒 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: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.

提交回复
热议问题