AI Briefing
KO

Speeding Up Scala Compilation

·2024.12.17 09:00

Key point

Improved Scala build speed with companion objects and build pipelining.

1 / 2

Details

The Cookie Run: Kingdom server, built with Scala2 and sbt, applied two improvements to reduce compilation bottlenecks.

The first is placing typeclass instances in companion objects. Compared to extends on a separate trait or importing a separate object, this reduced the cost of implicit search in the typer phase, and time spent in implicits decreased significantly. For a sub-project, compile time dropped from about 65 seconds to 45 seconds, a 1.44x speedup.

In profiling, phase-by-phase time was checked via -Vstatistics:typer, and in particular successful in scope and failed in scope decreased significantly, while successful of type and failed of type increased slightly. The implicit search flamegraph also allows tracking in more detail which type searches consume time.

The second is applying build pipelining. This can be enabled with ThisBuild / usePipelining := true, which lets dependent sub-projects continue compiling using only the pickler/early output, without waiting for .class files. However, macros can block pipelining, so projects that depend on macro definitions or macro expansion need sequential compilation.

  • If a project depends on macro definitions, sbt implicitly switches to sequential compilation.
  • If macro expansion requires user code, early output alone is not sufficient.
  • In such cases, use exportPipelining := false to control behavior per sub-project.
  • Debug logs show messages like early output can't be made because of macros to help identify the cause.

In the end, including build pipelining, overall project compile speed improved by 1.22x. The key is reducing typeclass search cost to lower typer time, and structuring dependencies to allow pipelining while accounting for macro constraints.

This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.

Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.