Add the members of a diff operation without looking them up

diff_operation() created each operation through three operator[] calls and
assignments. The keys "op", "path" and "value" are distinct and already in
the order of std::map, so they are added with the positional insert that
flatten() uses, constructing the values in place.

diff() (instructions on macOS, Linux Ir GCC 13 / Clang 18): canada 85.3M -> 82.0M,
51.1M -> 50.7M / 55.3M -> 50.2M; citm_catalog 22.0M -> 21.5M, 14.3M -> 14.2M /
14.9M -> 14.2M; twitter 12.4M -> 12.0M, 7.5M unchanged / 8.0M -> 7.5M.
Output is identical.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-22 09:29:51 +02:00
1 parent ba25f96169
commit 39ae5bc7be
2 files changed
+16 -6

No files matched your search

+8 -3
View File
@@ -39899,13 +39899,18 @@ public:
/// @brief append an operation @a op for @a path to @a result, with @a value if it is not null
static void diff_operation(basic_json& result, const char* op, const string_t& path, const basic_json* value)
{
// the keys are distinct and in the order of std::map; the members are
// added without looking them up
basic_json entry(value_t::object);
entry["op"] = op;
entry["path"] = path;
auto& members = *entry.m_data.m_value.object;
typename object_t::iterator hint = members.end();
json_pointer::add_flattened(members, hint, string_t("op"), string_t(op), detail::priority_tag<2> {});
json_pointer::add_flattened(members, hint, string_t("path"), path, detail::priority_tag<2> {});
if (value != nullptr)
{
entry["value"] = *value;
json_pointer::add_flattened(members, hint, string_t("value"), *value, detail::priority_tag<2> {});
}
entry.set_parents();
result.push_back(std::move(entry));
}