-
-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't uselessly recombine registries each time we recurse into subsch…
…emas. Also add a benchmark via @robherring to ensure we compare these timings (validating with or without a registry which isn't really being used). Fixes a ~4-5x slowdown. These are now on par with each other. Closes: #1088
- Loading branch information
Showing
2 changed files
with
39 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
An unused schema registry should not cause slower validation. | ||
"Unused" here means one where no reference resolution is occurring anyhow. | ||
See https://github.com/python-jsonschema/jsonschema/issues/1088. | ||
""" | ||
from pyperf import Runner | ||
from referencing import Registry | ||
from referencing.jsonschema import DRAFT201909 | ||
|
||
from jsonschema import Draft201909Validator | ||
|
||
registry = Registry().with_resource( | ||
"urn:example:foo", | ||
DRAFT201909.create_resource({}) | ||
) | ||
|
||
schema = {"$ref": "https://json-schema.org/draft/2019-09/schema"} | ||
instance = {"maxLength": 4} | ||
|
||
no_registry = Draft201909Validator(schema) | ||
with_useless_registry = Draft201909Validator(schema, registry=registry) | ||
|
||
if __name__ == "__main__": | ||
runner = Runner() | ||
|
||
runner.bench_func( | ||
"no registry", | ||
lambda: no_registry.is_valid(instance), | ||
) | ||
runner.bench_func( | ||
"useless registry", | ||
lambda: with_useless_registry.is_valid(instance), | ||
) |
This file contains 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