Hacker News new | ask | show | jobs
by pron 381 days ago
C also creates a distinction between compile-time and run-time, which is more arcane and complicated than that of Zig's, and your code uses it, too: macros (and other pre-processor programming). And there are other distinctions that are more subtle, such as whether the source of a target function is available to the caller's compilation unit or not, static or not etc..

C only seems cleaner and simpler if you already know it well.

1 comments

My point is not about whether compile-time programming is simpler in C or in Zig, but that is in most cases the wrong solution. My example is also not about compile time programming (and does not use macro: https://godbolt.org/z/Mc6M9dK4M), but about letting the optimizer do its job. The end result is then leaner than attempting to write a complicated compile time solution - I would argue.
Right tool for the job. There was no comptime problem shown in the blog.

But if there were zig would prob be simpler since it uses one language that seamlessly weaves comptime and runtime together

I don't know, to me it seems the blog tries to make the case that comptime is useful for low-level optimization: "Is this not amazing? We just used comptime to make a function which compares a string against "Hello!\n", and the assembly will run much faster than the naive comparison function. It's unfortunately still not perfect." But it turns out that a C compiler will give you the "perfect" code directly while the comptime Zig version is fairly complicated. You can argue that this was just a bad example and that there are other examples where comptime makes more sense. The thing is, about two decades ago I was similarly excited about expression-template libraries for very similar reasons. So I can fully understand how the idea of "seamlessly weaves comptime and runtime together" can appear cool. I just realized at some point that it isn't actually all that useful.
> But it turns out that a C compiler will give you the "perfect" code directly while the comptime Zig version is fairly complicated.

In this case both would (or could) give the "perfect" code without any explicit comptime programming.

> I just realized at some point that it isn't actually all that useful.

Except, again, C code often uses macros, which is a more cumbersome mechanism than comptime (and possibly less powerful; see, e.g. how Zig implements printf).

I agree that comptime isn't necessarily very useful for micro optimisation, but that's not what it's for. Being able to shift computations in time is usedful for more "algorithmic" macro optimisations, e.g. parsing things at compile time or generating de/serialization code.

Of course, a compiler could possibly also optimize the Zig code perfectly. The point is that the blogger did not understand it and instead created an overly complex solution which is not actually needed. Most C code I write or review does not use a lot of macros, and where they are used it seems perfectly fine to me.