~/talk

C++ Performance Tips - Cutting Down on Unnecessary Objects

Talkat C++ on Sea 2025 · 2025-06-23..06-25· [c-and-cpp]
links:none on file
links:none on file
slides:none on file

Extra temporaries cost CPU and heap: examples show a copy of std::string or std::vector causing two heap allocations vs one when moved or constructed in place, and range-for with plain auto producing two copies for a two-element vector. Practical fixes include passing non‑trivial readonly objects by const reference, using std::string_view and std::span to avoid conversions, reserving and using reserve + emplace_back for vectors, preferring in‑place constructors (emplace, piecewise_construct), and enabling diagnostics like -Wall and clang‑tidy performance checks.

AI-written summary · about

  1. 0:00Introduction
  2. 1:07Unnecessary objects hurt performance
  3. 3:14Costly object creation
  4. 6:01When extra objects are acceptable
  5. 7:24Detecting unnecessary objects
  6. 8:07Avoid temporary function args
  7. 9:52Efficient return non-trivial members
  8. 11:59Range-based loops and copies
  9. 13:22Structured bindings and copies
  10. 14:04Named return value optimization pitfalls
  11. 16:57Lambda captures and references
  12. 18:57Capturing variadic arguments by reference
  13. 19:39String view to avoid temps
  14. 21:30Move parameters to avoid copies
  15. 21:52String concatenation with reserve
  16. 23:18String view substr savings
  17. 23:59Reserve vector capacity
  18. 25:03Use span instead of vectors
  19. 26:08Initializer lists and temps
  20. 27:09Pair construction and move
  21. 27:51Tuple construction alternatives
  22. 28:57Optional in-place construction
  23. 29:59Expected error handling
  24. 31:44Variant in-place changes
  25. 33:50Two array vs array
  26. 34:33emplace_back vs push_back
  27. 36:18emplace for map keys
  28. 37:22emplace for map values
  29. 38:29emplace vs try_emplace
  30. 39:13emplace with pair constructors
  31. 40:16insert_or_assign for maps
  32. 41:39Transparent comparators
  33. 44:47Transparent comparators unordered
  34. 46:52Moving data to compile time
  35. 47:56Magic statics and exit destructors
  36. 48:38Compile-time data structures
  37. 49:40Compile-time string concatenation
  38. 50:26Compile-time sets/maps limits
  39. 50:47Key takeaways and final recommendations
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.