Compare commits

...
Author SHA1 Message Date
Niels Lohmann ddb5d366e7 Look up the key only once in the callback parser's key()
Since #5466, key() searched the object for a duplicate key and then looked
the key up again to insert the placeholder. Use a single emplace() instead.
Parsing a 32k-member object with a callback: 10.4 ms -> 7.2 ms (3.12.0: 8.0 ms).

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-14 20:20:59 +02:00
2 changed files with 12 additions and 6 deletions

No files matched your search

+6 -3
View File
@@ -674,16 +674,19 @@ class json_sax_dom_callback_parser
if (keep && ref_stack.back())
{
auto& obj = *ref_stack.back()->m_data.m_value.object;
const auto it = obj.find(val);
if (it != obj.end())
// a single lookup: insert the placeholder, or find the existing member
const auto inserted = obj.emplace(val, discarded);
const auto it = inserted.first;
if (!inserted.second)
{
// this is a duplicate key (legal in JSON); remember its
// current value so it can be restored later if the new
// value is rejected by the callback, instead of being
// erased together with the discarded placeholder
duplicate_key_stash.emplace_back(&(it->second), it->second);
it->second = discarded;
}
object_element = &(obj[val] = discarded);
object_element = &(it->second);
}
return true;
+6 -3
View File
@@ -13850,16 +13850,19 @@ class json_sax_dom_callback_parser
if (keep && ref_stack.back())
{
auto& obj = *ref_stack.back()->m_data.m_value.object;
const auto it = obj.find(val);
if (it != obj.end())
// a single lookup: insert the placeholder, or find the existing member
const auto inserted = obj.emplace(val, discarded);
const auto it = inserted.first;
if (!inserted.second)
{
// this is a duplicate key (legal in JSON); remember its
// current value so it can be restored later if the new
// value is rejected by the callback, instead of being
// erased together with the discarded placeholder
duplicate_key_stash.emplace_back(&(it->second), it->second);
it->second = discarded;
}
object_element = &(obj[val] = discarded);
object_element = &(it->second);
}
return true;