📖 Book · lowlatency · medium

🎚️ Compiler optimization levels: -O0 → -O3, crystal clear

What -O really controls, the same loop in real -O0 vs -O3 assembly, when to use which, how to decide, and the UB that optimization exposes.

A crystal-clear deep dive into compiler optimization levels: what the -O dial controls under the as-if rule, the ladder from -O0 (no opt, best debugging) through -O2 (the release default) to -O3 (auto-vectorization, unrolling — not always faster), plus -Og (debuggable), -Os/-Oz (size) and -Ofast (-O3 + fast-math, breaks IEEE 754). Shows the SAME sum-of-squares loop in real, annotated gcc assembly at -O0 (stack-bound, one int per iteration) versus -O3 -march (auto-vectorized AVX2, eight ints per iteration); runnable constexpr/constant-folding and __OPTIMIZE__ demos; a when-to-use-which guide; a profile-measure-LTO-PGO decision checklist; and the latent-UB gotchas (strict aliasing, uninitialized reads, signed overflow, fast-math) where optimization exposes bugs that 'worked' at -O0 — fix the UB, don't lower the level.

8 chapters▶ runnable code✦ in-depth animation

What this book covers

  1. 01What -O actually controls
  2. 02The ladder, level by level
  3. 03See the difference in the assembly
  4. 04Optimization moves work to compile time
  5. 05When to use which level
  6. 06How to decide — a checklist
  7. 07Optimization exposes latent bugs
  8. 08Cheat sheet

Read it in full — runnable C++ + step-through animations

Every chapter of Compiler optimization levels: -O0 → -O3, crystal clear ships complete, editable code you compile and run in the browser, plus the same in-depth animations as the lessons.