9

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?

Michał Urbaniak
  • 1,249
  • 13
  • 28
  • 11
    The Scala compiler spends most of its time, AFAIK, in the typer phase. Scala has a more powerful type system than Kotlin, this might be one reason. Otherwise related question: https://stackoverflow.com/questions/30005124/why-is-compilation-very-slow-for-scala-programs - if you don't want your question to be closed as opinion-based, it would be helpful to provide concrete numbers, e.g. benchmarks for comparable code. – 0__ Jan 05 '16 at 16:09
  • 3
    scala is principally non-local. Single change in an implicit may lead entire project to recompile – ayvango Jan 05 '16 at 16:45
  • 1
    After seeing 0__'s answer we can clearly see that the question can be answered in a non-opinion-based way... – Michał Urbaniak Jan 05 '16 at 18:54
  • 3
    @MichałUrbaniak 0__'s answer is not an answer to the 3 question (title and the other two at the end). Is just a well written draft analysis of the sentence that *Kotlin compile faster than Scala* and not why... This question is still a primary opinion based for everybody except *Dmitry Jemerov* :) – Michele d'Amico Jan 05 '16 at 20:56

1 Answers1

29

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 two 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" :)

Sean Reilly
  • 21,526
  • 4
  • 48
  • 62
0__
  • 66,707
  • 21
  • 171
  • 266
  • 5
    A very good answer to a poor question :) – Michał Urbaniak Jan 05 '16 at 21:14
  • 1
    @MichałUrbaniak It is really not an answer. For _one example_ I picked, Kotlin compiles maybe three or four times more LOC per time compared to Scala. If your unit is "algorithm" perhaps the speed increase is less than 2x, while you might need to write less code. But there are different approaches to Project Euler, and there are many other scenarios and larger code bases where you would write different kind of code... The formula also changes when you take sbt's capability of incremental compilation into account. Finally, this cannot explain **why a compiler takes this and that much time**. – 0__ Jan 05 '16 at 21:22
  • Note: the lines of code comparison is not meaningful. If you look at the code, you'll see why. The Kotlin version adds a lot of scaffolding to each solution (declares package, main function, prints output, etc). The Scala version is bare-bones and only asserts solutions. The same could be done in Kotlin. – pabl0rg May 31 '16 at 10:34
  • I would argue word-count is very meaningful. A lot of code that is hand-written by Kotlin needs to be inferred by Scala compiler. Scala compiler is doing extra work. Regardless of how necessary or useful that extra work may be is a different question. – Chetan Bhasin Mar 25 '19 at 15:30