January 21, 2025
The Kotlin Foundation sponsored four projects for the Google Summer of Code 2024, and we are excited to announce the completion of the “Incremental Compilation for the Kotlin/Wasm Compiler” project! JetBrains, a Kotlin Foundation member, provided mentoring support for this project.
This project enhanced the Kotlin-to-Wasm compiler with incremental compilation capabilities, reducing build times by allowing it to recompile only modified files. The contributor, Osama Ahmad, optimized and reused components from the Kotlin/JS backend while improving documentation and refactoring the codebase.
Thanks to Osama Ahmad and the mentor from JetBrains, Igor Yakovlev, for their great input!
Explore Kotlin/Wasm.
Incremental compilation is a technique that helps increase compilation speed by recompiling only changed files instead of your whole program (a clean build). The Kotlin-to-Wasm compiler is used to support only clean builds, but during this project, we enhanced it to support incremental compilation, too.
The primary goals for the project were:
During the project, the following key achievements were made:
linkWasmCompiledFragments
, into multiple smaller and clearer functions, along with the necessary documentation.Newly introduced components such as WasmSerializer
and WasmDeserializer
are well documented, as well.Through benchmarking, we found that the time taken to serialize and deserialize Wasm cached compiled files majorly contributes to linking time. This is because a single Wasm binary is produced, and each time a change is made, we need to load (deserialize) all cached compiled modules and link all of them in a single binary, even if only a single file in a single module was changed.
One solution to this problem is to implement per-module compilation, in which each Kotlin module is compiled to a standalone Wasm binary, and utilize Wasm features to link between the different module Wasm binaries. This way, we don’t need to load every module and link all of them into a single binary on each recompilation.