Skip to content

Commit 94bf3ba

Browse files
authored
Update 2025-11-30-comptime-c-functions.md
1 parent 3b220bc commit 94bf3ba

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

_posts/2025-11-30-comptime-c-functions.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ It's not actually "stack vs heap" that matters; what matters is whether the comp
3232

3333
The best use-case I can think of for this technique is generating lookup tables at compile-time, since functions like `sin()` *also* successfully get optimized away. This technique seems to also work fine however for implementing runtime-allocated data structures, without needing macros.
3434

35+
I added a `main()` function to the programs to prove that they don't crash on any `assert()` calls at runtime, but even when you remove `main()` the `fn_version()` and `macro_version()` functions get optimized just as hard.
36+
3537
[Link-time optimization](https://en.wikipedia.org/wiki/Interprocedural_optimization) with `-flto` should allow Clang and GCC to perform these optimizations even when the code is split across several object files.
3638

3739
# Generic Stack
3840

39-
Copy of the code on [Compiler Explorer](https://godbolt.org/z/f86naxzE5):
41+
Copy of the code on [Compiler Explorer](https://godbolt.org/z/s1b6xEEqK):
4042

4143
```c
4244
#include <assert.h>
@@ -181,6 +183,11 @@ void macro_version(size_t n) {
181183

182184
assert(stack_empty(&s));
183185
}
186+
187+
int main() {
188+
fn_version(2);
189+
macro_version(2);
190+
}
184191
```
185192
186193
# Generic Hash Map

0 commit comments

Comments
 (0)