Reserve SAX array capacity when the length is known - #5435
Conversation
Definite-length CBOR/MessagePack/UBJSON arrays pass the element count to start_array, but the DOM parser only used it for an overflow check. Reserve the vector so decoding does not reallocate log2(N) times. Signed-off-by: elix3r <157088510+22elix3r@users.noreply.github.com>
|
Confirming the review with a run of #5405's own proof-of-concept against this branch — the 5-byte CBOR blob that claims
which is precisely the row the issue tabulates for "unconditional The issue is unusually explicit about this, in a boxed correction added specifically to prevent the uncapped version:
The speedup is real (I measured 21 → 7 allocations per Also flagging a collision: @dexhunter commented on #5405 on 26 Aug offering to take it, with the cap in the plan. Worth coordinating so the work isn't done twice. Generated by Claude Code |
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated and/or formatted correctly. 📎 A ready-to-apply patch is attached to the failed workflow run as the git apply amalgamation.patchThis does not require installing astyle yourself. |
Summary
Binary readers know definite-length array sizes and pass them to `sax->start_array(len)`, but the DOM parser never `reserve()`d the underlying `std::vector`. Large arrays therefore reallocated repeatedly during decode.
Related Issue
Fixes #5405
Changes Made
Testing
Commands executed:
Results:
Notes
Text JSON still passes `unknown_size()` and is unchanged. Objects are `std::map` by default and cannot reserve.