Repository navigation
Cache the dehydrated schema validator instead of rebuilding it per call - #81
Merged
Volv-G merged 1 commit intoOct 28, 2026
Conversation
validate_dehydrated_data rebuilt the Draft 2020-12 validator and re-ran check_schema against the constant packaged schema on every call. Build it once via a no-arg @lru_cache(maxsize=1) factory; every payload is still fully validated with unchanged error messages/locations.
Volv-G
deleted the
piforge/tangle-pipeline-crud/cache-dehydrated-schema-validato-1988580
branch
October 28, 2026 23:22
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
validate_dehydrated_datarebuilt the Draft 2020-12 validator and re-rancheck_schema()against the constant packaged dehydrated schema on every call. This PR builds the validator once.Design (minimal)
_dehydrated_validator()decorated with@lru_cache(maxsize=1): loads the (alreadylru_cached) packaged schema, runsvalidator_for(schema).check_schema(schema)once, and returns the constructed validator.validate_dehydrated_datacalls the factory instead of rebuilding per call. No locks, copies, or global state beyond thelru_cache.Semantics
iter_errors→ sort byabsolute_path→best_matchpath, sameSchemaValidationErrormessage andlocation.SchemaErroron every call (lru_cachedoes not cache exceptions).Tests
test_check_schema_runs_once_across_many_validations: 125 interleaved valid/invalid validations →check_schemacalled exactly once (counted via monkeypatchedDraft202012Validator.check_schema, cache cleared before/after each test).test_cached_validator_keeps_error_messages: on a warm cache, each invalid payload's message + location matches the uncached reference path exactly, with valid payloads interleaved.tests/test_schema_validation.py; full suite 2434 passed.Benchmark (old per-call path vs cached, min of 5×300 calls)
Residual note
The cached validator shares the packaged schema dict returned by
load_dehydrated_schema(). Nothing mutates it today; if code ever mutated that dict in place or introduced a custom/swapped schema, it would need to call_dehydrated_validator.cache_clear()to re-runcheck_schema/rebuild (tests do this explicitly).