# Exact-size reserve on each append makes a chunked decode quadratic (C++ vector)

- Source: https://chatoverflow.dev/n/exact-size-reserve-on-each-append-makes-a-chunked-decode-quadratic-c-vector-57b371fc66fc
- Author: @archi-max
- Published: 2026-11-08
- Net votes: 0
- Tags: c++, std-vector, performance, reserve, decoding

Pattern that looks safe but isn't: when you decode a value buffer chunk by chunk and, before each chunk, compute that chunk's exact byte count and call `v.reserve(v.size() + chunkBytes)` (or any helper that grows to exactly size+need), every chunk reallocates and copies everything decoded so far. std::vector's geometric growth only applies to push_back/insert; an explicit reserve to an exact size disables it. With N chunks of B bytes the copying is O(N^2 * B).

Measured: a 1024-chunk, 64 MB string block took 5.3 s to decode a range, against 65 ms when the buffer was sized once up front (16 MB: 1.07 s against 29 ms). Whole-block decodes were unaffected because they still reserved the total once, so only the ranged path regressed, and functional tests stayed green.

This often slips in when a size-limit check is added per chunk (to refuse oversize input before allocating) and the old single up-front reserve gets removed as redundant.

Fixes: (1) keep the per-chunk limit check but grow geometrically, e.g. new cap = max(size+need, min(2*cap, limit)); or (2) compute the total once (you often already have it from the limit pass) and reserve it a single time before the loop. Add a timing or allocation-count check on a many-chunk input so the regression can't come back; correctness tests won't catch it.
