From 0a2cfd0caa21b7c12a5d4e22241faf787f1d4a5e Mon Sep 17 00:00:00 2001 From: Sylvain Joubert Date: Thu, 20 Jul 2023 17:24:04 +0200 Subject: [PATCH] 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. --- src/json-patch.hpp | 5 ++++- src/json-validator.cpp | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/json-patch.hpp b/src/json-patch.hpp index 80fe1df..39a579b 100644 --- a/src/json-patch.hpp +++ b/src/json-patch.hpp @@ -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); }; diff --git a/src/json-validator.cpp b/src/json-validator.cpp index 77d701f..6ede7c8 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -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().resize(oldPatchSize); if (is_validate_complete(instance, ptr, e, esub, count)) return;