Fix performance regression from logical combination patch discard

A recent fix to discard patch from invalid logical combination
introduced a lot (for large json instance and/or schema) of copies to
make a patch backup. On some case, this introduced up to a 100x slower
validation time.

Resizing the patch object to its previous size avoid unnecessary
temporary allocations from the backup object.
This commit is contained in:
Sylvain Joubert 2023-07-20 17:24:04 +02:00 committed by Patrick Boettcher
parent 848bf758c7
commit 0a2cfd0caa
2 changed files with 6 additions and 3 deletions

View File

@ -28,10 +28,13 @@ public:
json_patch &replace(const json::json_pointer &, json value);
json_patch &remove(const json::json_pointer &);
json &get_json() { return j_; }
const json &get_json() const { return j_; }
operator json() const { return j_; }
private:
json j_;
json j_ = nlohmann::json::array();
static void validateJsonPatch(json const &patch);
};

View File

@ -436,12 +436,12 @@ class logical_combination : public schema
for (auto &s : subschemata_) {
first_error_handler esub;
json_patch old_patch(patch);
auto oldPatchSize = patch.get_json().size();
s->validate(ptr, instance, patch, esub);
if (!esub)
count++;
else
patch = old_patch;
patch.get_json().get_ref<nlohmann::json::array_t &>().resize(oldPatchSize);
if (is_validate_complete(instance, ptr, e, esub, count))
return;