From 072ee2af8336cff8e77f023853bba697bd3e4c05 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 29 Nov 2026 11:31:47 +0100 Subject: [PATCH] Collect up to 64 members of an ordered_json object on the stack The DOM parser collects the members of an ordered_json object on a stack and moves them into a vector of the exact size at the closing brace. Objects with more than 32 members were spilled into a growing vector, which then took the linear-search operator[] until the key index starts at 64 members. Collecting up to 64 members closes that gap: an object either fits on the stack or is spilled straight into the indexed path. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/input/json_sax.hpp | 5 +++-- single_include/nlohmann/json.hpp | 5 +++-- tests/src/unit-ordered_json.cpp | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 9f79202b8..086a1cbed 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -1322,8 +1322,9 @@ class json_sax_dom_parser : public json_sax_dom_error_state } /// the number of members an object collects on the stack before it - /// switches to storing them directly - static constexpr std::size_t object_buffer_limit = 32; + /// switches to storing them directly; this is the size from which the + /// stored object looks up its keys in a hash table (key_index_threshold) + static constexpr std::size_t object_buffer_limit = 64; void reserve_members() { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 44a5b6cd1..cae779ad5 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -17540,8 +17540,9 @@ class json_sax_dom_parser : public json_sax_dom_error_state } /// the number of members an object collects on the stack before it - /// switches to storing them directly - static constexpr std::size_t object_buffer_limit = 32; + /// switches to storing them directly; this is the size from which the + /// stored object looks up its keys in a hash table (key_index_threshold) + static constexpr std::size_t object_buffer_limit = 64; void reserve_members() { diff --git a/tests/src/unit-ordered_json.cpp b/tests/src/unit-ordered_json.cpp index 4d894e649..223c01a8b 100644 --- a/tests/src/unit-ordered_json.cpp +++ b/tests/src/unit-ordered_json.cpp @@ -247,8 +247,8 @@ TEST_CASE("regression test for issue #5673 - ordered_json::emplace with a non-rv namespace { // the sizes around which the DOM parser changes how it stores the elements of -// an array (16) and the members of an object in a vector (32, and 64 for the -// key index) +// an array (16) and the members of an object in a vector (64, which is also +// where the key index starts) const std::array container_sizes = {{0, 1, 2, 3, 15, 16, 17, 18, 31, 32, 33, 34, 63, 64, 65, 66, 200}}; template