C++20’s [[likely]] Attribute - Optimizations, Pessimizations, and [[unlikely]] Consequences
summary
C++20’s [[likely]]/[[unlikely]] changes only when the compiler actually alters code layout or emits different assembly: it affects the compiler branch predictor and can move hot code to reduce instruction-cache misses, but it does not directly change CPU branch predictors on most architectures. Behavior differs by compiler (GCC is aggressive, clang often ignores), compiler flags and linking, and even an identical code folding bug can erase differences. Measure on real inputs and check the generated assembly and perf counters before trusting hints.
AI-written summary · about
chapters (17)
- 0:00Introduction and background
- 1:55Attributes in C++20
- 4:12Compiler specification
- 5:24How compilers use hints
- 8:08Instruction cache impact
- 8:41Switch case analysis
- 12:18Branch prediction mechanics
- 17:13Hardware hint limitations
- 18:24Loop optimization example
- 22:16Compiler differences
- 25:12Exceptions and Kernel code
- 30:04Benchmarking challenges
- 34:08Compiler bugs and isolation
- 37:05Perf tool analysis
- 38:44Summary of findings
- 42:19Future improvements
- 45:27Q&A session
related talks
58mOptimization Tips - Mo' Hustle Mo' Problems
05mCache Warming: Warm Up The Code
01h 05mPerformance Optimization in Software Development - Being Friendly to Your Hardware
01h 02mMaking the Most Out of Your Compiler
01h 04mBranchless Programming in C++
55mCode Size Compiler Optimizations and Techniques for Embedded Systems
56mCompile-Time Compression and Resource Generation with C++20
01h 05mFast and small C++ - When efficiency matters
