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

查看:247
本文介绍了为什么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使用的标尺与其他人相同.区别只是编译器保持驻留状态.)

  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现在要做的.我当前的项目大约是5万行代码,但是我基本上从不等待IDE(Scala IDE for Eclipse)中的编译器,因为增量编译速度足够快.有些人看到的编译速度比那低.这通常是因为它们使用了编译成本很高的构造(通常是从库中导入的),例如复杂的隐式参数层次结构.

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天全站免登陆