Thoughts

mental health break ,./'"**^^$_---
In Lisp, it's considered bad form to make something a macro if it could be a regular function. In Zig, it's considered bad form to do
something at runtime if you could do it at compile time because the compiler isn't smart enough to perform any optimizations itself so you better just run the whole program at comptime. Make every function inline. Unroll every for loop at comptime. Store your data inside of types just to make sure that they don't take up space at runtime. Include some inline volatile asm that does nothing to prevent the compiler optimizing away your important program logic. Unlike C, Zig has good semantics for stuff like "runtime size" and "compile-time-known constant" but the Zig programmers are so used to the C compiler doing whatever the hell it wants that they don't trust the compiler. So they abuse their newfound power to try to coerce the compiler into generating what they want, but Zig isn't the C compiler. Zig doesn't hate you. I think the best example is this hang function from microzig: https://github.com/ZigEmbeddedGroup/microzig/blob/0c3293fe36167ea9ea1a9d5a0554a6da38d3aac1/core/src/microzig.zig#L65 Zig has semantics that the C compiler doesn't. One of them is the concept of `noreturn`, which lets you tell the compiler that the function will never return. Another one is `asm volatile` which lets you run inline assembly which has side-effects the compiler doesn't know about. The microzig author uses `noreturn` to denote that this function will never return. And then, just for good measure, also includes some volatile assembly to make sure that the loop isn't optimized away because he doesn't trust that the compiler will honor the `noreturn` promise. Edit: 11:33: To clarify the "runtime size" point—variables have a well-defined runtime size, but I think that can be deceptive because as I've alluded to, you can replace a constant variable with a custom type that has zero runtime size. ```zig const a: usize = 8; @compileLog(@sizeOf(@TypeOf(a))) // 8 bytes x += a; // becomes const a = struct { pub val: usize = 8; }; @compileLog(@sizeOf(a)) // a is now a type so its runtime size is 0 bytes x += a.val; ``` TADA! It should be obvious that both of these compile to the same code (especially with optimizations, the constant 8 will be inlined) and you haven't actually saved any memory, but sometimes I'm not so sure that Zig programmers realize this through their excitement about being able to control the runtime size of their variables. (e.g. Microzig having "zero sized" pins)
Link 10:29 a.m. Jun 28, 2024 UTC-4