Michał Urbaniak Michał Urbaniak - 1 month ago 14
Scala Question

Why does Kotlin compile faster than Scala?

When we read the wikipedia description of the Kotlin programming language, it is stating that:


JetBrains lead Dmitry Jemerov said that most languages did not have the features they were looking for, with the exception of Scala. However, he cited the slow compile time of Scala as an obvious deficiency.[4] One of the stated goals of Kotlin is to compile as quickly as Java.


How did they achieve that goal? And why is Scala compile time so slow that it was unacceptable for the Kotlin creators? Or - in other words - which features of the Scala compiler make it slower than the Kotlin compiler?

0__ 0__
Answer

Although I think the question is not well suited for Stack Overflow as it will tend to produce primarily opinion based answers, here is one attempt: You have too different languages, especially concerning the type system, and two completely independent implementations of compilers. So to expect them to have the "same" kind of compilation speed is already a fallacy. I have linked in my comment to another question that examines the speed of the Scala compiler. Basically, it depends on many factors, for example the amount of work that the type inferencer and implicit resolution required by a specific code base.

Nevertheless, I ran a very quick example: I compiled some Project Euler solutions in Kotlin and Scala. This gave me for a fresh re-compile of the whole project:

  • 6 seconds in Kotlin (down to 5 seconds in successive re-builds)
  • 10 seconds in Scala (down to 7 seconds in successive re-builds).

Origin of the source code:

  • I took this code for Kotlin, changed a lot of imports because apparently the Kotlin standard library changed in the meantime, in order to make it compile.
  • I took this code for Scala, and converted it into an sbt project with each problem wrapped in an object pXY extends App { ... } and placed it in a package euler.

Then I removed the files for which only one solution existed, ending up with 26 problems. Both projects were compiled with IntelliJ IDEA 15 CE using Rebuild Project.


To give another perspective on this business, I ran wc (word count) on the sources:

// lines words bytes
931  3603 33087 total  // Kotlin
261  1166  6472 total  // Scala

So now you can either argue that the Kotlin compiler needed to process "more source code" or that the Scala code was "more dense" :)