CP

C++ Performance

C++ performance patterns using move semantics, cache-friendly data structures, and compile-time evaluation

Details

Language / Topic
cppC++
Category
Performance

Rules

balanced
- Use move semantics (`std::move`) for expensive-to-copy objects — pass by value and move for sink parameters.
- Prefer `std::vector` and contiguous memory layouts over linked structures (`std::list`) for cache-friendly iteration.
- Use `constexpr` for compile-time computation and `std::string_view` / `std::span` for zero-copy views into existing data.
- Prefer `std::move` when transferring ownership: `void addItem(Item item) { items_.push_back(std::move(item)); }` — avoids a deep copy at the call site.
- Use `std::vector` over `std::list` for sequential data — vector's contiguous layout gives CPU prefetcher a ~10x throughput advantage over pointer-chasing linked nodes.
- Mark functions `constexpr` to move computation to compile time: `constexpr int factorial(int n) { return n <= 1 ? 1 : n * factorial(n - 1); }`.
- Use `std::string_view` and `std::span<T>` as read-only view parameters instead of `const std::string&` or pointer+length pairs — zero allocation, zero copy.
- Reserve vector capacity upfront when the final size is predictable: `v.reserve(expected_count)` eliminates repeated reallocations during `push_back` loops.
- Profile with `perf stat`, `valgrind --tool=callgrind`, or VTune before micro-optimizing — measure the actual bottleneck, not the assumed one.