Skip to content

Commit 689c323

Browse files
authored
Enhance documentation with optimization example
Added assembly code example demonstrating compile-time optimization by GCC and Clang.
1 parent 6fb0a4c commit 689c323

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ Compile-time function execution is great, but what if:
99
2. You don't want to use evil C macros.
1010
3. You want generic data structures that work for all types.
1111

12-
The below data structure showcase programs get optimized away at compile time by GCC and Clang, such that only the `printf()` at the end of `main()` is left.
12+
The below data structure showcase programs get optimized away at compile time by GCC and Clang, such that only the `printf()` at the end of `main()` is left:
13+
```nasm
14+
main:
15+
push rax
16+
lea rdi, [rip + .Lstr]
17+
call puts@PLT ; printf() got translated to the faster puts()
18+
xor eax, eax ; `xor eax, eax` is a faster `mov eax, 0`; eax is the return value register
19+
pop rcx
20+
ret
21+
```
1322

1423
[Link-time optimization](https://en.wikipedia.org/wiki/Interprocedural_optimization) should in theory allow GCC and Clang to perform this optimization even when the code is split across several object files, but I haven't bothered to test this for the below programs.
1524

0 commit comments

Comments
 (0)