So, I rewrote my game in C3
42 points by nemin
42 points by nemin
I wonder which of these "better C's" will gain more popularity, there are quite a few of them popping up with bigger non-trivial projects.
They even added a form of incremental-compilation, but it doesn’t remove the long LLVM linking times.
I keep being surpised at reports where the main bottleneck isn't the compiler but some LLVM interaction phase.
For me, the "better C" is called C++. Obviously there are problems with C++ too but the fact that I can take 95 to 99% of C code and just compile it with a C++ compiler is why for me. I can slowly adopt a minimal C++ style where the C code needs clarity.
In practise I doubt any of them are enough better than C to get any widespread adoption.
Is the module system from C3 nice? Yes. Is that enough to move any C dev I know away from C? No. And same goes for zig, hare, etc. They will have their communities of course and they're neat little languages, but widespread adoption the level of C seems unlikely.
Also, I think a lot of C devs don't want to switch. We saw the drama in Linux, it took many Linus hard interventions to get some maintainers to cooperate.
I wonder if it's in part ego: using C correctly is very hard (I would claim impossible even), so seeing yourself as a good C programmer puts you above most programmers, and admitting you could use better safe guards requires humility, self-awareness, and character! (Obviously this isn't the only reason, but maybe related to why those discussions are prone to flame wars - they're deeply personal)
Well, none of the languages give better safe guards either. I think that's a big part of what reduces the appeal: "why take the work to move if all I get is some better syntax" style arguments.
I'd say pride and being used to it. Investing years into learning how to write kernel C and then someone tells you to throw it away. Plus you clearly liked it - I see it as the same as the advent of clankers writing all the code.
I don’t think you’re wrong but we’ve probably reached a point at which new platforms won’t be created in C. Whatever is used instead to create a very successful platform will probably take the crown, in the same way that C probably wouldn’t have achieved total dominance without Unix.
My best guess is that whatever that is will be something really friendly to new architectures, and really friendly to Chinese speakers (and quite possibly to lots of non-english speaking communities).
LLVM's -O0 code generator is slow.
Latest (Nov 27) c3c enables split_modules in -O0 builds, which splits LLVM modules and uses multiple LLVMContext for parallel code generation.
A quick experiment: codegen: 977 ms, IR generation: 776 ms, semantic analysis: 270 ms.
A fast -O0 code generator, such as TPDE and my direct-codegen will help. direct-codegen would decrease "codegen_wall_ms" (--stats=json field).
http_server.c3: 42 modules, 511 functions. Times in ms, median of 5 interleaved runs:
| Configuration | codegen wall off | codegen wall on | ratio |
|---|---|---|---|
| 28 threads, glibc (the earlier number) | 66.7 | 45.6 | 1.46× |
| 28 threads, mimalloc | 54.0 | 35.2 | 1.53× |
--threads 1 |
265.7 | 161.7 | 1.64× |
(The backend ratio is higher at 2.04x, but "codegen wall" also includes O0 pipeline, which direct-codegen doesn't touch.)
c3c heavily uses TLS (31%), ptrtoint (25%), and arithmetic with overflow intrinsics. I have to implement these LLVM IR features to reduce the fallback rate from 59% to 16%.
That said, direct-codegen is vibe-coded and not ready for upstreaming. Accepting it would also mean maintaining another code generator next to SelectionDAG and FastISel. GlobalISel was meant to be faster to compile and hasn't delivered on that.
Seems like it would be easier to fix the Zig experience on Windows than to rewrite the game but I guess C is remotely close enough to Zig.
That meant the memory model needed to be the same (no GC, no RAII)
I’ve never used Zig nor C3 but if there’s anybody here that did, can you explain what’s wrong with RAII and how does Zig code manage resources consistently without it?
The most common criticism I have heard from people who prefer Zig/C/C3/Odin/etc is that constructors and destructors/RAII encourage writing your code in terms of constructing/processing data a single object at a time, while it would be more efficient to handle whole arrays and use ZII (zero-is-initialization i.e. having each object have a default state that is all zero bytes).
Another criticism is that RAII encourages using global heap allocators, when arenas that are tied to some semantic scope of the program (like per-frame, per-request, per-file, etc.) would be faster.
(Personally, I can definitely see how it could encourage the "default" to be a certain way. Although a common conclusion is "and that's why languages with RAII are unworkable" and I can't agree with that, I think it's still totally possible to write code like that in a language with RAII.)
It reminds me of the famous "impedance mismatch" from OOP languages, where the efficient way used by databases is to store all data as dumb structs in flat arrays, but the OOP way wants you to have them grouped into smart objects that have behavior, and so some people invent ORMs to map between the two while other people say you should just give up on using OOP for that.
Great summary! I would add that it's also useful to view memory management strategies as a continuum of control:
You often can do things across levels, like static allocation in a GC language, but that usually requires some amount of going across the grain of the language. For example, RAII in practice pushes you to the API style where containers retain a reference to the allocator, making the more versatile unmanaged API unidiomatic.
That Rust APIs prefer storing references to allocators is not attributable to RAII. Requiring users to pass the correct allocator to every function produces too much opportunity for error and so the alternative arises due to an aversion to footguns.
I am not sure: the fact that you outright can't pass an argument to a destructor seems to loom larger than the chance of messing it up. C++, which pioneered RAII, but is relatively unconcerned with provable correctness, also retains the allocator reference.
I think one might be able to ensure the correct allocator is passed to container methods with some horrifying lifetime branding stuff
Interesting. The more I write lower-level Rust, the more I find myself reaching for arenas, ZII behavior (never heard the term before this) and SoA rather than regular RAII for core data structures. Now that the allocator api is stabilized, hopefully it becomes more safe and idiomatic.
My struggles with RAII are mostly to do with lack of control over how my cleanup code runs. In my case, these are in Rust, but I think they probably translate to most languages with RAII.
More theoretical:
close_range(2) (if the FDs are contiguous) or with io_uring and IORING_OP_CLOSE if they're not.I think the sweet spot is for me is probably using linear types to enforce explicit cleanup with Zig-style defer/errdefer for ergonomics. I'm excited to see how this feels in Valen.
RAII for game stuff and other soft real time stuff can be annoying because you end up allocating and deallocating a bunch when a lot of times you could “just” do nothing or reuse memory slots or whatever
You can do arenas in rust of course but it’s a bunch of futzing about to get to similar levels of safety as zig so if you don’t want the ceremony and don’t care to prove correctness… zig will probably lead to terser code all around
Rust just stabilized the Allocator API! I think that may make arenas easier? (I haven’t been following this super closely, so I may be wrong.)
Yup! I think a bunch of the existing arena crates are expected to update to use it internally. It’s now a lot easier to just define a custom allocator as a one-off too, though you need to take care with deallocation logic, which remains unsafe.
There's defer and errdefer, but it is basically "you're a big smart Real Programmer™, you won't make a simple mistake like forgetting to free a resource".
No, that’s wrong. The idea is to think in terms of lifetimes on a collection level, not thinking about allocating or deallocating individual instances. For example a web server which creates an arena per request and frees all memory allocated during the processing of a request at once. And, by the way, this is how web servers like Apache manage memory today. RAII and implicit memory management like rust makes that kind of memory management strategy unidiomatic, but in a language like Zig it is exactly the way memory is meant to be managed.
But collections don't have RAII either, so I really don't see how that makes any difference.
Not to mention that collections are like 70% of all things that allocate.
The crucial difference is in how a developer is expected to think, and thus structure their code. If you program in a language like Zig the way it is intended, you simply do not have a lot of individual allocations to worry about. Most programs actually don't have a lot of complex lifetimes to deal with. In games, most allocations are frame-bound. In web servers, most allocations are request-bound. If you are building a cache, you invalidate individual cache slots but the allocations managed by the cache are typically large chunks of memory, not individual cache items. ...and so on. An even more extreme example is one of the Zig flagship applications, being tigerbeetle which does not allocate or deallocate at all during runtime. In fact, most short-lived command-line tools and the like can just ignore dynamic memory management and never free anything.
A common issue that I observed in many C++ codebases is that a lot of code gets injected by the language at the end of each scope. While that's not inherent to RAII, it encourages these types of patterns. An extreme example of this would be expensive destructors that are ran by accident in tight loops. While this is fixable without ditching RAII entirely, in languages like Zig or C3 a mistake like this is less likely from my perspective.
how does Zig code manage resources consistently without it?
The go-to solution is including the resource management explicitly in your architecture. For games the popular approach is using object pools and/or arena allocators, while other software like TigerBeetle (a financial database), allocates all resources upfront for better predictability.
I guess one could argue that RAII like in Rust make memory management less explicit and you might shoot yourself in the foot in some hot path by forgetting something something do allocate. Same with implicit Drop when you leave a scope.
In Zig, imho the usual way to make consistent ressources management is to use defer/errdefer. Or use a bump allocator and call free once everything is done.
idk 'bout C3 nor C++'s RAII, but must be similar.
That’s my experience. Implicit allocation is very convenient, especially when writing ‘high-level’ rust, and it’s fine for the great majority of applications, but for things like low-latency servers you have to go out of your way to control allocations and avoid talking to the kernel when possible.
Funny, I watched this guy’s Youtube channel yesterday when he announced he was switching away from WebGPU to NVRHI, and I didn’t expect another major tech change like switching languages.
Oh well, if he’s having fun it’s all good :) I enjoyed C3 when I tried it a while ago, then the language dev figured out his real audience isn’t C developers, so the language will now try to be its own thing rather than a small layer on top of C. Looking forward to see if it can carve a niche that’s becoming quickly saturated (Jai, Odin, Zig, maybe Nim?)