Skip to content

Commit 7c0eb59

Browse files
authored
Update 2025-11-30-comptime-c-functions.md
1 parent 8ae0090 commit 7c0eb59

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ date: 2025-11-30 12:00:00 +0100
77
Compile-time function execution is great, as it means your program has to run less code at runtime, but what if:
88
1. You're stuck with C.
99
2. You don't want to use evil C macros, as they are debugging nightmares.
10-
3. You want generic data structures that work for all types, and that can use `malloc()` and `free()` internally.
10+
3. You want type-generic data structures that work for all types, and that can use `malloc()` and `free()` internally.
1111

1212
This blog post shows programs where data structures get completely optimized away at compile time by Clang and GCC, using [constant folding](https://en.wikipedia.org/wiki/Constant_folding), [inlining](https://en.wikipedia.org/wiki/Inline_expansion), and [dead code elimination](https://en.wikipedia.org/wiki/Dead-code_elimination). Here is the x86-64 Assembly that their functions get optimized down to:
1313
```nasm
@@ -24,7 +24,7 @@ Embedded systems with tight runtime constraints, and zero-cost abstraction C lib
2424

2525
[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.
2626

27-
# Generic stack
27+
# Stack
2828

2929
I added a `main()` function to the below program, to prove that it doesn't crash on any `assert()` calls at runtime. It's important to note that the `fn_version()` and `macro_version()` functions get optimized just as hard, even when you remove the `main()`.
3030

@@ -186,7 +186,7 @@ int main() {
186186
}
187187
```
188188
189-
# Generic hash map
189+
# Hash map
190190
191191
Because a hash map is a much more complex data structure than a stack, GCC struggles to optimize it perfectly, even with macros and extra flags:
192192
- `-finline-limit=999999`

0 commit comments

Comments
 (0)