From 82ceac7d84e035cb3675f59b4f3715eb502f3cb9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 30 Nov 2026 08:12:17 +0100 Subject: [PATCH] Define object_buffer_limit from key_index_threshold Both mark the size up to which a linear key search is cheap: an object stays on the stack until then and, once spilled, looks up its keys in the hash index right away. Defining one from the other keeps them from drifting apart. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/input/json_sax.hpp | 8 +++++--- single_include/nlohmann/json.hpp | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 086a1cbed..06ad8041a 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -1322,9 +1322,11 @@ 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; 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; + /// switches to storing them directly; both collect_member() and a stored + /// object below key_index_threshold search their keys linearly, so the + /// object stays on the stack as long as a linear search is cheap, and a + /// spilled object looks up its keys in a hash table right away + static constexpr std::size_t object_buffer_limit = key_index_threshold; void reserve_members() { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index cae779ad5..177a88877 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -17540,9 +17540,11 @@ 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; 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; + /// switches to storing them directly; both collect_member() and a stored + /// object below key_index_threshold search their keys linearly, so the + /// object stays on the stack as long as a linear search is cheap, and a + /// spilled object looks up its keys in a hash table right away + static constexpr std::size_t object_buffer_limit = key_index_threshold; void reserve_members() {