Enabling Kotlin incremental compilation on Buck2

The Kotlin incremental compiler has been a true gem for developers chasing faster compilation since its introduction in build tools. Now, we’re excited to bring its benefits to Buck2 –  Meta’s buil…

Iveta Kovalenko
13 min readadvanced
--
View Original

Overview

The article discusses the integration of Kotlin's incremental compiler into Buck2, Meta's build system, highlighting the improvements in build times and developer productivity. It outlines the steps taken to implement this feature, including challenges faced and solutions developed.

What You'll Learn

1

How to integrate Kotlin's incremental compiler into a build system

2

Why preserving previous build outputs is crucial for incremental compilation

3

How to configure a build system for distributed builds with relocatable caches

4

When to use classpath snapshots for dependency tracking in Kotlin

5

How to optimize custom compiler plugins for incremental compilation

Prerequisites & Requirements

  • Understanding of Kotlin and its incremental compilation process
  • Familiarity with Buck2 build system(optional)

Key Questions Answered

How does Kotlin's incremental compiler improve build times?
Kotlin's incremental compiler focuses only on changed files rather than recompiling entire modules, leading to significant reductions in build times. The article mentions that critical modules can build up to 3x faster, enhancing developer productivity.
What steps were taken to integrate Kotlin's incremental compiler into Buck2?
The integration involved several steps, including using the Build Tools API, preserving previous outputs, making the compiler cache relocatable, and configuring the incremental compiler to track changes in both modules and dependencies. These steps ensured efficient incremental builds.
What challenges were faced when integrating the incremental compiler?
Challenges included ensuring compatibility with existing unshaded compiler versions, managing the output of custom compiler plugins, and handling multiple rounds of compilation due to circular dependencies. Solutions involved modifying the build system and updating plugins for compatibility.
What impact did incremental compilation have on Meta's codebase?
The implementation of incremental compilation resulted in a 30% improvement in average build times for developers, with specific modules experiencing nearly double the speed. This demonstrated the effectiveness of the incremental compiler in large codebases.

Key Statistics & Figures

Build time improvement
30%
This improvement was observed for the average developer after enabling incremental compilation.
Speed increase for specific modules without annotation processing
Nearly doubled
This significant speed increase highlights the effectiveness of incremental compilation in reducing build times.
Critical modules build time reduction
Up to 3x faster
This statistic indicates the potential for substantial gains in developer productivity.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Programming Language
Kotlin
Used for developing applications and integrating the incremental compiler.
Build System
Buck2
Meta's build system that supports incremental compilation for Kotlin.
Annotation Processing Tool
Kotlin Symbol Processing (ksp2)
Used for annotation processing without impacting the incremental compilation flow.

Key Actionable Insights

1
Integrate the Kotlin incremental compiler early in your build system to leverage its benefits.
By adopting the incremental compiler during the initial phases of toolchain development, you can provide feedback and influence its evolution, ensuring a more stable integration in the long run.
2
Preserve previous build outputs to enhance incremental compilation efficiency.
Configuring your build system to retain outputs from previous builds can significantly reduce compilation times, especially in larger projects where changes are incremental.
3
Ensure that your compiler plugins are compatible with incremental compilation.
Updating custom plugins to handle partial inputs correctly is essential for maintaining build integrity and performance when using the incremental compiler.
4
Utilize classpath snapshots for effective dependency tracking.
Implementing classpath snapshots allows the incremental compiler to efficiently determine which files need recompilation based on changes in dependencies, further optimizing build times.

Common Pitfalls

1
Failing to update custom compiler plugins for incremental compilation can lead to incomplete results.
If plugins are not designed to handle partial inputs, they may produce incorrect outputs when only a subset of files is compiled. This can severely impact build reliability.
2
Not preserving previous outputs can hinder the effectiveness of incremental compilation.
When previous outputs are deleted before a rebuild, the incremental compiler cannot function optimally, leading to longer build times and reduced efficiency.

Related Concepts

Incremental Compilation Techniques
Dependency Management In Kotlin
Build Optimization Strategies