From 39ae5bc7bebdcdb9870ed867073a6c27e3dc7edb Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 16 Oct 2026 21:31:20 +0200 Subject: [PATCH] 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 --- include/nlohmann/json.hpp | 11 ++++++++--- single_include/nlohmann/json.hpp | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index cf68bc3f9..9dae01b4f 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -7443,13 +7443,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)); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e53b6ec4c..9bf340c58 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -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)); }