Repository navigation
Description
How do I try it?
TL;DR: zig build --watch -fincremental --prominent-compile-errors, either targeting x86_64-linux with the self-hosted backend, or just not emitting a binary (i.e. -fno-emit-bin).
The easiest way to try out incremental compilation is passing the --watch -fincremental flags to zig build: the build system will track when source files change and perform incremental updates. (Note that --watch does have bugs on macOS right now; #21905). It might also be helpful to add the --prominent-compile-errors flag.
The self-hosted x86_64 backend targeting ELF and the C backend have fairly good support for incremental compilation. Other backends are almost certain to crash or miscompile at the moment. However, a common use case for incremental compilation is for quick compile error feedback when trying to get code compiling, in which case you might want to consider emitting no binary at all (in the build system, don't install the compile step artifact, just depend on the compile step directly).
Using incremental compilation makes you more likely to encounter compiler bugs, including false positive compile errors, false negative compile errors, miscompilations, and more. If you encounter one and are able to figure out how to consistently reproduce it, bug reports are appreciated.
Rather than bugs in existing parts of the incremental compilation logic, this issue tracks all logic which is explicitly WIP or incomplete, such as most linkers.
Codegen and linking
- self-hosted ELF linker
- Solved with the merge of Elf2: create a new linker from scratch #25299. Although the new linker is currently missing many features, it is the default when using
-fincremental, and has no known issues with incremental compilation.
- Solved with the merge of Elf2: create a new linker from scratch #25299. Although the new linker is currently missing many features, it is the default when using
- self-hosted COFF linker
- Coff2: create a new linker from scratch #25430 made progress towards this, but @jacobly0 reports that we're sporadically affected by some kind of race condition in the Windows kernel, meaning this linker is currently buggy. We can check this off once that's solved.
- self-hosted MachO linker
- LLVM backend
- this is low priority as there isn't much to gain from it; we still have to wait for the full LLVM emit every update
Other
- incremental updates for
std.builtin.panicand other cached values- some values are cached in
Zcu, e.g.Zcu.panic_func_index - so, updates to
panicdon't register the correct dependencies and can crash the compiler or worse
- some values are cached in
- incremental updates for
std.builtintypes- we currently don't register dependencies when doing this;
Zcu.PerThread.getBuiltinetc should live onSemaso as to register appropriate dependencies
- we currently don't register dependencies when doing this;
-
@embedFile(incremental: handle@embedFile#22602) - enable comptime call memoization
first, do some experiments to see how valuable memoization actually isconfirmed: sometimes very important for compilation speed!- associated each memoized call with an
AnalUnitto track dependencies properly
- figure out how to handle test references
- determining which tests are referenced at the end of an update would require a
resolveReferences - if we're doing that, let's just memoize its result in a field on
Zcu - but, changing the test discovery rules could be an alternative solution, since it's an idea that's been floated anyway
- determining which tests are referenced at the end of an update would require a
- for efficiency, memoize the result of
Zcu.resolveReferencesin a field onZcuto avoid recomputing it when we callgetAllErrorsAlloc/totalErrorCountmultiple times
Tooling
- fuzzing infrastructure to find crashes and incorrect behavior in incremental compilation
- debug tooling to dump dependency information into a graphviz/similar file
- a compact binary format giving information about invalidated dependencies
- if the compiler generates this under
-fincrementaland has a way to give it to the user, this could be useful for incremental bug reports
- if the compiler generates this under
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIncremental Compilation
Activity
andrewrk commentedon Aug 28, 2024
Regarding fuzzing, here are some properties we can test in addition to the obvious ones (i.e. not panicking or crashing)
std.process.exitto be reanalyzed. This applies to everything not generated by the test case, assuming the test case cannot set std options.mlugg commentedon Oct 16, 2024
It's worth noting that parts of
std/start.zigcan correctly be invalidated ifroot.main(orroot._startetc) changes.EDIT: so can a few other things, like
std.optionsandstd.builtin.panic.test: add new incremental case
Zcu: allow test declarations to be failed
mlugg commentedon May 25, 2025
@reader-lamp no; building in
ReleaseSafewill not be subject to the performance improvements of incremental compilation (at least not by default). Release builds are allowed, and even expected, to take several orders of magnitude longer to compile than Debug. If you want fast compilation speed, that's what Debug is for, and that's where incremental compilation can bring us significant gains.TapirLiu commentedon Nov 4, 2025
It should be true for v0.15. You can modify the std lib code to see the effect.
mlugg commentedon Nov 5, 2025
Yes. Do however note that Zig's lazy analysis model means that only the parts of the standard library you use get compiled. (By default, that includes the default panic and segfault handlers, which in turn depend on stack tracing code, which in turn requires parsing debug information; all of that machinery is responsible for the vast majority of the compile time for a "Hello World").
Incremental compilation, on the other hand, allows the compiler to cache this information and only re-compile parts of the standard library if necessary (e.g. you changed some comptime parameters passed to a standard library function, or changed the standard library source code itself). The standard library still cannot be cached globally, i.e. such that the same cache can be used for many different compilations, as a result of Zig's single-compilation-unit compilation model as well as our compiler architecture; but the Zig compiler is actually relatively performant, so compiling the std code once per project is a very small cost.
reader-lamp commentedon Nov 5, 2025
@mlugg I don't think this is correct. for example, here is hello world:
note that is the best of 5 runs. compare with Go language:
so its at least 7 times slower, even after compiling the std code once.
nektro commentedon Nov 6, 2025
x86_64-windows still uses the llvm backend by default at the time of writing so that comparison isn't representative
mlugg commentedon Nov 7, 2025
@reader-lamp Please read the issue properly before making performance claims. You are looking at the performance of the default behavior, which is to not use incremental compilation, because the feature is currently experimental.
To use incremental compilation right now, you need to use
zig build --watch -fincremental. Under this, updating a codebase while making no changes will usually take a single-digit number of milliseconds. Regardless, the performance of incremental compilation is not final at the moment, because again, the feature is under active development.xdBronch commentedon Nov 8, 2025
@reader-lamp the benchmarks you keep doing are not representative and really not useful, the point of incremental is making rebuilds faster and you didn't even use
--watchwhich is currently required. even then, it's never this straight forward to compare compilation speeds of languages so arbitrarily, there's no saying what something like a hello world may include by default.and please stop deleting your comments, it makes it very hard to follow what the other comments are talking about, you've done it 4 times now
xdBronch commentedon Nov 9, 2025
they show zig being slower in a comparison that is not representative. Go and Zig are very very different languages with very different compilers, it doesn't make much sense to compare them by looking at such a small program because the majority of the compilation time is being spent on something your benchmark doesn't show (the standard library, startup code, build system overhead maybe). Does C++ compile fast just because a hello world does?
I'm not saying that your use case isn't valid but if you're going to evaluate the compilation speed of incremental you're gonna have to actually use it. most compilations are recompilations, it should be the metric you're most interested in.
it's not my intention to disprove your data but to show why the data isn't necessarily meaningful, its not unreasonable to say that Go compiles faster than zig, its a simpler language and a lot of work has been put into it! if you want a closer to fair comparison (though again, nothing can be a direct comparison between languages!) LLVM should not be used as said before.
I could even make Zig look a lot better than Go
reader-lamp commentedon Nov 9, 2025
@xdBronch straw man. you only have Zig data here, comparing again my Go data is obviously wrong because its different computers. allow me:
so Go is still double the speed, and keeps the debug symbols also. to be clear, I like Zig, and I am actively looking into making it my primary language, but to just pretend these issues don't exist is not helpful
TheScabbage commentedon Nov 9, 2025
When using incremental compilation, a default
zig initproject recompiles in about 18ms for me. Go takes 20ms for a hello world.mlugg commentedon Nov 9, 2025
@reader-lamp you seem to just be using this issue to discuss the Zig compiler's performance in general. Not once so far have you benchmarked, or even used, incremental compilation. Nobody is "pretending these issues don't exist"; rather, they are trying to keep the thread on-topic.
This issue tracks the implementation of incremental compilation in the Zig compiler. If you have concerns or questions specifically about the performance of incremental compilation, feel free to raise them here. Otherwise, I ask you bring this discussion elsewhere. It seems to me like this conversation would be better-suited to a Zig community.
Also, as xdBroch already asked, please refrain from deleting comments; it makes the discussion extremely difficult to follow.