Skip to content

Commit

Permalink
Merge pull request #100 from Stranger6667/dd/cache-validator
Browse files Browse the repository at this point in the history
perf: Cache JSON Schema validators by their schema's JSON representation
  • Loading branch information
Zac-HD authored Feb 7, 2023
2 parents afbb2a9 + 8c1fa03 commit e393e2d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
release:
runs-on: ubuntu-latest
needs: [check, test, test-slow]
if: github.repository == 'Zac-HD/hypothesis-jsonschema' && github.ref == 'refs/heads/master'
if: github.repository == 'python-jsonschema/hypothesis-jsonschema' && github.ref == 'refs/heads/master'
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

#### 0.22.1 - 2023-02-07
- Cache JSON Schema validators by their schema's JSON representation

#### 0.22.0 - 2021-12-15
- never generate trailing newlines for regex patterns ending in `$`
(allowed by Python, but not by JSON Schema)
Expand Down
1 change: 0 additions & 1 deletion deps/check.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ flake8-docstrings
flake8-mutable
# flake8-noqa # See https://github.com/JBKahn/flake8-print/issues/50
flake8-print
flake8-simplify
flake8-strftime
mypy
pep8-naming
Expand Down
6 changes: 0 additions & 6 deletions deps/check.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#
# pip-compile --output-file=deps/check.txt deps/check.in
#
astor==0.8.1
# via flake8-simplify
attrs==21.2.0
# via flake8-bugbear
autoflake==1.4
Expand Down Expand Up @@ -36,7 +34,6 @@ flake8==4.0.1
# flake8-mutable
# flake8-polyfill
# flake8-print
# flake8-simplify
# flake8-strftime
# pep8-naming
flake8-2020==1.6.1
Expand All @@ -63,8 +60,6 @@ flake8-polyfill==1.0.2
# pep8-naming
flake8-print==4.0.0
# via -r deps/check.in
flake8-simplify==0.14.2
# via -r deps/check.in
flake8-strftime==0.3.2
# via -r deps/check.in
gitdb==4.0.9
Expand All @@ -77,7 +72,6 @@ importlib-metadata==4.2.0
# flake8
# flake8-2020
# flake8-comprehensions
# flake8-simplify
# stevedore
isort==5.10.1
# via shed
Expand Down
2 changes: 1 addition & 1 deletion src/hypothesis_jsonschema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
The only public API is `from_schema`; check the docstring for details.
"""

__version__ = "0.22.0"
__version__ = "0.22.1"
__all__ = ["from_schema"]

from ._from_schema import from_schema
27 changes: 27 additions & 0 deletions src/hypothesis_jsonschema/_canonicalise.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import math
import re
from fractions import Fraction
from functools import lru_cache
from typing import Any, Dict, List, Optional, Tuple, Union

import jsonschema
Expand Down Expand Up @@ -69,7 +70,33 @@ def next_down(val: float) -> float:
return out


class CacheableSchema:
"""Cache schema by its JSON representation.
Canonicalisation is not required as schemas with the same JSON representation
will have the same validator.
"""

__slots__ = ("schema", "encoded")

def __init__(self, schema: Schema) -> None:
self.schema = schema
self.encoded = hash(json.dumps(schema, sort_keys=True))

def __eq__(self, other: "CacheableSchema") -> bool: # type: ignore
return self.encoded == other.encoded

def __hash__(self) -> int:
return self.encoded


def _get_validator_class(schema: Schema) -> JSONSchemaValidator:
return __get_validator_class(CacheableSchema(schema))


@lru_cache(maxsize=128)
def __get_validator_class(wrapper: CacheableSchema) -> JSONSchemaValidator:
schema = wrapper.schema
with contextlib.suppress(jsonschema.exceptions.SchemaError):
validator = jsonschema.validators.validator_for(schema)
validator.check_schema(schema)
Expand Down

0 comments on commit e393e2d

Please sign in to comment.