Writing fast C code for a modern CPU (and applying it to PostgreSQL)
summary
Modern superscalar CPUs reward predictability: measured on Zen4 and Zen2, branch misprediction stays near 0.03% for 1,000-item patterns but rises to >12% by 25,000 items, doubling loop time; a branchless rewrite and conditional-move use cut mispredictions and halved runtime in microbenchmarks. Hardware prefetching orders give up to 20x slowdown on random access versus sequential. Tools used include perf stat / perf record to find branch misses and Compiler Explorer (Godbolt) to inspect generated asm; PostgreSQL patches and TPCH 5GB tests show modest-to-large gains (one query 10.9s→6.4s).
AI-written summary · about
chapters (10)
- 0:00Introduction and context
- 3:21CPU instruction fundamentals
- 6:16Branch prediction mechanics
- 10:45Testing and measuring branch prediction
- 18:52Mitigating branch misprediction
- 21:51Compiler-based optimizations
- 26:51Data access patterns and caching
- 31:17PostgreSQL memory optimizations
- 37:29Heap page layout experiments
- 40:21Closing remarks and Q&A
related talks
32mHacking Postgres Executor For Performance
50mImproving Postgres' Efficiency
54mx86 Internals for Fun & Profit
48mTricks from in-memory databases
56mPerformance Improvements in PostgreSQL9 2
50mPerformance Archaeology
35mAlgorithmic and microarchitecture optimizations of C++ applications
01h 30mTuning C++: Benchmarks, and CPUs, and Compilers! Oh My!
