Reserve capacity when converting JSON objects to maps - #5434
Conversation
from_json into unordered_map and other reservable object types emplaced one entry at a time and rehashed as it grew. The array path already reserved; do the same when Container::reserve exists. Signed-off-by: elix3r <157088510+22elix3r@users.noreply.github.com>
| SECTION("std::unordered_map reserve path with many keys") | ||
| { | ||
| json big; | ||
| for (int i = 0; i < 200; ++i) | ||
| { | ||
| big[std::to_string(i)] = i; | ||
| } | ||
| const auto o = big.get<std::unordered_map<std::string, int>>(); | ||
| CHECK(o.size() == 200); | ||
| CHECK(o.at("0") == 0); | ||
| CHECK(o.at("199") == 199); | ||
| } | ||
|
|
There was a problem hiding this comment.
This test is not really testing the reserving part. Maybe you could explicitly check capacity() here?
|
The reserve works — measured on #5406's own benchmark (5,000-entry object → One deviation from the ticket. #5406 asks for the object path to mirror the array path:
This instead introduces a new 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
`from_json` into `std::unordered_map` (and other reservable object types) never called `reserve()`, so a 5k-entry object rehashed repeatedly. The array conversion path already reserves when `reserve` exists.
Related Issue
Fixes #5406
Changes Made
Testing
Commands executed:
Results:
Notes
`std::map` has no `reserve`; the helper is a no-op there.