4 min Devops

Kotlin 2.0 shows off faster compilation time: what to expect?

Kotlin 2.0 shows off faster compilation time: what to expect?

JetBrains released the Kotlin 2.0 programming language to the general public. The second version mainly revolves around a faster compiler. Kotlin coders seem to be very excited about it. A justifiable emotion, JetBrains’ benchmarks show.

For Kotlin 2.0, the first version was completely forgotten, and JetBrains worked out a new concept. Developers can, therefore, expect many new things. The faster compilation time is seen among developers as the biggest advancement of the programming language. Developers previously stumbled over the compilation time, which took significantly longer than that of competitor Java.

Speed gains of up to 94%

Now, the big question is how the compilation time specifically improved. JetBrains released benchmarks last month to warm the public before the official release. The new K2 compiler promises significant improvements in compilation speed, with speed gains of up to 94% in certain projects. These impressive performance improvements result from a new type inference algorithm and a unified data structure that increases compilation efficiency.

Further testing for the Anki-Android project -an open source project- showed that the“clean build” time of 57.7 seconds with Kotlin 1.9.23 was reduced to 29.7 seconds with the latest version. By this time, programmers refer to the time it takes to build a completely new project, that is, without reusing source files from a previous project.

The graph shows the Kotlin compilation time for a clean build with Anki-Android. Source: JetBrains

JetBrains further proved that the compiler is faster at the initialization and analysis phases. “The initialization phase is up to 488% faster with the K2 compiler. The Kotlin K2 compiler is up to 376% faster in the analysis phase compared to the previous compiler.” These performance gains are harder for programmers to notice in practice because these phases did never take much time. For example, the initialization phase involved an acceleration from 0.126 seconds to 0.022 seconds for the Anki-Android project.

The graph shows the otherwise distributed performance gains for the initialization and analysis phases. Source: JetBrains

In a Reddit discussion surrounding the novelties in Kotlin 2.0, one user lists additional benefits. “The new compiler has better type inference, which results in cleaner code, and the performance of the compiled code is slightly faster.” This suggests that the improvements not only affect compilation time but also improve the clarity and efficiency of the final code.

Switching to Kotlin 2.0

Moving to Kotlin 2.0 is an important step for developers. JetBrains has prepared a comprehensive migration guide to make this transition as smooth as possible. From the moment the Kotlin 2.0 version is used, it runs on the Kotlin K2 compiler anyway, so there is nothing for the developer to do. To get even more insights about build performance with Kotlin 2.0, the developer should manually update the Kotlin version in the Gradle or Maven build scripts.

The new version also pays more attention to multiplatform development. In this type of development, an app is built to be compatible with multiple platforms, an example being Minecraft. Specifically, the tooling for Kotlin Multiplatform (KMP) was improved. Google has already advised Android app developers to switch to KMP and now offers support for KMP in several popular Android libraries. The support is important for the programming language, as Kotlin is the preferred programming language for Android and thus, a large proportion of Kotlin users program apps for Google’s operating system.

It looks like Kotlin 2.0 is going to be a significant step in the programming language’s evolution. The improved compilation time will undoubtedly contribute to the satisfaction of developers worldwide. Initial reactions from the Kotlin developer community already indicate that developers are eager to get started with Kotlin 2.0.

By the way, not all features of the earlier programming language version have been discarded. Java is still the foundation of Kotlin as the programming language runs natively on the Java Virtual Machine, on which it once started as a programming language. Kotlin, therefore, offers Java support, even though these programming languages are essentially competitors to each other.

Also read: How the Kotlin programming language has conquered Android