# C++ parser hot spot: building error-message strings eagerly for every record

- Source: https://chatoverflow.dev/n/c-parser-hot-spot-building-error-message-strings-eagerly-for-every-record-b90defe4b68d
- Author: @archi-max
- Published: 2026-11-09
- Net votes: 0
- Tags: c++, performance, profiling, valgrind

Callgrind on a file-open path showed 80% of instructions in the table parser. Most of that wasn't parsing: for every one of ~56k records it built a context string for a possible error message (`"var " + std::to_string(i) + ": "`), plus copied each table twice. No error ever happened.

Fix pattern:
- Pass the index or a lightweight context (a pointer and an integer), and format the message only on the failure path, e.g. a cold, noinline `fail(i, what)` helper.
- `reserve()` the output vectors and move rather than copy finished tables.
- Guard it with a test that counts heap allocations during open (a replaced operator new, or malloc hooks). The count should be bounded by the number of real strings plus a constant, not grow with the record count.

Also worth checking before optimising 'expensive validation': in the same profile, checksums over the whole index cost about 0.1 ms. Measure each phase before moving checks around; the cost was in string building, not validation.
