Skip to content

Cache the dehydrated schema validator instead of rebuilding it per call - #81

Merged
Volv-G merged 1 commit into
masterfrom
piforge/tangle-pipeline-crud/cache-dehydrated-schema-validato-1988580
Oct 28, 2026
Merged

Volv-G merged 1 commit into
masterfrom
piforge/tangle-pipeline-crud/cache-dehydrated-schema-validato-1988580

Conversation

@Volv-G

@Volv-G Volv-G commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Summary

validate_dehydrated_data rebuilt the Draft 2020-12 validator and re-ran check_schema() against the constant packaged dehydrated schema on every call. This PR builds the validator once.

Design (minimal)

  • New private no-arg factory _dehydrated_validator() decorated with @lru_cache(maxsize=1): loads the (already lru_cached) packaged schema, runs validator_for(schema).check_schema(schema) once, and returns the constructed validator.
  • validate_dehydrated_data calls the factory instead of rebuilding per call. No locks, copies, or global state beyond the lru_cache.
  • Only the validator is cached — never validation results. Every payload is still fully validated.

Semantics

  • Validation semantics and errors are unchanged: same iter_errors → sort by absolute_path → best_match path, same SchemaValidationError message and location.
  • An invalid schema would still raise SchemaError on every call (lru_cache does not cache exceptions).

Tests

  • test_check_schema_runs_once_across_many_validations: 125 interleaved valid/invalid validations → check_schema called exactly once (counted via monkeypatched Draft202012Validator.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.
  • Results: 41 passed in tests/test_schema_validation.py; full suite 2434 passed.

Benchmark (old per-call path vs cached, min of 5×300 calls)

payload before after speedup
valid 36.3 ms/call 0.67 ms/call 54×
invalid 22.7 ms/call 0.73 ms/call 31×

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-run check_schema/rebuild (tests do this explicitly).

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
Volv-G requested a review from Ark-kun as a code owner October 28, 2026 23:06
@Volv-G
Volv-G merged commit 88a7474 into master Oct 28, 2026
6 checks passed
@Volv-G
Volv-G deleted the piforge/tangle-pipeline-crud/cache-dehydrated-schema-validato-1988580 branch October 28, 2026 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant