News for hackers. Stuff that matters.
Re:Zig 0.9.0 by refulgentis on Monday December 20, 2021 @07:17PM (#29632218)
I had no idea it wasn't in production yet: is there a story for why it consumes so much space on HN? Is the story strong enough already that is a clear alternative to Rust for post-C++ projects?
Re:Zig 0.9.0 by kbd on Monday December 20, 2021 @07:35PM (#29632358)
Zig is full of good ideas and seems to be a truly serious attempt, by very talented developers, at improving the “C level” of the stack. I put my money where my mouth is and have a recurring donation to the Zig Software Foundation every month. Lots of people are excited about it. Dunno why you thought it was “ready for production” at a 0.x version though. They’re actively working on the self-hosted compiler and there are breaking changes every release still fyi.
Re:Zig 0.9.0 by Spex_guy on Monday December 20, 2021 @07:33PM (#29632342)
I can't comment for all the other people who are posting and voting for those posts, but at least for me Zig has quickly become my language of choice for side projects. Its cross compilation features alone are enough for it to replace the system C/C++ compiler toolchains I used to use, and the language itself is everything I'm looking for. Readable (IMO) syntax, proper namespaces, order independent declarations, powerful metaprogramming, and an unmatched level of internal consistency all make it stand out to me. It feels like a massively simplified C++, a native language that improves significantly on C without introducing a massive set of unrelated features.
Re:Zig 0.9.0 by jeremyjh on Monday December 20, 2021 @08:09PM (#29632605)
It seems strange to compare to C++ when it has none of the features that most define C++ like RAII, OOP & templates. Its not really "simplified" - its something totally different.
Re:Zig 0.9.0 by jhgb on Monday December 20, 2021 @08:44PM (#29632849)
Presumably the idea is that lots of things that you basically have to use C++ for today because C alone is weak could be served by Zig instead, not that Zig and C++ are comparable languages.
Re:Zig 0.9.0 by xedrac on Monday December 20, 2021 @09:05PM (#29632967)
I really like Zig, but the lack of RAII means we're back to malloc/free style programming, and I would never opt into that unless Rust could not be used (e.g. binary size too large). Having said that, it's way better than C and I hope it does well.
Re:Zig 0.9.0 by tw102 on Monday December 20, 2021 @09:51PM (#29633250)
Unfortunately malloc-free is unavoidable if you're writing code that needs to allocate memory rather carefully (e.g. guaranteeing no OOMs—Rust has many situations where it'll silently heap alloc and then panic on OOM). Looks like Zig has accepted that it'll be used for those situations and decided to make that experience really good, instead of deciding to be a rustalike or insist on RAII. I think that's an appropriate choice! It makes me excited to use C less.
Re:Zig 0.9.0 by Arnavion on Monday December 20, 2021 @10:51PM (#29633637)
RAII and explicit allocators are independent concerns. That Rust chose a bad default in having 1) a static global allocator, and 2) an expectation of infallible allocations from said global allocator that was then made pervasive in its libstd and third-party ecosystem, has nothing to do with the fact that it has lifetime-based destructors. Having explicit allocators does not mean you need to `free` manually instead of via an automatically-called destructor. A type that allocates needs to ensure a corresponding free in its dtor, and then every other code that uses the type gets the lifetime-based cleanup for free.
Re:Zig 0.9.0 by jhgb on Monday December 20, 2021 @10:04PM (#29633328)
I imagine that Zig has a lot more focus on "I can't use X in my environment" types of situation. It seems that for many such situations it might be a better fit than Rust.
Re:Zig 0.9.0 by ncmncm on Monday December 20, 2021 @11:36PM (#29633934)
There is, for practical purposes, noplace where one can't use C++, except where gatekeepers exercise power keep it out. Typically it would be only a day's work to get those building with a C++ compiler, whence they could begin modernizing.

There are plenty of loadable modules in C++ for Linux and BSDs, and plugins for Postgres, in places where there is no expectation of upstreaming them.

Zig is in a similar boat.

Re:Zig 0.9.0 by jhgb on Tuesday December 21, 2021 @07:10AM (#29636590)
Perhaps you're correct about C++, but I was more referring to the Zig vs. Rust situation.
Re:Zig 0.9.0 by ncmncm on Tuesday December 21, 2021 @12:39PM (#29639852)
Since the Zig tool chain can also compile C, and Zig can use C headers without translation, the cases are more similar than one might otherwise suspect.

But of course it would be less of an upgrade, and the Zig parts would have to stay clearly segregated.

Re:Zig 0.9.0 by kaba0 on Tuesday December 21, 2021 @06:29AM (#29636323)
Not really back to that. Zig just chooses a different way to deal with memory, namely different compilation modes and faster write-compile-test cycles.

It’s a different tradeoff which may be worth it for some use-cases. But it’s definitely not obvious whether it is worse, as the alternative comes with a huge complexity on the language side.

Re:Zig 0.9.0 by jeremyjh on Monday December 20, 2021 @09:32PM (#29633159)
It is possible they will add a type level resource release obligation at some point. It would not be anything quite like the Rust borrow checker but I think would be a big help. https://github.com/ziglang/zig/issues/782
Re:Zig 0.9.0 by kaba0 on Tuesday December 21, 2021 @06:12AM (#29636248)
Well, Zig’s compile time metaprogramming capabilities do rival that of C++’s in a much more simple way, imo.
Re:Zig 0.9.0 by int_19h on Monday December 20, 2021 @11:21PM (#29633826)
It does have comptime, which covers a lot of the same ground as templates (and generics, in other languages).
Re:Zig 0.9.0 by anonymoushn on Tuesday December 21, 2021 @03:39AM (#29635303)
comptime seems like a superset of templates. The target audience was probably not using OOP. People do come by the discord to chat about how nice RAII is though.
Re:Zig 0.9.0 by wtetzner on Monday December 20, 2021 @08:05PM (#29632580)
> is there a story for why it consumes so much space on HN?

People on HN find it interesting.

Re:Zig 0.9.0 by dnautics on Monday December 20, 2021 @08:03PM (#29632562)
It's in production somewhere, but the authors currently discourage it.
Re:Zig 0.9.0 by jorangreef on Tuesday December 21, 2021 @03:15AM (#29635152)
My guess is that Zig's design choices are hitting "a sweeter sweet spot" for systems programming that resonates with many engineers reading HN.

At least for TigerBeetle [1], a distributed database, the story was strong enough even last year that we were prepared to paddle out and wait for the swell to break, rather than be saddled for years with undefined behavior and broken/slow tooling, or else a steep learning curve for the rest of the project's lifetime. We realized that as a new project, our stability roadmaps would probably coincide, and that Zig makes a huge amount of sense for greenfield projects starting out.

The simplicity and readability of Zig is remarkable, which comes down to the emphasis on orthogonality, and this is important when it comes to writing distributed systems.

Appropriating Conway's Law a little loosely, I think it's more difficult (though certainly possible) to arrive at a super simple design for a distributed consensus protocol like Viewstamped Replication, Paxos or Raft, if the language's design is not itself also encouraging simplicity, readability and explicitness in the first place, not to mention a minimum of necessary and excellent abstractions. Because every abstraction carries with it some probability of leaking and undermining the foundations of the system, I feel that whether we make them zero-cost or not is almost besides the point compared to getting the number of abstractions and composition of the system just right.

For example, Zig's comptime encouraged a distributed consensus design where we didn't leak networking/locking/threading throughout the consensus [2] as is commonly the case in many implementations I've read, even in high-level languages like Go. It made things like deterministic fuzzing [3] really the natural solution. People who've worked on some major distributed systems in C++ have commented how refreshing it is to read consensus written in Zig!

Zig also has a different/balanced/all-encompassing approach to safety that resonates more with how I feel about writing safe systems overall: all axes of safety as a spectrum rather than as an extreme (this helps to prevent pursuing one axis of safety at the expense of others), safety also including things like NASA's "The Power of 10: Rules for Developing Safety-Critical Code" [4], assertions, checked arithmetic (this should be enabled by default in safe builds, which it is in Zig), static memory allocation, and compiler checked syscall error handling, the latter of which is really the number one thing by far that makes distributed databases unsafe according to the findings in "An Analysis of Production Failures in Distributed Data-Intensive Systems" [5].

While we could certainly benefit from the muscle of Rust's borrow checker in places, it makes less sense since TigerBeetle's design actively avoids the cost of multi-threading, with a single-threaded control plane for more efficient use of io_uring (zero-copy when moving memory in the hot path), plus static memory allocation and never freeing anything in the lifetime of the system. The new IO APIs like io_uring also encourage a future of single-threaded control planes (outsourcing to the kernel thread pool where threads are cheaper) since context switches are rapidly becoming relatively more expensive. Multi-threading for the sake of I/O is less of a necessary evil these days than it was say 5 years ago.

At some point, the benefits didn't outweigh the costs, and we had to weigh this up. In the end, it came down to simplicity, readability and state-of-the-art tooling.

[1] https://www.tigerbeetle.com

[2] https://github.com/coilhq/tigerbeetle/blob/main/src/vsr/repl...

[3] https://github.com/coilhq/tigerbeetle#simulation-tests

[4] https://web.cecs.pdx.edu/~kimchris/cs201/handouts/The%20Powe...

[5] https://www.usenix.org/system/files/conference/osdi14/osdi14...