为什么 Scala 程序的编译速度很慢? [英] Why is compilation very slow for Scala programs?

查看:28
本文介绍了为什么 Scala 程序的编译速度很慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去两个月我一直在使用 Scala.我也在为一个小应用程序使用 Play 框架.我观察到,即使程序打印Hello World",编译也很慢.为什么这么慢?有什么减少时间的小窍门吗?

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?

推荐答案

在您的情况下,编译速度有多快?

How fast is compilation in your case?

scalac 的速度受两个因素的限制:

scalac's speed is bounded by two factors:

  1. 这是一个在 JVM 上运行的相当大的程序.因此启动时间并不长,因为必须 (1) 启动 JVM (2) 将 scalac 加载到其中 (3) JIT 编译大部分以提高速度.2-4 秒的启动时间是典型的,并且 scalac 的前几次运行并不是非常快.如果您的设置正确,这将通过 scalac 在正在运行的 JVM 中保持常驻和温暖"来缓解.sbt 和所有 IDE 一样.我建议使用这些选项之一,否则,如果您必须从命令行编译,请使用fsc",这也使编译器常驻.(举个例子:人们通常不会抱怨 REPL 的速度,但 REPL 使用与其他人相同的 scalac.区别只是编译器保持驻留.

  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).

即使是完全热身的 scalac 也必须应对有时需要复杂类型推断的程序.因此,对于具有非常简单的类型系统(例如 Go)的语言,它的速度不如编译器快.我在我 3 岁的 Macbook Pro 上看到编译速度为 500-800 行/秒.这没有打破世界纪录,但对于增量编译(IDE 和 sbt 现在所做的)来说已经足够了.我当前的项目大约有 50K 行代码,但我基本上从不等待我的 IDE(Eclipse 的 Scala IDE)中的编译器,因为增量编译足够快.有些人看到编译速度低于这个速度.这通常是因为它们使用了编译成本非常高的构造(通常从库中导入),例如复杂的隐式参数层次结构.

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.

这篇关于为什么 Scala 程序的编译速度很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆