Skip to content

Incremental compilation #21165

Open
Open

Description

@mlugg

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.
  • self-hosted COFF linker
  • 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.panic and other cached values
    • some values are cached in Zcu, e.g. Zcu.panic_func_index
    • so, updates to panic don't register the correct dependencies and can crash the compiler or worse
  • incremental updates for std.builtin types
    • we currently don't register dependencies when doing this; Zcu.PerThread.getBuiltin etc should live on Sema so as to register appropriate dependencies
  • @embedFile (incremental: handle @embedFile #22602)
  • enable comptime call memoization
    • first, do some experiments to see how valuable memoization actually is confirmed: sometimes very important for compilation speed!
    • associated each memoized call with an AnalUnit to 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
  • for efficiency, memoize the result of Zcu.resolveReferences in a field on Zcu to avoid recomputing it when we call getAllErrorsAlloc/totalErrorCount multiple 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 -fincremental and has a way to give it to the user, this could be useful for incremental bug reports

Activity

  1. moved this to Incremental Compilation in Performanceon Aug 22, 2024
  2. andrewrk commented on Aug 28, 2024

    @andrewrk
    Member

    Regarding fuzzing, here are some properties we can test in addition to the obvious ones (i.e. not panicking or crashing)

    • path independence: regardless of the incremental steps taken along the way, two compilations based on the same set of input files should result in the same output binary and/or compilation errors.
    • Detection of triggering too much invalidation: test cases can be generated in an environment that includes the standard library; no matter what test case is generated, it should not cause, for example, std.process.exit to be reanalyzed. This applies to everything not generated by the test case, assuming the test case cannot set std options.
  3. added this to the 0.14.0 milestone on Oct 10, 2024
  4. mlugg commented on Oct 16, 2024

    @mlugg
    MemberAuthor

    Detection of triggering too much invalidation

    It's worth noting that parts of std/start.zig can correctly be invalidated if root.main (or root._start etc) changes.

    EDIT: so can a few other things, like std.options and std.builtin.panic.

  5. added a commit that references this issue on Nov 11, 2024
  6. added a commit that references this issue on Dec 6, 2024
  7. modified the milestones: 0.14.0, 0.15.0 on Feb 10, 2025
  8. mlugg commented on May 25, 2025

    @mlugg
    MemberAuthor

    @reader-lamp no; building in ReleaseSafe will 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.

  9. TapirLiu commented on Nov 4, 2025

    @TapirLiu

    It should be true for v0.15. You can modify the std lib code to see the effect.

  10. mlugg commented on Nov 5, 2025

    @mlugg
    MemberAuthor

    just to be clear, I read this on the Zig Discord:

    so the standard library is compiled every time you compile your code, not cached seperately

    is that true with the current Zig?

    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.

  11. reader-lamp commented on Nov 5, 2025

    @reader-lamp

    but the Zig compiler is actually relatively performant, so compiling the std code once per project is a very small cost.

    @mlugg I don't think this is correct. for example, here is hello world:

    zig-x86_64-windows-0.15.1> Measure-Command { zig build-exe hello.zig }
    TotalMilliseconds : 1128.1618
    

    note that is the best of 5 runs. compare with Go language:

    > Measure-Command { go build hello.go }
    TotalMilliseconds : 162.6737
    

    so its at least 7 times slower, even after compiling the std code once.

  12. nektro commented on Nov 6, 2025

    @nektro
    Contributor

    x86_64-windows still uses the llvm backend by default at the time of writing so that comparison isn't representative

  13. mlugg commented on Nov 7, 2025

    @mlugg
    MemberAuthor

    @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.

  14. xdBronch commented on Nov 8, 2025

    @xdBronch
    Contributor

    @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 --watch which 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

  15. xdBronch commented on Nov 9, 2025

    @xdBronch
    Contributor

    they have repeatedly shown Zig being orders of magnitude slower than other options

    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?

    my use case is extremely common so its worth providing that info.

    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.

    so where is your differing data?

    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.

    ~ $ time Downloads/zig-x86_64-linux-0.15.1/zig build-exe hello.zig
    
    ________________________________________________________
    Executed in  238.48 millis    fish           external
       usr time  417.02 millis    0.00 micros  417.02 millis
       sys time  124.13 millis  393.00 micros  123.74 millis
    

    I could even make Zig look a lot better than Go

    ~ $ time Downloads/zig-x86_64-linux-0.15.1/zig build-exe hello.zig -fstrip
    
    ________________________________________________________
    Executed in   77.20 millis    fish           external
       usr time  120.57 millis  288.00 micros  120.28 millis
       sys time   44.59 millis   15.00 micros   44.57 millis
    
  16. reader-lamp commented on Nov 9, 2025

    @reader-lamp

    I could even make Zig look a lot better than Go

    ~ $ time Downloads/zig-x86_64-linux-0.15.1/zig build-exe hello.zig -fstrip
    
    ________________________________________________________
    Executed in   77.20 millis    fish           external
       usr time  120.57 millis  288.00 micros  120.28 millis
       sys time   44.59 millis   15.00 micros   44.57 millis
    

    @xdBronch straw man. you only have Zig data here, comparing again my Go data is obviously wrong because its different computers. allow me:

    zig-x86_64-windows-0.15.1> Measure-Command { .\zig build-exe hello.zig -fstrip }
    TotalMilliseconds : 301.8097
    
    go1.25.4.windows-amd64> Measure-Command { go\bin\go build hello.go }
    TotalMilliseconds : 155.1268
    

    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

  17. TheScabbage commented on Nov 9, 2025

    @TheScabbage

    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

    When using incremental compilation, a default zig init project recompiles in about 18ms for me. Go takes 20ms for a hello world.

  18. mlugg commented on Nov 9, 2025

    @mlugg
    MemberAuthor

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    incremental compilationProblem occurs only when reusing compiler state.

    Type

    No type

    Projects

    • Status
      Incremental Compilation

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions