Skip to main content

The Hot Path: Allocation-Free and Copy-Free

advanced11 min readLesson 172 of 204

Reserve, reuse, move, and stringify once โ€” the four habits that remove most runtime cost before touching the compiler flags.

Reserve before you grow

std::vector doubling causes log(n) reallocations, each moving every element. reserve(n) up front makes the hot loop allocation-free. Allocation counters (like this module's) catch offenders instantly.

Reuse buffers across iterations

A buffer allocated inside a loop is a per-iteration tax. Hoist it to the caller, pass by reference, clear with clear() (keeps capacity).

Move, don't copy

std::move for last-use values, emplace_back in-place construction, and returning by value (RVO makes it free). A wrong push_back(x) where push_back(std::move(x)) was meant costs a full deep copy per element.

Copy strings once

Repeated concatenation in a loop is quadratic. Build with reserve + append, or accumulate into a single buffer. The graded exercises here count both allocations and byte-copies, so the quadratic version fails by arithmetic, not by opinion.

Now practice

Practice: Algorithmic WinsReplace sort-and-scan with hashing when the grader counts comparisons โ€” algorithmic wins proven by arithmetic.1 challenge ยท ยท ~14 min