Zig's Incremental Compilation Internals
a day ago
- Zig's incremental compilation detects changed functions/declarations, recompiles only those, and patches the binary for fast rebuilds.
- The feature is now viable for real-world projects and used daily by the Zig core team.
- A demo shows rebuilds taking 50-70ms after an initial 5-second build.
- Per-file processing (parse, AstGen, ZIR) is embarrassingly parallel and cached on disk.
- Semantic analysis uses a dependency graph of analysis units (types, values, bodies) to minimize re-analysis.
- Code generation (AIR to MIR) is embarrassingly parallel and functions are independent.
- Incremental linking uses a MappedFile abstraction for resizing and patchability, avoiding full relinking.
- The linker amortizes movement costs via exponential growth, making slow updates rare.
- Flush includes a reference graph traversal; optimization opportunities remain.
- Usage: run `zig build --watch -fincremental` targeting x86_64-linux; works on Zig master branch.