From 06f75cfb8627e853f938a2d9be70e7b3d91ed9e6 Mon Sep 17 00:00:00 2001 From: DontBlameMe Date: Fri, 20 Nov 2026 02:51:51 +0100 Subject: [PATCH 1/2] perf(validator): reuse schema results for repeated frames --- schema/validate.py | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/schema/validate.py b/schema/validate.py index 00c3664..dc53058 100644 --- a/schema/validate.py +++ b/schema/validate.py @@ -569,6 +569,10 @@ def registry() -> Registry: # builds a fresh one per case — rebuilds rather than reuses. _VALIDATORS: dict[str, tuple[Registry, Draft202012Validator]] = {} +# An instance repeated across the corpus is checked again for its own `where`, but its errors +# against a given registry and ref never change. The key is the instance's own JSON spelling. +_VALIDATION_ERRORS: dict[tuple[str, str], tuple[Registry, tuple[str, ...]]] = {} + def validator_for(reg: Registry, ref: str) -> Draft202012Validator: """The validator for one `$ref` in this registry, built once and reused. @@ -588,19 +592,32 @@ def validator_for(reg: Registry, ref: str) -> Draft202012Validator: def check(reg: Registry, ref: str, instance: object, where: str, label: str) -> None: global CHECKS CHECKS += 1 - # The ref is resolved through the registry, never by extracting a subschema: a - # relative `#/$defs/...` inside it must keep the base URI of the file it lives in. - validator = validator_for(reg, ref) - errors = sorted( - validator.iter_errors(instance), - key=lambda error: ( - "/".join(str(part) for part in error.absolute_path), - error.message, - ), - ) + try: + key = json.dumps(instance, sort_keys=True, separators=(",", ":"), ensure_ascii=False) + except (TypeError, ValueError): + key = None + cache_key = (ref, key) if key is not None else None + cached = _VALIDATION_ERRORS.get(cache_key) if cache_key is not None else None + if cached is not None and cached[0] is reg: + errors = cached[1] + else: + # The ref is resolved through the registry, never by extracting a subschema: a + # relative `#/$defs/...` inside it must keep the base URI of the file it lives in. + validator = validator_for(reg, ref) + errors = tuple( + f"{'/'.join(str(part) for part in error.absolute_path) or ''}: {error.message}" + for error in sorted( + validator.iter_errors(instance), + key=lambda error: ( + "/".join(str(part) for part in error.absolute_path), + error.message, + ), + ) + ) + if cache_key is not None: + _VALIDATION_ERRORS[cache_key] = (reg, errors) for error in errors: - at = "/".join(str(part) for part in error.absolute_path) or "" - fail(where, f"{label}: {at}: {error.message}") + fail(where, f"{label}: {error}") def plain_integer(digits: str) -> int: From 83dea8ab91c4024e71776a74c4868c4856c9db08 Mon Sep 17 00:00:00 2001 From: DontBlameMe Date: Fri, 20 Nov 2026 03:07:43 +0100 Subject: [PATCH 2/2] fix(validator): keep object order in the cache key --- schema/validate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/validate.py b/schema/validate.py index dc53058..cf0c4aa 100644 --- a/schema/validate.py +++ b/schema/validate.py @@ -593,7 +593,7 @@ def check(reg: Registry, ref: str, instance: object, where: str, label: str) -> global CHECKS CHECKS += 1 try: - key = json.dumps(instance, sort_keys=True, separators=(",", ":"), ensure_ascii=False) + key = json.dumps(instance, separators=(",", ":"), ensure_ascii=False) except (TypeError, ValueError): key = None cache_key = (ref, key) if key is not None else None