Fix performance regression from logical combination patch discard (#288)

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-09-22 16:03:55 +02:00 committed by GitHub
parent 2143027c7f
commit 79535fe0b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 &replace(const json::json_pointer &, json value);
json_patch &remove(const json::json_pointer &); json_patch &remove(const json::json_pointer &);
json &get_json() { return j_; }
const json &get_json() const { return j_; }
operator json() const { return j_; } operator json() const { return j_; }
private: private:
json j_; json j_ = nlohmann::json::array();
static void validateJsonPatch(json const &patch); static void validateJsonPatch(json const &patch);
}; };

View File

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