⚙️ C++ Advanced

What are C++ optimization techniques?

Answer

C++ performance optimization operates at multiple levels: 1. Algorithmic (most impactful): choose O(log n) over O(n), O(n) over O(n²). Profile first — optimize the actual bottleneck, not assumptions. 2. Memory layout (cache efficiency): AoS (Array of Structs) → SoA (Struct of Arrays) for SIMD and cache: // AoS (cache-unfriendly for processing x,y separately): struct Point { float x, y, z; }; Point points[1000]; // SoA (cache-friendly for processing all x values): float x[1000], y[1000], z[1000];. Keep hot data contiguous (std::vector over std::list). Reserve vector capacity upfront. 3. Avoid unnecessary copies: pass by const reference, return by value (RVO), use std::move for temporaries, use emplace_back over push_back. 4. Inlining and constexpr: small functions marked inline or implemented in header avoid call overhead. constexpr moves computation to compile time. 5. Branch prediction: sort data before processing, use likely/unlikely hints: if (__builtin_expect(isHotPath, 1)) { /* ... */ }. C++20: [[likely]] if (condition) { }. 6. SIMD vectorization: use auto-vectorization-friendly loops (simple indexed loops, no aliasing), or use intrinsics/SIMD libraries (xsimd, Eigen) for explicit vectorization. 7. Compiler optimizations: -O2/-O3 enables most optimizations; -march=native enables CPU-specific instructions; LTO (Link Time Optimization) -flto; PGO (Profile-Guided Optimization). 8. Zero-cost abstractions: templates, inlined algorithms, constexpr — generate efficient code with no runtime overhead. The C++ "zero overhead principle." 9. Profiling tools: perf (Linux), Valgrind/Callgrind, Intel VTune, gprof, Google Benchmark for microbenchmarks. 10. Small String Optimization (SSO), Small Buffer Optimization (SBO): store small objects inline without heap allocation.