mirror of
https://github.com/nlohmann/json.git
synced 2026-10-15 10:47:13 +00:00
Hint the end when converting an object to a standard container
from_json for map-like containers called emplace() without a hint, which searches the tree for every key. The keys of the source object are sorted, so inserting with the end as the hint is constant time, as it was in 3.12.0 via std::inserter. get<std::map<...>>() of a 25,000 entry object: 4.2 ms -> 3.25 ms (3.12.0: 3.7 ms). Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
2 files changed
+10
-2
No files matched your search
@@ -397,9 +397,13 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
|
||||
ConstructibleObjectType ret;
|
||||
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
|
||||
from_json_object_reserve(ret, inner_object->size(), priority_tag<1> {});
|
||||
using value_type = typename ConstructibleObjectType::value_type;
|
||||
for (const auto& p : *inner_object)
|
||||
{
|
||||
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
|
||||
// the keys of the source are sorted for the usual object types, so
|
||||
// hinting the end makes every insertion constant-time (for containers
|
||||
// that order their keys the same way)
|
||||
ret.insert(ret.end(), value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>()));
|
||||
}
|
||||
obj = std::move(ret);
|
||||
}
|
||||
|
||||
Generated
+5
-1
@@ -6101,9 +6101,13 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
|
||||
ConstructibleObjectType ret;
|
||||
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
|
||||
from_json_object_reserve(ret, inner_object->size(), priority_tag<1> {});
|
||||
using value_type = typename ConstructibleObjectType::value_type;
|
||||
for (const auto& p : *inner_object)
|
||||
{
|
||||
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
|
||||
// the keys of the source are sorted for the usual object types, so
|
||||
// hinting the end makes every insertion constant-time (for containers
|
||||
// that order their keys the same way)
|
||||
ret.insert(ret.end(), value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>()));
|
||||
}
|
||||
obj = std::move(ret);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user