r/cpp • u/TheRavagerSw • Sep 08 '26
How I optimized clang for 1.73x-1.91x performance boost over apt clang
I wanted to have fast test and release builds, so I decided to "upgrade" my compiler.
What I used:
- Musl Libc
- LLVM libc overlay
- Mimalloc
- Thin LTO
- PGO
- Bolt via perf2bolt
- Optimization flags like: -march -O3 -DNDEBUG -fno-semantic-interposition -fno-plt -fno-pie
- No exceptions no RTTI
I tested the build speed with hyperfine with 2 test cases, one is sqlite.c the other is tu including eigen.
| Workload | Build mode | Optimized Clang speedup | Compile-time reduction |
|---|---|---|---|
| Eigen, C++23 | -O0 | 1.69× | 40.8% |
| Eigen, C++23 | -O0 -g3 -fno-omit-frame-pointer | 1.72× | 41.9% |
| Eigen, C++23 | -O3 -DNDEBUG | 1.73× | 42.2% |
| SQLite amalgamation | -O0 | 1.74× | 42.5% |
| SQLite amalgamation | -O0 -g3 -fno-omit-frame-pointer | 1.89× | 47.1% |
| SQLite amalgamation | -O3 -DNDEBUG | 1.73× | 42.2% |
In case anyone is wondering, it also outperformed LLVM release binaries, by about %13 to %15 for each case, but having more performance than the system compiler is more cool so I'm not giving the table for that.
It is also very awesome to have a compiler not depend on glibc. If no errors arise, I'll probably never update my compiler for 2 years at least. As my build is portable(no march native just v3)
I'm planning on moving to cachyOS or any other optimized linux distro next, to finalise my build enviroment. I'm currently on Ubuntu.
12
u/MaitoSnoo [[indeterminate]] Sep 08 '26
do you have a breakdown of what exactly contributed to how much of the perf increase? did you check whether the new clang build produced the exact same binaries (same hashes) for some large open source projects (e.g. Chromium)?
8
u/jeffbailey Sep 08 '26
I love seeing people use LLVM libc for their projects. :)
3
u/yuehuang Sep 09 '26
Can you share Front end and backend number independently? I saw a good boost using mimalloc for the frontend but near zero benefit for the backend. Did you validate binary parity?
3
u/RoyBellingan Sep 08 '26
I am sorry you had a speedup in debug mode ? Compared to what ?