News for hackers. Stuff that matters.
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by 9d on Sunday June 08, 2025 @09:11PM (#44220604)
> For a larger project like the Zig compiler itself, it takes the time down from 75 seconds to 20 seconds. We’re only just getting started.

Excited to see what he can do with this. He seems like a really smart guy.

What's the package management look like? I tried to get an app with QuickJS + SDL3 working, but the mess of C++ pushed me to Rust where it all just works. Would be glad to try it out in Zig too.

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by stratts on Sunday June 08, 2025 @10:26PM (#44220976)
Package management in Zig is more manual than Rust, involving fetching the package URL using the CLI, then importing the module in your build script. This has its upsides - you can depend on arbitrary archives, so lots of Zig packages of C libraries are just a build script with a dependency on a unmodified tarball release. But obviously it's a little trickier for beginners.

SDL3 has both a native Zig wrapper: https://github.com/Gota7/zig-sdl3

And a more basic repackaging on the C library/API: https://github.com/castholm/SDL

For QuickJS, the only option is the C API: https://github.com/allyourcodebase/quickjs-ng

Zig makes it really easy to use C packages directly like this, though Zig's types are much more strict so you'll inevitably be doing a lot of casting when interacting with the API

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by LAC-Tech on Monday June 09, 2025 @12:29AM (#44221415)
It's also worth pointing out that the Zig std library covers a lot more than the rust one. No need for things like rustix, rand, hashbrown, and a few others I always have to add whenever I do rust stuff.
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by nindalf on Monday June 09, 2025 @12:52AM (#44221499)
You add hashbrown as an explicit dependency? The standard library HashMap is a re-export of hashbrown. Doesn’t it work for you?
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by vlovich123 on Monday June 09, 2025 @01:11AM (#44221587)
Can’t speak for the op but there’s a number of high performance interfaces that avoid redundant computations that are only available directly from hashbrown.
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by LAC-Tech on Monday June 09, 2025 @01:10AM (#44221579)
huh, does it? I always add it so I can make non-deterministic hashmaps in rust. oh and you need one more one for the hashing function I think.

But I did not know hashmap re-exported hashbrown, thanks.

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by nindalf on Monday June 09, 2025 @02:57AM (#44222025)
Yep, they’re the same since Rust 1.36 (Jul 2019) - https://blog.rust-lang.org/2019/07/04/Rust-1.36.0/
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by LAC-Tech on Monday June 09, 2025 @03:11AM (#44222101)
https://doc.rust-lang.org/std/?search=hashbrown

looks like there's no way to access it, outside of hashmap.

Though maybe you just need the third party hasher and you can call with_hasher.

IDK man there's a lot going on with rust.

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by nindalf on Monday June 09, 2025 @08:03AM (#44223598)
Yes that’s exactly it. If speed is a priority and the input is trusted, change the hasher to something faster like ahash. The ahash crate makes this easy.
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by eknkc on Monday June 09, 2025 @05:35AM (#44222740)
It is pretty easy to interface stuff like QuickJS C API. I had a POC here from last year: https://github.com/eknkc/zquickjs

Even this is pretty usable, handling value conversions and such thanks to comptime. (Take a look at the tests here: https://github.com/eknkc/zquickjs/blob/master/src/root.zig)

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by throwawaymaths on Monday June 09, 2025 @01:39AM (#44221682)
im stunned that zig can compile itself in 75 seconds (even with llvm)
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by pjmlp on Monday June 09, 2025 @04:12AM (#44222374)
We used to have such fast compile times with Turbo Pascal, and other dialects, Modula-2, Oberon dialects, across 16 bit and early 32 bit home computers.

Then everything went south, with the languages that took over mainstream computing.

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by faresahmed on Monday June 09, 2025 @05:14AM (#44222637)
Not to disagree with you, but even C++ is going through great efforts to improve compile-times through C++20 modules and C++23 standard library modules (import std;). Although no compiler fully supports both, you can get an idea of how they can improve compile-times with clang and libc++

    $ # No modules
    $ clang++ -std=c++23 -stdlib=libc++ a.cpp # 4.8s
    $ # With modules
    $ clang++ -std=c++23 -stdlib=libc++ --precompile -o std.pcm /path/to/libc++/v1/std.cppm # 4.6s but this is done once
    $ clang++ -std=c++23 -stdlib=libc++ -fmodule-file=std=std.pcm b.cpp # 1.5s 
a.cpp and b.cpp are equivalent but b.cpp does `import std;` and a.cpp imports every standard C++ header file (same thing as import std, you can find them in libc++' std.cppm).

Notice that this is an extreme example since we're importing the whole standard library and is actually discouraged [^1]. Instead you can get through the day with just these flags: `-stdlib=libc++ -fimplicit-modules -fimplicit-module-maps` and of course -std=c++20 or later, no extra files/commands required! but you are only restricted to doing import <vector>; and such, no import std.

[^1]: non-standard headers like `bits/stdc++.h` which does the same thing (#including the whole standard library) is what is actually discouraged because a. non-standard and b. compile-times, but I can see `import std` solving these two and being encouraged once it's widely available!

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by pjmlp on Monday June 09, 2025 @05:46AM (#44222785)
As big fan of C++ modules (see my github), we are decades away of widespread adoption, unfortunately.

See regular discussions on C++ reddit, regarding state of modules support across the ecosystem.

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by 9d on Monday June 09, 2025 @11:32AM (#44225519)
C++ will be a very useful, fast, safe, and productive language in 2070.
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by rurban on Monday June 09, 2025 @06:34AM (#44223001)
tinycc is still fast. all the current single-pass compilers are fast.
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by pjmlp on Monday June 09, 2025 @09:24AM (#44224265)
Agreed, I would say the main problem is lack of focus on developer productivity.

Ok, the goalpost has moved on with what -O0 is expected to deliver in machine code quality, lets then have something like -ffast-compile, or interpreter/jit as alternative toolchain in the box.

Practical example from D land, compile D with dmd during development, use gdc or ldc for release.

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by 9d on Monday June 09, 2025 @11:40AM (#44225604)
Am I wrong about this?

Their algorithms were simpler.

Their output was simpler.

As their complexity grew, proportionately did program performance.

Not to mention adding language convenience features (generics, closures).

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by pjmlp on Monday June 09, 2025 @12:14PM (#44225985)
See Ada, released in 1983.

Generics were already present in CLU and ML, initially introduced in 1976.

Check their features.

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by 9d on Monday June 09, 2025 @04:27PM (#44229081)
Yeah but--
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by WalterBright on Monday June 09, 2025 @02:14AM (#44221813)
The dmd D compiler can compile itself (debug build):

real 0m18.444s user 0m17.408s sys 0m1.688s

On an ancient processor (it runs so fast I just never upgraded it):

cat /proc/cpuinfo processor : 0 vendor_id : AuthenticAMD cpu family : 15 model : 107 model name : AMD Athlon(tm) 64 X2 Dual Core Processor 4400+ stepping : 2 cpu MHz : 2299.674 cache size : 512 KB physical id : 0 siblings : 2 core id : 0 cpu cores : 2 apicid : 0 initial apicid : 0 fpu : yes

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by AndyKelley on Monday June 09, 2025 @02:32AM (#44221909)
18s eh? we're looking at 15s in https://github.com/ziglang/zig/pull/24124

oh, and by the way that includes the package manager, so the compile time accounts for:

* HTTP

* TLS (including aegis-128l, aegis-256, aes128-gcm, aes256-gcm, chacha20poly1305)

* deflate, zstd, and xz

* git protocol

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by WalterBright on Monday June 09, 2025 @03:00AM (#44222037)
Nice to hear from you, Andrew! I assume you're using a machine newer than 15 years ago :-)

I suppose it would compile faster if I didn't have symbolic debug info turned on.

Anyhow, our users often use dmd for development because of the high speed turnaround, and gdc/ldc for deployment with their more optimized code gen.

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by AndyKelley on Monday June 09, 2025 @03:07AM (#44222082)
You too! Yeah I think that was a great call. I took inspiration from D for sure when aiming for this milestone that we reached today.

Some people say you should use an old computer for development to help you write faster code. I say you should use a new computer for development, and write the fastest code you possibly can by exploiting all the new CPU instructions and optimizing for newer caching characteristics.

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by WalterBright on Monday June 09, 2025 @03:20AM (#44222144)
I'm still in the camp of using computers our users tend to have.

Also, self-compile times are strongly related to how much code there is in the compiler, not just the compile speed.

I also confess to being a bit jaded on this. I've been generating code from 8086 processors to the latest. Which instructions and combinations are faster is always flip-flopping around from chip to chip. So I leave it to the gdc/ldc compilers for the top shelf speed, and just try to make the code gen bulletproof and do a solid job.

Working on the new AArch64 has been quite fun. I'll be doing a presentation on it later in the summer. My target machine is a Raspberry Pi, which is a great machine.

Having the two code generators side by side also significantly increased the build times, because it's a lot more code being compiled.

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by AndyKelley on Monday June 09, 2025 @03:33AM (#44222212)
Fair enough, and yeah I hear you on the compilation cost of all the targets. We don't have aarch64 yet but in addition to x86_64 we do have an LLVM backend, a C backend, a SPIR-V backend, WebAssembly backend, RISC-V backend, and sparc backend. All that plus the stuff I mentioned earlier in 15s on a modern laptop.
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by WalterBright on Monday June 09, 2025 @03:54PM (#44228713)
I considered a C backend at one time, but C is not expressive enough. The generated code would be ugly. For example, exception handling. D's heavy reliance on common blocks in the object code is another issue. C doesn't support nested functions (static links). And so on.

Never found a user who asked for that, either :-/

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by steveklabnik on Monday June 09, 2025 @04:45PM (#44229287)
Some users want a C backend, not for maintainability reasons, but for the ability to compile on platforms that have nothing but a C compiler. The maintainability or aesthetics of the C is irrelevant, it's like another intermediate representation.
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by WalterBright on Monday June 09, 2025 @07:19PM (#44230699)
I bet it would be easier to write a code gen for such platforms than to wrassal generated C code and work around the endless problems.

Anyhow, one of the curious features of D is its ability to translate C code to D code. Curious as it was never intentionally designed, it was discovered by one of our users.

D has the ability to create a .di file from a .d file, which is analogous to writing a .h file from a .c file. When D gained the ability to compile C files, you just ask it to create a .di file, and voila! the C code translated to D!

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by steveklabnik on Monday June 09, 2025 @10:22PM (#44231825)
Maybe, I don’t use those platforms and so I don’t know from experience, I just know that’s why people asked us in Rust.

I somehow missed that D has that! I try to read the forums now and again, but I should keep more active tabs on how stuff is going :)

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by AndyKelley on Monday June 09, 2025 @05:22PM (#44229666)
Can confirm, Zig's generated C is extremely ugly. We literally treat it as an object format [1].

The MSVC limitations are maddening, from how short string literals must be, to the complete lack of inline assembly when targeting x86_64.

[1]: https://ziglang.org/documentation/0.14.1/std/#std.Target.Obj...

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by miki123211 on Monday June 09, 2025 @06:23AM (#44222949)
I think it would be a good idea to have some kind of "speedbump" tool that makes your software slower, but in a way where optimizing it would also optimize the faster version.

I don't know whether this is technically feasible, maybe you could run it on CPUs with good power management and force them to underclock or something.

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by hiccuphippo on Monday June 09, 2025 @06:33PM (#44230296)
You could use Qemu to emulate an older CPU, you would need to disable kvm with -no-kvm. There's also a throttle option I found while googling this.
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by candrewlee on Monday June 09, 2025 @02:54AM (#44222012)
15s is fast, wow.

Do you have any metrics on which parts of the whole compiler, std, package manager, etc. take the longest to compile? How much does comptime slowness affect the total build time?

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by mlugg on Monday June 09, 2025 @05:29AM (#44222714)
[edited to fix a formatting problem, sorry!]

Well, one interesting number is what happens when you limit the compiler to this feature set:

* Compilation front-end (tokenizing/parsing, IR lowering, semantic analysis)

* Our own ("self-hosted") x86_64 code generator

* Our own ("self-hosted") ELF linker

...so, that's not including the LLVM backend and LLD linker integration, the package manager, `zig build`, etc. Building this subset of the compiler (on the branch which the 15 second figure is from) takes around 9 seconds. So, 6 seconds quicker.

This is essentially a somewhat-educated guess, so it could be EXTREMELY wrong, but of those 6s, I would imagine that around 1-2 are spent on all the other codegen backends and linkers (they aren't too complex and most of them are fairly incomplete), and probably a good 3s or so are from package management, since that pulls in HTTP, TLS, zip+tar, etc. TLS in particular does bring in some of our std.crypto code which sometimes sucks up more compile time than it really should. The remaining few seconds can be attributed to some "everything else" catch-all.

Amusingly, when I did some slightly more in-depth analysis of compiler performance some time ago, I discovered that most of the compiler's time -- at least during semantic analysis -- is spent analyzing different calls to formatted printing (since they're effectively "templated at compile time" in Zig, so the compiler needs to do a non-trivial amount of work for every different-looking call to something like `std.log.info`). That's not actually hugely unreasonable IMO, because formatted printing is a super common operation, but it's an example of an area we could improve on (both in the compiler itself, and in the standard library by simplifying and speeding up `std.fmt`). This is one example of a case where `comptime` execution is a big contributor to compile times.

However, aside from that one caveat of `std.fmt`, I would say that `comptime` slowness isn't a huge deal for many projects. Really, it depends how much they use `comptime`. You can definitely feel the limited speed of `comptime` execution if you use it heavily (e.g. try to parse a big file at `comptime`). However, most codebases are more restrained in their use of `comptime`; it's like a spice, a bit is lovely, but you don't want to overdo it! As with any kind of metaprogramming, overuse of `comptime` can lead to horribly unreadable code, and many major Zig projects have a pretty tasteful approach to using `comptime` in the right places. So for something like the Zig compiler, the speed of `comptime` execution honestly doesn't factor in that much (aside from that `std.fmt` caveat discussed above). `comptime` is very closely tied in to general semantic analysis (things like type checking) in Zig's design, so we can't really draw any kind of clear line, but on the PR I'm taking these measurements against, the threading actually means that even if semantic analysis (i.e. `comptime` execution plus more stuff) were instantaneous, we wouldn't see a ridiculous performance boost, since semantic analysis is now running in parallel to code generation and linking, and those three phases are faiiirly balanced right now in terms of speed.

In general (note that I am biased, since I'm a major contributor to the project!), I find that the Zig compiler is honestly a fair bit faster than people give it credit for. Like, it might sound pretty awful that (even after these improvements), building a "Hello World" takes (for me) around 0.3s -- but unlike C (where libc is precompiled and just needs to be linked, so the C compiler literally has to handle only the `main` you wrote), the Zig compiler is actually freshly building standard library code to handle, for instance, debug info parsing and stack unwinding in the case of a panic (code which is actually sorta complicated!). Right now, you're essentially getting a clean build of these core standard library components every time you build your Zig project (this will be improved upon in the future with incremental compilation). We're still planning to make some huge improvements to compilation speed across the board of course -- as Andrew says, we're really only just getting started with the x86_64 backend -- but I think we've already got something pretty decently fast.

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by 9d on Monday June 09, 2025 @12:34PM (#44226213)
Wait, is this a competition?
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by WalterBright on Monday June 09, 2025 @03:56PM (#44228734)
Always!
Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by 9d on Monday June 09, 2025 @04:22PM (#44229012)
Dang, I'm far behind then, I haven't even downloaded QBE[1] yet!

[2] https://c9x.me/compile/

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by WalterBright on Monday June 09, 2025 @07:20PM (#44230709)
> Dang

Better spelled as Dlang!

Re:Zig Devlog: Self-Hosted x86 Back End Is Now Default in De... by txdv on Monday June 09, 2025 @06:18AM (#44222925)
I wonder if there is a guide to do that.

When I tried compiling zig it would take ages because it would go through different stages (with the entirety of bootstraping from wasm)