Keep diff()'s recursive levels small and its result elided
diff_recursively built every patch operation in place from initializer lists. Unoptimized builds give each of those temporaries its own stack slot, so every level of the bounded descent cost kilobytes of stack (about 6 KB with clang -O0), and the 128 recursive levels overflowed the 1 MB stack of MSVC Debug in the "deeply nested values" test. The operations and the key comparison of two objects are now built by separate functions, which diff_iteratively shares, and both diff functions append to one result instead of returning a patch per level that the caller copies. With clang -O0, diffing values nested 300 levels deep now peaks at about 190 KB of stack instead of 880 KB. Since diff() now owns the only returned value, clang's -Wnrvo no longer reports the returns of diff_recursively, which alternated between the local patch and diff_iteratively's result. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
2 files changed
+330
-566
No files matched your search
+165
-283
@@ -5994,7 +5994,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
static basic_json diff(const basic_json& source, const basic_json& target,
|
static basic_json diff(const basic_json& source, const basic_json& target,
|
||||||
const string_t& path = "")
|
const string_t& path = "")
|
||||||
{
|
{
|
||||||
return diff_recursively(source, target, path, 0);
|
// the patch
|
||||||
|
basic_json result(value_t::array);
|
||||||
|
diff_recursively(result, source, target, path, 0);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -6032,87 +6035,73 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json added_ops{}; // NOLINT(readability-redundant-member-init)
|
basic_json added_ops{}; // NOLINT(readability-redundant-member-init)
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
// The operations of a diff are built by the functions below rather than
|
||||||
@brief @ref diff, for values at nesting level @a depth
|
// where they are needed: building one takes several temporaries, and
|
||||||
|
// unoptimized builds give each temporary a stack slot of its own in the
|
||||||
|
// function it appears in. In diff_recursively, which is on the call stack
|
||||||
|
// once per nesting level, that made every level cost kilobytes of stack.
|
||||||
|
|
||||||
Diffing two arrays or objects calls this function again, once per nesting
|
/// @brief append a "replace" operation for @a path with @a value to @a result
|
||||||
level, so values nested deeply enough used to exhaust the call stack and
|
static void diff_replace(basic_json& result, const string_t& path, const basic_json& value)
|
||||||
terminate the process. The descent is bounded here: once @ref
|
|
||||||
detail::recursion_depth_limit levels have been entered, @ref
|
|
||||||
diff_iteratively diffs what is left without the call stack.
|
|
||||||
*/
|
|
||||||
static basic_json diff_recursively(const basic_json& source, const basic_json& target,
|
|
||||||
const string_t& path, const std::size_t depth)
|
|
||||||
{
|
{
|
||||||
// the patch
|
|
||||||
basic_json result(value_t::array);
|
|
||||||
|
|
||||||
// if the values are the same, return an empty patch
|
|
||||||
if (source == target)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(depth >= detail::recursion_depth_limit()))
|
|
||||||
{
|
|
||||||
return diff_iteratively(source, target, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (source.type() != target.type())
|
|
||||||
{
|
|
||||||
// different types: replace value
|
|
||||||
result.push_back(
|
result.push_back(
|
||||||
{
|
{
|
||||||
{"op", "replace"}, {"path", path}, {"value", target}
|
{"op", "replace"}, {"path", path}, {"value", value}
|
||||||
});
|
});
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (source.type())
|
/// @brief append a "remove" operation for @a path to @a result
|
||||||
{
|
static void diff_remove(basic_json& result, const string_t& path)
|
||||||
case value_t::array:
|
|
||||||
{
|
|
||||||
// first pass: traverse common elements
|
|
||||||
std::size_t i = 0;
|
|
||||||
while (i < source.size() && i < target.size())
|
|
||||||
{
|
|
||||||
// recursive call to compare array values at index i
|
|
||||||
auto temp_diff = diff_recursively(source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)), depth + 1);
|
|
||||||
result.insert(result.end(), temp_diff.begin(), temp_diff.end());
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We now reached the end of at least one array
|
|
||||||
// in a second pass, traverse the remaining elements
|
|
||||||
|
|
||||||
// remove my remaining elements, highest index first; appending
|
|
||||||
// in that order avoids the quadratic reinsertion done before
|
|
||||||
for (std::size_t j = source.size(); j > i; --j)
|
|
||||||
{
|
{
|
||||||
result.push_back(object(
|
result.push_back(object(
|
||||||
{
|
{
|
||||||
{"op", "remove"},
|
{"op", "remove"}, {"path", path}
|
||||||
{"path", detail::concat<string_t>(path, '/', detail::to_string<string_t>(j - 1))}
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
i = source.size();
|
|
||||||
|
|
||||||
// add other remaining elements
|
/// @brief append an "add" operation for @a path with @a value to @a result
|
||||||
while (i < target.size())
|
static void diff_add(basic_json& result, const string_t& path, const basic_json& value)
|
||||||
{
|
{
|
||||||
result.push_back(
|
result.push_back(
|
||||||
{
|
{
|
||||||
{"op", "add"},
|
{"op", "add"}, {"path", path}, {"value", value}
|
||||||
{"path", detail::concat<string_t>(path, "/-")},
|
|
||||||
{"value", target[i]}
|
|
||||||
});
|
});
|
||||||
++i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
/// @brief append the "remove" operations for the elements of array
|
||||||
|
/// @a source from @a index on, and the "add" operations for the
|
||||||
|
/// elements of array @a target from source's size on, to @a result
|
||||||
|
static void diff_array_tails(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path, const std::size_t index)
|
||||||
|
{
|
||||||
|
// remove my remaining elements, highest index first; appending
|
||||||
|
// in that order avoids the quadratic reinsertion done before
|
||||||
|
for (std::size_t j = source.size(); j > index; --j)
|
||||||
|
{
|
||||||
|
diff_remove(result, detail::concat<string_t>(path, '/', detail::to_string<string_t>(j - 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
case value_t::object:
|
// add other remaining elements
|
||||||
|
for (std::size_t i = source.size(); i < target.size(); ++i)
|
||||||
|
{
|
||||||
|
diff_add(result, detail::concat<string_t>(path, "/-"), target[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief compare the keys of objects @a source and @a target
|
||||||
|
|
||||||
|
If the keys both objects have are in the same order in both, and the keys
|
||||||
|
only @a target has come after them, stores the keys common to both in
|
||||||
|
source's order in @a common_keys, stores the "add" operations for the keys
|
||||||
|
only @a target has in @a added_ops, and returns true: the caller then diffs
|
||||||
|
the objects member by member. Otherwise, appends operations that remove
|
||||||
|
every member of @a source and add every member of @a target to @a result,
|
||||||
|
and returns false.
|
||||||
|
*/
|
||||||
|
static bool diff_object_keys(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path, std::vector<typename object_t::key_type>& common_keys,
|
||||||
|
basic_json& added_ops)
|
||||||
{
|
{
|
||||||
// first pass: record, for every source key, whether it is
|
// first pass: record, for every source key, whether it is
|
||||||
// common to both objects (in source's iteration order) or
|
// common to both objects (in source's iteration order) or
|
||||||
@@ -6120,10 +6109,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// a by-product of the target.find() call already needed to
|
// a by-product of the target.find() call already needed to
|
||||||
// tell the two cases apart, so it adds no extra lookups. The
|
// tell the two cases apart, so it adds no extra lookups. The
|
||||||
// "remove" ops themselves are emitted later, interleaved
|
// "remove" ops themselves are emitted later, interleaved
|
||||||
// with the recursive per-key diffs in the fast path below,
|
// with the per-key diffs in the caller's fast path, to match
|
||||||
// to match source's original iteration order (as the
|
// source's original iteration order (as the original,
|
||||||
// original, pre-reordering-aware implementation did) instead
|
// pre-reordering-aware implementation did) instead of
|
||||||
// of grouping all removes before all recursive diffs.
|
// grouping all removes before all per-key diffs.
|
||||||
std::vector<typename object_t::key_type> common_keys_source_order;
|
std::vector<typename object_t::key_type> common_keys_source_order;
|
||||||
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
||||||
{
|
{
|
||||||
@@ -6139,20 +6128,19 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// source.find() call already needed to detect added keys. At
|
// source.find() call already needed to detect added keys. At
|
||||||
// the same time, determine whether every added key comes
|
// the same time, determine whether every added key comes
|
||||||
// after every common key in target's order (a precondition
|
// after every common key in target's order (a precondition
|
||||||
// for the fast path below, which only ever appends new keys
|
// for the fast path, which only ever appends new keys
|
||||||
// at the very end): for an object_t whose iteration order is
|
// at the very end): for an object_t whose iteration order is
|
||||||
// a pure function of the key set (e.g. the default std::map,
|
// a pure function of the key set (e.g. the default std::map,
|
||||||
// which always iterates in sorted key order), the order
|
// which always iterates in sorted key order), the order
|
||||||
// check further below is always true and this whole
|
// check further below is always true and this whole
|
||||||
// mechanism is effectively a no-op; it only matters for a
|
// mechanism is effectively a no-op; it only matters for a
|
||||||
// reorderable object_t such as the one backing `ordered_json`.
|
// reorderable object_t such as the one backing `ordered_json`.
|
||||||
// patch ops for keys that were added (i.e., in target but not
|
// The patch ops for keys that were added (i.e., in target but not
|
||||||
// in source); built here so the fast path below can reuse
|
// in source) are built here so the fast path can reuse
|
||||||
// them without a second source.find() per target key. Only
|
// them without a second source.find() per target key. Only
|
||||||
// used by the fast path -- the slow (reordering) path
|
// used by the fast path -- the slow (reordering) path
|
||||||
// rebuilds "add" ops for every key itself.
|
// rebuilds "add" ops for every key itself.
|
||||||
std::vector<typename object_t::key_type> common_keys_target_order;
|
std::vector<typename object_t::key_type> common_keys_target_order;
|
||||||
basic_json added_ops(value_t::array);
|
|
||||||
bool new_keys_form_suffix = true;
|
bool new_keys_form_suffix = true;
|
||||||
bool seen_new_key = false;
|
bool seen_new_key = false;
|
||||||
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
||||||
@@ -6160,12 +6148,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
if (source.find(it.key()) == source.end())
|
if (source.find(it.key()) == source.end())
|
||||||
{
|
{
|
||||||
seen_new_key = true;
|
seen_new_key = true;
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
diff_add(added_ops, detail::concat<string_t>(path, '/', detail::escape(it.key())), it.value());
|
||||||
added_ops.push_back(
|
|
||||||
{
|
|
||||||
{"op", "add"}, {"path", path_key},
|
|
||||||
{"value", it.value()}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -6181,43 +6164,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
{
|
{
|
||||||
// fast path: order of common keys already matches (or the
|
// fast path: order of common keys already matches (or the
|
||||||
// object_t's iteration order does not depend on
|
// object_t's iteration order does not depend on
|
||||||
// insertion history), so a plain per-key recursive diff
|
// insertion history), so a plain per-key diff is correct
|
||||||
// is correct and minimal, as before. common_keys_source_order
|
// and minimal, as before
|
||||||
// is, by construction, the subsequence of source's keys
|
common_keys = std::move(common_keys_source_order);
|
||||||
// that are common to both objects, in source's iteration
|
return true;
|
||||||
// order -- so it can be walked in lockstep with `source`
|
|
||||||
// using a cheap key comparison instead of another lookup.
|
|
||||||
// Deleted keys (those source keys not in common_keys_source_order)
|
|
||||||
// are interleaved here too, in source's original order, to
|
|
||||||
// match the historical (pre-reordering-aware) output order.
|
|
||||||
auto common_it = common_keys_source_order.cbegin();
|
|
||||||
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
|
||||||
{
|
|
||||||
if (common_it != common_keys_source_order.cend() && it.key() == *common_it)
|
|
||||||
{
|
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
|
||||||
auto temp_diff = diff_recursively(it.value(), target[it.key()], path_key, depth + 1);
|
|
||||||
result.insert(result.end(), temp_diff.begin(), temp_diff.end());
|
|
||||||
++common_it;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// found a key that is not in target -> remove it
|
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"}, {"path", path_key}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// append the "add" ops for brand-new keys collected above
|
|
||||||
// during the pass over target -- no second source.find()
|
|
||||||
// per target key needed
|
|
||||||
result.insert(result.end(), added_ops.begin(), added_ops.end());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// slow path: the common keys are in a different relative
|
// slow path: the common keys are in a different relative
|
||||||
// order in source and target (only possible for a
|
// order in source and target (only possible for a
|
||||||
// reorderable object_t like ordered_map). Building a
|
// reorderable object_t like ordered_map). Building a
|
||||||
@@ -6234,11 +6186,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// it moves it to the end, fixing its position.
|
// it moves it to the end, fixing its position.
|
||||||
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
||||||
{
|
{
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
diff_remove(result, detail::concat<string_t>(path, '/', detail::escape(it.key())));
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"}, {"path", path_key}
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add every key that is either common (just removed
|
// add every key that is either common (just removed
|
||||||
@@ -6247,15 +6195,96 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// target exactly
|
// target exactly
|
||||||
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
||||||
{
|
{
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
diff_add(result, detail::concat<string_t>(path, '/', detail::escape(it.key())), it.value());
|
||||||
result.push_back(
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief @ref diff, for values at nesting level @a depth, appending the
|
||||||
|
operations to @a result
|
||||||
|
|
||||||
|
Diffing two arrays or objects calls this function again, once per nesting
|
||||||
|
level, so values nested deeply enough used to exhaust the call stack and
|
||||||
|
terminate the process. The descent is bounded here: once @ref
|
||||||
|
detail::recursion_depth_limit levels have been entered, @ref
|
||||||
|
diff_iteratively diffs what is left without the call stack.
|
||||||
|
*/
|
||||||
|
static void diff_recursively(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path, const std::size_t depth)
|
||||||
{
|
{
|
||||||
{"op", "add"}, {"path", path_key},
|
// if the values are the same, there is nothing to do
|
||||||
{"value", it.value()}
|
if (source == target)
|
||||||
});
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(depth >= detail::recursion_depth_limit()))
|
||||||
|
{
|
||||||
|
diff_iteratively(result, source, target, path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (source.type() != target.type())
|
||||||
|
{
|
||||||
|
// different types: replace value
|
||||||
|
diff_replace(result, path, target);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (source.type())
|
||||||
|
{
|
||||||
|
case value_t::array:
|
||||||
|
{
|
||||||
|
// first pass: traverse common elements
|
||||||
|
std::size_t i = 0;
|
||||||
|
while (i < source.size() && i < target.size())
|
||||||
|
{
|
||||||
|
// recursive call to compare array values at index i
|
||||||
|
diff_recursively(result, source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)), depth + 1);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We now reached the end of at least one array
|
||||||
|
// in a second pass, traverse the remaining elements
|
||||||
|
diff_array_tails(result, source, target, path, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case value_t::object:
|
||||||
|
{
|
||||||
|
std::vector<typename object_t::key_type> common_keys;
|
||||||
|
basic_json added_ops(value_t::array);
|
||||||
|
if (diff_object_keys(result, source, target, path, common_keys, added_ops))
|
||||||
|
{
|
||||||
|
// fast path: common_keys is, by construction, the
|
||||||
|
// subsequence of source's keys that are common to both
|
||||||
|
// objects, in source's iteration order -- so it can be
|
||||||
|
// walked in lockstep with `source` using a cheap key
|
||||||
|
// comparison instead of another lookup. Deleted keys
|
||||||
|
// (those source keys not in common_keys) are interleaved
|
||||||
|
// here too, in source's original order, to match the
|
||||||
|
// historical (pre-reordering-aware) output order.
|
||||||
|
auto common_it = common_keys.cbegin();
|
||||||
|
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
||||||
|
{
|
||||||
|
if (common_it != common_keys.cend() && it.key() == *common_it)
|
||||||
|
{
|
||||||
|
diff_recursively(result, it.value(), target[it.key()], detail::concat<string_t>(path, '/', detail::escape(it.key())), depth + 1);
|
||||||
|
++common_it;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// found a key that is not in target -> remove it
|
||||||
|
diff_remove(result, detail::concat<string_t>(path, '/', detail::escape(it.key())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// append the "add" ops for brand-new keys collected by
|
||||||
|
// diff_object_keys -- no second source.find() per target
|
||||||
|
// key needed
|
||||||
|
result.insert(result.end(), added_ops.begin(), added_ops.end());
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6270,29 +6299,22 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
// both primitive types: replace value
|
// both primitive types: replace value
|
||||||
result.push_back(
|
diff_replace(result, path, target);
|
||||||
{
|
|
||||||
{"op", "replace"}, {"path", path}, {"value", target}
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief @ref diff without the call stack
|
@brief @ref diff without the call stack, appending the operations to
|
||||||
|
@a result
|
||||||
|
|
||||||
Produces the same patch as @ref diff_recursively. Only reached for values
|
Produces the same operations as @ref diff_recursively. Only reached for
|
||||||
nested more deeply than @ref detail::recursion_depth_limit.
|
values nested more deeply than @ref detail::recursion_depth_limit.
|
||||||
*/
|
*/
|
||||||
static basic_json diff_iteratively(const basic_json& source, const basic_json& target,
|
static void diff_iteratively(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
const string_t& path)
|
const string_t& path)
|
||||||
{
|
{
|
||||||
// the patch
|
|
||||||
basic_json result(value_t::array);
|
|
||||||
|
|
||||||
// The arrays and objects being diffed are kept on an explicit stack,
|
// The arrays and objects being diffed are kept on an explicit stack,
|
||||||
// and every pair of elements is still diffed completely before the
|
// and every pair of elements is still diffed completely before the
|
||||||
// next one, so the operations come out in the same order as in
|
// next one, so the operations come out in the same order as in
|
||||||
@@ -6320,10 +6342,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
if (s.type() != t.type())
|
if (s.type() != t.type())
|
||||||
{
|
{
|
||||||
// different types: replace value
|
// different types: replace value
|
||||||
result.push_back(
|
diff_replace(result, current_path, t);
|
||||||
{
|
|
||||||
{"op", "replace"}, {"path", current_path}, {"value", t}
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6337,124 +6356,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
case value_t::object:
|
case value_t::object:
|
||||||
{
|
{
|
||||||
// first pass: record, for every source key, whether it is
|
std::vector<typename object_t::key_type> common_keys;
|
||||||
// common to both objects (in source's iteration order) or
|
|
||||||
// was deleted (i.e., in source but not in target) -- this is
|
|
||||||
// a by-product of the t.find() call already needed to
|
|
||||||
// tell the two cases apart, so it adds no extra lookups. The
|
|
||||||
// "remove" ops themselves are emitted later, interleaved
|
|
||||||
// with the per-key diffs in the fast path below, to match
|
|
||||||
// source's original iteration order (as the original,
|
|
||||||
// pre-reordering-aware implementation did) instead of
|
|
||||||
// grouping all removes before all per-key diffs.
|
|
||||||
std::vector<typename object_t::key_type> common_keys_source_order;
|
|
||||||
for (auto it = s.cbegin(); it != s.cend(); ++it)
|
|
||||||
{
|
|
||||||
if (t.find(it.key()) != t.end())
|
|
||||||
{
|
|
||||||
common_keys_source_order.push_back(it.key());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// second pass: find keys that were added (i.e., in target but
|
|
||||||
// not in source), and record the keys common to both, in
|
|
||||||
// target's iteration order -- again a by-product of the
|
|
||||||
// s.find() call already needed to detect added keys. At
|
|
||||||
// the same time, determine whether every added key comes
|
|
||||||
// after every common key in target's order (a precondition
|
|
||||||
// for the fast path below, which only ever appends new keys
|
|
||||||
// at the very end): for an object_t whose iteration order is
|
|
||||||
// a pure function of the key set (e.g. the default std::map,
|
|
||||||
// which always iterates in sorted key order), the order
|
|
||||||
// check further below is always true and this whole
|
|
||||||
// mechanism is effectively a no-op; it only matters for a
|
|
||||||
// reorderable object_t such as the one backing `ordered_json`.
|
|
||||||
// patch ops for keys that were added (i.e., in target but not
|
|
||||||
// in source); built here so the fast path below can reuse
|
|
||||||
// them without a second s.find() per target key. Only
|
|
||||||
// used by the fast path -- the slow (reordering) path
|
|
||||||
// rebuilds "add" ops for every key itself.
|
|
||||||
std::vector<typename object_t::key_type> common_keys_target_order;
|
|
||||||
basic_json added_ops(value_t::array);
|
basic_json added_ops(value_t::array);
|
||||||
bool new_keys_form_suffix = true;
|
if (diff_object_keys(result, s, t, current_path, common_keys, added_ops))
|
||||||
bool seen_new_key = false;
|
|
||||||
for (auto it = t.cbegin(); it != t.cend(); ++it)
|
|
||||||
{
|
{
|
||||||
if (s.find(it.key()) == s.end())
|
// fast path: the frame walks source in lockstep with
|
||||||
{
|
// common_keys, as diff_recursively does, and appends
|
||||||
seen_new_key = true;
|
// added_ops once all members are done
|
||||||
const auto path_key = detail::concat<string_t>(current_path, '/', detail::escape(it.key()));
|
|
||||||
added_ops.push_back(
|
|
||||||
{
|
|
||||||
{"op", "add"}, {"path", path_key},
|
|
||||||
{"value", it.value()}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
common_keys_target_order.push_back(it.key());
|
|
||||||
if (seen_new_key)
|
|
||||||
{
|
|
||||||
new_keys_form_suffix = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (common_keys_source_order == common_keys_target_order && new_keys_form_suffix)
|
|
||||||
{
|
|
||||||
// fast path: order of common keys already matches (or the
|
|
||||||
// object_t's iteration order does not depend on
|
|
||||||
// insertion history), so a plain per-key diff is correct
|
|
||||||
// and minimal, as before. The frame walks source in
|
|
||||||
// lockstep with common_keys_source_order, which is, by
|
|
||||||
// construction, the subsequence of source's keys that
|
|
||||||
// are common to both objects, in source's iteration
|
|
||||||
// order -- so a cheap key comparison replaces another
|
|
||||||
// lookup. Deleted keys are interleaved there too, in
|
|
||||||
// source's original order, and the "add" ops collected
|
|
||||||
// above are appended once all members are done.
|
|
||||||
stack.emplace_back(&s, &t, current_path.size());
|
stack.emplace_back(&s, &t, current_path.size());
|
||||||
stack.back().member = s.cbegin();
|
stack.back().member = s.cbegin();
|
||||||
stack.back().common_keys = std::move(common_keys_source_order);
|
stack.back().common_keys = std::move(common_keys);
|
||||||
stack.back().added_ops = std::move(added_ops);
|
stack.back().added_ops = std::move(added_ops);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// slow path: the common keys are in a different relative
|
|
||||||
// order in source and target (only possible for a
|
|
||||||
// reorderable object_t like ordered_map). Building a
|
|
||||||
// minimal reordering patch is a nontrivial (LCS-like)
|
|
||||||
// problem; instead, remove every source key -- both
|
|
||||||
// deleted keys (which must be removed regardless) and
|
|
||||||
// common keys (removed so they can be re-added in
|
|
||||||
// target's order) -- and re-add every key that should
|
|
||||||
// remain, with its final target value, in target's
|
|
||||||
// order. basic_json::patch()'s "add" operation on an
|
|
||||||
// object uses operator[], which appends at the end for a
|
|
||||||
// vector-backed insertion-ordered map when the key does
|
|
||||||
// not already exist -- so removing a key and then adding
|
|
||||||
// it moves it to the end, fixing its position.
|
|
||||||
for (auto it = s.cbegin(); it != s.cend(); ++it)
|
|
||||||
{
|
|
||||||
const auto path_key = detail::concat<string_t>(current_path, '/', detail::escape(it.key()));
|
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"}, {"path", path_key}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// add every key that is either common (just removed
|
|
||||||
// above) or brand new, in target's iteration order, so
|
|
||||||
// that the final order after applying the patch matches
|
|
||||||
// target exactly
|
|
||||||
for (auto it = t.cbegin(); it != t.cend(); ++it)
|
|
||||||
{
|
|
||||||
const auto path_key = detail::concat<string_t>(current_path, '/', detail::escape(it.key()));
|
|
||||||
result.push_back(
|
|
||||||
{
|
|
||||||
{"op", "add"}, {"path", path_key},
|
|
||||||
{"value", it.value()}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -6470,10 +6382,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
// both primitive types: replace value
|
// both primitive types: replace value
|
||||||
result.push_back(
|
diff_replace(result, current_path, t);
|
||||||
{
|
|
||||||
{"op", "replace"}, {"path", current_path}, {"value", t}
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6508,28 +6417,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
// We now reached the end of at least one array
|
// We now reached the end of at least one array
|
||||||
// in a second pass, traverse the remaining elements
|
// in a second pass, traverse the remaining elements
|
||||||
|
diff_array_tails(result, *frame.source, *frame.target, current_path, frame.index);
|
||||||
// remove my remaining elements, highest index first; appending
|
|
||||||
// in that order avoids the quadratic reinsertion done before
|
|
||||||
for (std::size_t j = source_array.size(); j > frame.index; --j)
|
|
||||||
{
|
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"},
|
|
||||||
{"path", detail::concat<string_t>(current_path, '/', detail::to_string<string_t>(j - 1))}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// add other remaining elements
|
|
||||||
for (std::size_t i = source_array.size(); i < target_array.size(); ++i)
|
|
||||||
{
|
|
||||||
result.push_back(
|
|
||||||
{
|
|
||||||
{"op", "add"},
|
|
||||||
{"path", detail::concat<string_t>(current_path, "/-")},
|
|
||||||
{"value", target_array[i]}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -6551,11 +6439,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// found a key that is not in target -> remove it
|
// found a key that is not in target -> remove it
|
||||||
const auto path_key = detail::concat<string_t>(current_path, '/', detail::escape(it.key()));
|
diff_remove(result, detail::concat<string_t>(current_path, '/', detail::escape(it.key())));
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"}, {"path", path_key}
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -6572,8 +6456,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
current_path.resize(stack.back().path_length);
|
current_path.resize(stack.back().path_length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
+165
-283
@@ -30952,7 +30952,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
static basic_json diff(const basic_json& source, const basic_json& target,
|
static basic_json diff(const basic_json& source, const basic_json& target,
|
||||||
const string_t& path = "")
|
const string_t& path = "")
|
||||||
{
|
{
|
||||||
return diff_recursively(source, target, path, 0);
|
// the patch
|
||||||
|
basic_json result(value_t::array);
|
||||||
|
diff_recursively(result, source, target, path, 0);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -30990,87 +30993,73 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json added_ops{}; // NOLINT(readability-redundant-member-init)
|
basic_json added_ops{}; // NOLINT(readability-redundant-member-init)
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
// The operations of a diff are built by the functions below rather than
|
||||||
@brief @ref diff, for values at nesting level @a depth
|
// where they are needed: building one takes several temporaries, and
|
||||||
|
// unoptimized builds give each temporary a stack slot of its own in the
|
||||||
|
// function it appears in. In diff_recursively, which is on the call stack
|
||||||
|
// once per nesting level, that made every level cost kilobytes of stack.
|
||||||
|
|
||||||
Diffing two arrays or objects calls this function again, once per nesting
|
/// @brief append a "replace" operation for @a path with @a value to @a result
|
||||||
level, so values nested deeply enough used to exhaust the call stack and
|
static void diff_replace(basic_json& result, const string_t& path, const basic_json& value)
|
||||||
terminate the process. The descent is bounded here: once @ref
|
|
||||||
detail::recursion_depth_limit levels have been entered, @ref
|
|
||||||
diff_iteratively diffs what is left without the call stack.
|
|
||||||
*/
|
|
||||||
static basic_json diff_recursively(const basic_json& source, const basic_json& target,
|
|
||||||
const string_t& path, const std::size_t depth)
|
|
||||||
{
|
{
|
||||||
// the patch
|
|
||||||
basic_json result(value_t::array);
|
|
||||||
|
|
||||||
// if the values are the same, return an empty patch
|
|
||||||
if (source == target)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(depth >= detail::recursion_depth_limit()))
|
|
||||||
{
|
|
||||||
return diff_iteratively(source, target, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (source.type() != target.type())
|
|
||||||
{
|
|
||||||
// different types: replace value
|
|
||||||
result.push_back(
|
result.push_back(
|
||||||
{
|
{
|
||||||
{"op", "replace"}, {"path", path}, {"value", target}
|
{"op", "replace"}, {"path", path}, {"value", value}
|
||||||
});
|
});
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (source.type())
|
/// @brief append a "remove" operation for @a path to @a result
|
||||||
{
|
static void diff_remove(basic_json& result, const string_t& path)
|
||||||
case value_t::array:
|
|
||||||
{
|
|
||||||
// first pass: traverse common elements
|
|
||||||
std::size_t i = 0;
|
|
||||||
while (i < source.size() && i < target.size())
|
|
||||||
{
|
|
||||||
// recursive call to compare array values at index i
|
|
||||||
auto temp_diff = diff_recursively(source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)), depth + 1);
|
|
||||||
result.insert(result.end(), temp_diff.begin(), temp_diff.end());
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We now reached the end of at least one array
|
|
||||||
// in a second pass, traverse the remaining elements
|
|
||||||
|
|
||||||
// remove my remaining elements, highest index first; appending
|
|
||||||
// in that order avoids the quadratic reinsertion done before
|
|
||||||
for (std::size_t j = source.size(); j > i; --j)
|
|
||||||
{
|
{
|
||||||
result.push_back(object(
|
result.push_back(object(
|
||||||
{
|
{
|
||||||
{"op", "remove"},
|
{"op", "remove"}, {"path", path}
|
||||||
{"path", detail::concat<string_t>(path, '/', detail::to_string<string_t>(j - 1))}
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
i = source.size();
|
|
||||||
|
|
||||||
// add other remaining elements
|
/// @brief append an "add" operation for @a path with @a value to @a result
|
||||||
while (i < target.size())
|
static void diff_add(basic_json& result, const string_t& path, const basic_json& value)
|
||||||
{
|
{
|
||||||
result.push_back(
|
result.push_back(
|
||||||
{
|
{
|
||||||
{"op", "add"},
|
{"op", "add"}, {"path", path}, {"value", value}
|
||||||
{"path", detail::concat<string_t>(path, "/-")},
|
|
||||||
{"value", target[i]}
|
|
||||||
});
|
});
|
||||||
++i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
/// @brief append the "remove" operations for the elements of array
|
||||||
|
/// @a source from @a index on, and the "add" operations for the
|
||||||
|
/// elements of array @a target from source's size on, to @a result
|
||||||
|
static void diff_array_tails(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path, const std::size_t index)
|
||||||
|
{
|
||||||
|
// remove my remaining elements, highest index first; appending
|
||||||
|
// in that order avoids the quadratic reinsertion done before
|
||||||
|
for (std::size_t j = source.size(); j > index; --j)
|
||||||
|
{
|
||||||
|
diff_remove(result, detail::concat<string_t>(path, '/', detail::to_string<string_t>(j - 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
case value_t::object:
|
// add other remaining elements
|
||||||
|
for (std::size_t i = source.size(); i < target.size(); ++i)
|
||||||
|
{
|
||||||
|
diff_add(result, detail::concat<string_t>(path, "/-"), target[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief compare the keys of objects @a source and @a target
|
||||||
|
|
||||||
|
If the keys both objects have are in the same order in both, and the keys
|
||||||
|
only @a target has come after them, stores the keys common to both in
|
||||||
|
source's order in @a common_keys, stores the "add" operations for the keys
|
||||||
|
only @a target has in @a added_ops, and returns true: the caller then diffs
|
||||||
|
the objects member by member. Otherwise, appends operations that remove
|
||||||
|
every member of @a source and add every member of @a target to @a result,
|
||||||
|
and returns false.
|
||||||
|
*/
|
||||||
|
static bool diff_object_keys(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path, std::vector<typename object_t::key_type>& common_keys,
|
||||||
|
basic_json& added_ops)
|
||||||
{
|
{
|
||||||
// first pass: record, for every source key, whether it is
|
// first pass: record, for every source key, whether it is
|
||||||
// common to both objects (in source's iteration order) or
|
// common to both objects (in source's iteration order) or
|
||||||
@@ -31078,10 +31067,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// a by-product of the target.find() call already needed to
|
// a by-product of the target.find() call already needed to
|
||||||
// tell the two cases apart, so it adds no extra lookups. The
|
// tell the two cases apart, so it adds no extra lookups. The
|
||||||
// "remove" ops themselves are emitted later, interleaved
|
// "remove" ops themselves are emitted later, interleaved
|
||||||
// with the recursive per-key diffs in the fast path below,
|
// with the per-key diffs in the caller's fast path, to match
|
||||||
// to match source's original iteration order (as the
|
// source's original iteration order (as the original,
|
||||||
// original, pre-reordering-aware implementation did) instead
|
// pre-reordering-aware implementation did) instead of
|
||||||
// of grouping all removes before all recursive diffs.
|
// grouping all removes before all per-key diffs.
|
||||||
std::vector<typename object_t::key_type> common_keys_source_order;
|
std::vector<typename object_t::key_type> common_keys_source_order;
|
||||||
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
||||||
{
|
{
|
||||||
@@ -31097,20 +31086,19 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// source.find() call already needed to detect added keys. At
|
// source.find() call already needed to detect added keys. At
|
||||||
// the same time, determine whether every added key comes
|
// the same time, determine whether every added key comes
|
||||||
// after every common key in target's order (a precondition
|
// after every common key in target's order (a precondition
|
||||||
// for the fast path below, which only ever appends new keys
|
// for the fast path, which only ever appends new keys
|
||||||
// at the very end): for an object_t whose iteration order is
|
// at the very end): for an object_t whose iteration order is
|
||||||
// a pure function of the key set (e.g. the default std::map,
|
// a pure function of the key set (e.g. the default std::map,
|
||||||
// which always iterates in sorted key order), the order
|
// which always iterates in sorted key order), the order
|
||||||
// check further below is always true and this whole
|
// check further below is always true and this whole
|
||||||
// mechanism is effectively a no-op; it only matters for a
|
// mechanism is effectively a no-op; it only matters for a
|
||||||
// reorderable object_t such as the one backing `ordered_json`.
|
// reorderable object_t such as the one backing `ordered_json`.
|
||||||
// patch ops for keys that were added (i.e., in target but not
|
// The patch ops for keys that were added (i.e., in target but not
|
||||||
// in source); built here so the fast path below can reuse
|
// in source) are built here so the fast path can reuse
|
||||||
// them without a second source.find() per target key. Only
|
// them without a second source.find() per target key. Only
|
||||||
// used by the fast path -- the slow (reordering) path
|
// used by the fast path -- the slow (reordering) path
|
||||||
// rebuilds "add" ops for every key itself.
|
// rebuilds "add" ops for every key itself.
|
||||||
std::vector<typename object_t::key_type> common_keys_target_order;
|
std::vector<typename object_t::key_type> common_keys_target_order;
|
||||||
basic_json added_ops(value_t::array);
|
|
||||||
bool new_keys_form_suffix = true;
|
bool new_keys_form_suffix = true;
|
||||||
bool seen_new_key = false;
|
bool seen_new_key = false;
|
||||||
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
||||||
@@ -31118,12 +31106,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
if (source.find(it.key()) == source.end())
|
if (source.find(it.key()) == source.end())
|
||||||
{
|
{
|
||||||
seen_new_key = true;
|
seen_new_key = true;
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
diff_add(added_ops, detail::concat<string_t>(path, '/', detail::escape(it.key())), it.value());
|
||||||
added_ops.push_back(
|
|
||||||
{
|
|
||||||
{"op", "add"}, {"path", path_key},
|
|
||||||
{"value", it.value()}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -31139,43 +31122,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
{
|
{
|
||||||
// fast path: order of common keys already matches (or the
|
// fast path: order of common keys already matches (or the
|
||||||
// object_t's iteration order does not depend on
|
// object_t's iteration order does not depend on
|
||||||
// insertion history), so a plain per-key recursive diff
|
// insertion history), so a plain per-key diff is correct
|
||||||
// is correct and minimal, as before. common_keys_source_order
|
// and minimal, as before
|
||||||
// is, by construction, the subsequence of source's keys
|
common_keys = std::move(common_keys_source_order);
|
||||||
// that are common to both objects, in source's iteration
|
return true;
|
||||||
// order -- so it can be walked in lockstep with `source`
|
|
||||||
// using a cheap key comparison instead of another lookup.
|
|
||||||
// Deleted keys (those source keys not in common_keys_source_order)
|
|
||||||
// are interleaved here too, in source's original order, to
|
|
||||||
// match the historical (pre-reordering-aware) output order.
|
|
||||||
auto common_it = common_keys_source_order.cbegin();
|
|
||||||
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
|
||||||
{
|
|
||||||
if (common_it != common_keys_source_order.cend() && it.key() == *common_it)
|
|
||||||
{
|
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
|
||||||
auto temp_diff = diff_recursively(it.value(), target[it.key()], path_key, depth + 1);
|
|
||||||
result.insert(result.end(), temp_diff.begin(), temp_diff.end());
|
|
||||||
++common_it;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// found a key that is not in target -> remove it
|
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"}, {"path", path_key}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// append the "add" ops for brand-new keys collected above
|
|
||||||
// during the pass over target -- no second source.find()
|
|
||||||
// per target key needed
|
|
||||||
result.insert(result.end(), added_ops.begin(), added_ops.end());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// slow path: the common keys are in a different relative
|
// slow path: the common keys are in a different relative
|
||||||
// order in source and target (only possible for a
|
// order in source and target (only possible for a
|
||||||
// reorderable object_t like ordered_map). Building a
|
// reorderable object_t like ordered_map). Building a
|
||||||
@@ -31192,11 +31144,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// it moves it to the end, fixing its position.
|
// it moves it to the end, fixing its position.
|
||||||
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
||||||
{
|
{
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
diff_remove(result, detail::concat<string_t>(path, '/', detail::escape(it.key())));
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"}, {"path", path_key}
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add every key that is either common (just removed
|
// add every key that is either common (just removed
|
||||||
@@ -31205,15 +31153,96 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// target exactly
|
// target exactly
|
||||||
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
||||||
{
|
{
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
diff_add(result, detail::concat<string_t>(path, '/', detail::escape(it.key())), it.value());
|
||||||
result.push_back(
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief @ref diff, for values at nesting level @a depth, appending the
|
||||||
|
operations to @a result
|
||||||
|
|
||||||
|
Diffing two arrays or objects calls this function again, once per nesting
|
||||||
|
level, so values nested deeply enough used to exhaust the call stack and
|
||||||
|
terminate the process. The descent is bounded here: once @ref
|
||||||
|
detail::recursion_depth_limit levels have been entered, @ref
|
||||||
|
diff_iteratively diffs what is left without the call stack.
|
||||||
|
*/
|
||||||
|
static void diff_recursively(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path, const std::size_t depth)
|
||||||
{
|
{
|
||||||
{"op", "add"}, {"path", path_key},
|
// if the values are the same, there is nothing to do
|
||||||
{"value", it.value()}
|
if (source == target)
|
||||||
});
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(depth >= detail::recursion_depth_limit()))
|
||||||
|
{
|
||||||
|
diff_iteratively(result, source, target, path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (source.type() != target.type())
|
||||||
|
{
|
||||||
|
// different types: replace value
|
||||||
|
diff_replace(result, path, target);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (source.type())
|
||||||
|
{
|
||||||
|
case value_t::array:
|
||||||
|
{
|
||||||
|
// first pass: traverse common elements
|
||||||
|
std::size_t i = 0;
|
||||||
|
while (i < source.size() && i < target.size())
|
||||||
|
{
|
||||||
|
// recursive call to compare array values at index i
|
||||||
|
diff_recursively(result, source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)), depth + 1);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We now reached the end of at least one array
|
||||||
|
// in a second pass, traverse the remaining elements
|
||||||
|
diff_array_tails(result, source, target, path, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case value_t::object:
|
||||||
|
{
|
||||||
|
std::vector<typename object_t::key_type> common_keys;
|
||||||
|
basic_json added_ops(value_t::array);
|
||||||
|
if (diff_object_keys(result, source, target, path, common_keys, added_ops))
|
||||||
|
{
|
||||||
|
// fast path: common_keys is, by construction, the
|
||||||
|
// subsequence of source's keys that are common to both
|
||||||
|
// objects, in source's iteration order -- so it can be
|
||||||
|
// walked in lockstep with `source` using a cheap key
|
||||||
|
// comparison instead of another lookup. Deleted keys
|
||||||
|
// (those source keys not in common_keys) are interleaved
|
||||||
|
// here too, in source's original order, to match the
|
||||||
|
// historical (pre-reordering-aware) output order.
|
||||||
|
auto common_it = common_keys.cbegin();
|
||||||
|
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
||||||
|
{
|
||||||
|
if (common_it != common_keys.cend() && it.key() == *common_it)
|
||||||
|
{
|
||||||
|
diff_recursively(result, it.value(), target[it.key()], detail::concat<string_t>(path, '/', detail::escape(it.key())), depth + 1);
|
||||||
|
++common_it;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// found a key that is not in target -> remove it
|
||||||
|
diff_remove(result, detail::concat<string_t>(path, '/', detail::escape(it.key())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// append the "add" ops for brand-new keys collected by
|
||||||
|
// diff_object_keys -- no second source.find() per target
|
||||||
|
// key needed
|
||||||
|
result.insert(result.end(), added_ops.begin(), added_ops.end());
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31228,29 +31257,22 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
// both primitive types: replace value
|
// both primitive types: replace value
|
||||||
result.push_back(
|
diff_replace(result, path, target);
|
||||||
{
|
|
||||||
{"op", "replace"}, {"path", path}, {"value", target}
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief @ref diff without the call stack
|
@brief @ref diff without the call stack, appending the operations to
|
||||||
|
@a result
|
||||||
|
|
||||||
Produces the same patch as @ref diff_recursively. Only reached for values
|
Produces the same operations as @ref diff_recursively. Only reached for
|
||||||
nested more deeply than @ref detail::recursion_depth_limit.
|
values nested more deeply than @ref detail::recursion_depth_limit.
|
||||||
*/
|
*/
|
||||||
static basic_json diff_iteratively(const basic_json& source, const basic_json& target,
|
static void diff_iteratively(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
const string_t& path)
|
const string_t& path)
|
||||||
{
|
{
|
||||||
// the patch
|
|
||||||
basic_json result(value_t::array);
|
|
||||||
|
|
||||||
// The arrays and objects being diffed are kept on an explicit stack,
|
// The arrays and objects being diffed are kept on an explicit stack,
|
||||||
// and every pair of elements is still diffed completely before the
|
// and every pair of elements is still diffed completely before the
|
||||||
// next one, so the operations come out in the same order as in
|
// next one, so the operations come out in the same order as in
|
||||||
@@ -31278,10 +31300,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
if (s.type() != t.type())
|
if (s.type() != t.type())
|
||||||
{
|
{
|
||||||
// different types: replace value
|
// different types: replace value
|
||||||
result.push_back(
|
diff_replace(result, current_path, t);
|
||||||
{
|
|
||||||
{"op", "replace"}, {"path", current_path}, {"value", t}
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31295,124 +31314,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
case value_t::object:
|
case value_t::object:
|
||||||
{
|
{
|
||||||
// first pass: record, for every source key, whether it is
|
std::vector<typename object_t::key_type> common_keys;
|
||||||
// common to both objects (in source's iteration order) or
|
|
||||||
// was deleted (i.e., in source but not in target) -- this is
|
|
||||||
// a by-product of the t.find() call already needed to
|
|
||||||
// tell the two cases apart, so it adds no extra lookups. The
|
|
||||||
// "remove" ops themselves are emitted later, interleaved
|
|
||||||
// with the per-key diffs in the fast path below, to match
|
|
||||||
// source's original iteration order (as the original,
|
|
||||||
// pre-reordering-aware implementation did) instead of
|
|
||||||
// grouping all removes before all per-key diffs.
|
|
||||||
std::vector<typename object_t::key_type> common_keys_source_order;
|
|
||||||
for (auto it = s.cbegin(); it != s.cend(); ++it)
|
|
||||||
{
|
|
||||||
if (t.find(it.key()) != t.end())
|
|
||||||
{
|
|
||||||
common_keys_source_order.push_back(it.key());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// second pass: find keys that were added (i.e., in target but
|
|
||||||
// not in source), and record the keys common to both, in
|
|
||||||
// target's iteration order -- again a by-product of the
|
|
||||||
// s.find() call already needed to detect added keys. At
|
|
||||||
// the same time, determine whether every added key comes
|
|
||||||
// after every common key in target's order (a precondition
|
|
||||||
// for the fast path below, which only ever appends new keys
|
|
||||||
// at the very end): for an object_t whose iteration order is
|
|
||||||
// a pure function of the key set (e.g. the default std::map,
|
|
||||||
// which always iterates in sorted key order), the order
|
|
||||||
// check further below is always true and this whole
|
|
||||||
// mechanism is effectively a no-op; it only matters for a
|
|
||||||
// reorderable object_t such as the one backing `ordered_json`.
|
|
||||||
// patch ops for keys that were added (i.e., in target but not
|
|
||||||
// in source); built here so the fast path below can reuse
|
|
||||||
// them without a second s.find() per target key. Only
|
|
||||||
// used by the fast path -- the slow (reordering) path
|
|
||||||
// rebuilds "add" ops for every key itself.
|
|
||||||
std::vector<typename object_t::key_type> common_keys_target_order;
|
|
||||||
basic_json added_ops(value_t::array);
|
basic_json added_ops(value_t::array);
|
||||||
bool new_keys_form_suffix = true;
|
if (diff_object_keys(result, s, t, current_path, common_keys, added_ops))
|
||||||
bool seen_new_key = false;
|
|
||||||
for (auto it = t.cbegin(); it != t.cend(); ++it)
|
|
||||||
{
|
{
|
||||||
if (s.find(it.key()) == s.end())
|
// fast path: the frame walks source in lockstep with
|
||||||
{
|
// common_keys, as diff_recursively does, and appends
|
||||||
seen_new_key = true;
|
// added_ops once all members are done
|
||||||
const auto path_key = detail::concat<string_t>(current_path, '/', detail::escape(it.key()));
|
|
||||||
added_ops.push_back(
|
|
||||||
{
|
|
||||||
{"op", "add"}, {"path", path_key},
|
|
||||||
{"value", it.value()}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
common_keys_target_order.push_back(it.key());
|
|
||||||
if (seen_new_key)
|
|
||||||
{
|
|
||||||
new_keys_form_suffix = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (common_keys_source_order == common_keys_target_order && new_keys_form_suffix)
|
|
||||||
{
|
|
||||||
// fast path: order of common keys already matches (or the
|
|
||||||
// object_t's iteration order does not depend on
|
|
||||||
// insertion history), so a plain per-key diff is correct
|
|
||||||
// and minimal, as before. The frame walks source in
|
|
||||||
// lockstep with common_keys_source_order, which is, by
|
|
||||||
// construction, the subsequence of source's keys that
|
|
||||||
// are common to both objects, in source's iteration
|
|
||||||
// order -- so a cheap key comparison replaces another
|
|
||||||
// lookup. Deleted keys are interleaved there too, in
|
|
||||||
// source's original order, and the "add" ops collected
|
|
||||||
// above are appended once all members are done.
|
|
||||||
stack.emplace_back(&s, &t, current_path.size());
|
stack.emplace_back(&s, &t, current_path.size());
|
||||||
stack.back().member = s.cbegin();
|
stack.back().member = s.cbegin();
|
||||||
stack.back().common_keys = std::move(common_keys_source_order);
|
stack.back().common_keys = std::move(common_keys);
|
||||||
stack.back().added_ops = std::move(added_ops);
|
stack.back().added_ops = std::move(added_ops);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// slow path: the common keys are in a different relative
|
|
||||||
// order in source and target (only possible for a
|
|
||||||
// reorderable object_t like ordered_map). Building a
|
|
||||||
// minimal reordering patch is a nontrivial (LCS-like)
|
|
||||||
// problem; instead, remove every source key -- both
|
|
||||||
// deleted keys (which must be removed regardless) and
|
|
||||||
// common keys (removed so they can be re-added in
|
|
||||||
// target's order) -- and re-add every key that should
|
|
||||||
// remain, with its final target value, in target's
|
|
||||||
// order. basic_json::patch()'s "add" operation on an
|
|
||||||
// object uses operator[], which appends at the end for a
|
|
||||||
// vector-backed insertion-ordered map when the key does
|
|
||||||
// not already exist -- so removing a key and then adding
|
|
||||||
// it moves it to the end, fixing its position.
|
|
||||||
for (auto it = s.cbegin(); it != s.cend(); ++it)
|
|
||||||
{
|
|
||||||
const auto path_key = detail::concat<string_t>(current_path, '/', detail::escape(it.key()));
|
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"}, {"path", path_key}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// add every key that is either common (just removed
|
|
||||||
// above) or brand new, in target's iteration order, so
|
|
||||||
// that the final order after applying the patch matches
|
|
||||||
// target exactly
|
|
||||||
for (auto it = t.cbegin(); it != t.cend(); ++it)
|
|
||||||
{
|
|
||||||
const auto path_key = detail::concat<string_t>(current_path, '/', detail::escape(it.key()));
|
|
||||||
result.push_back(
|
|
||||||
{
|
|
||||||
{"op", "add"}, {"path", path_key},
|
|
||||||
{"value", it.value()}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -31428,10 +31340,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
// both primitive types: replace value
|
// both primitive types: replace value
|
||||||
result.push_back(
|
diff_replace(result, current_path, t);
|
||||||
{
|
|
||||||
{"op", "replace"}, {"path", current_path}, {"value", t}
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -31466,28 +31375,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
// We now reached the end of at least one array
|
// We now reached the end of at least one array
|
||||||
// in a second pass, traverse the remaining elements
|
// in a second pass, traverse the remaining elements
|
||||||
|
diff_array_tails(result, *frame.source, *frame.target, current_path, frame.index);
|
||||||
// remove my remaining elements, highest index first; appending
|
|
||||||
// in that order avoids the quadratic reinsertion done before
|
|
||||||
for (std::size_t j = source_array.size(); j > frame.index; --j)
|
|
||||||
{
|
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"},
|
|
||||||
{"path", detail::concat<string_t>(current_path, '/', detail::to_string<string_t>(j - 1))}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// add other remaining elements
|
|
||||||
for (std::size_t i = source_array.size(); i < target_array.size(); ++i)
|
|
||||||
{
|
|
||||||
result.push_back(
|
|
||||||
{
|
|
||||||
{"op", "add"},
|
|
||||||
{"path", detail::concat<string_t>(current_path, "/-")},
|
|
||||||
{"value", target_array[i]}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -31509,11 +31397,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// found a key that is not in target -> remove it
|
// found a key that is not in target -> remove it
|
||||||
const auto path_key = detail::concat<string_t>(current_path, '/', detail::escape(it.key()));
|
diff_remove(result, detail::concat<string_t>(current_path, '/', detail::escape(it.key())));
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"}, {"path", path_key}
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -31530,8 +31414,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
current_path.resize(stack.back().path_length);
|
current_path.resize(stack.back().path_length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
Reference in new issue
Block a user