Skip to content

Commit 6ab7443

Browse files
morlutocursoragent
authored andcommitted
perf(benchmarks): cache schema validators with lru_cache
The _validator function re-reads and re-checks the schema file on every call. With 219 task.toml files and multiple schema validations per task, this causes unnecessary I/O and computation. Add lru_cache(maxsize=16) to _validator — there are only a handful of schemas, so the cache hit rate is near 100%.
1 parent 6536175 commit 6ab7443

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

benchmarks/tooling/benchmark_contracts.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from pathlib import Path
88
from typing import Any
99

10+
from functools import lru_cache
11+
1012
from jsonschema import Draft202012Validator
1113
from jsonschema.exceptions import SchemaError, ValidationError
1214

@@ -48,6 +50,7 @@ def _read_toml(path: Path) -> dict[str, Any]:
4850
return value
4951

5052

53+
@lru_cache(maxsize=16)
5154
def _validator(schema_name: str) -> Draft202012Validator:
5255
path = SCHEMAS / schema_name
5356
schema = _read_json(path)

0 commit comments

Comments
 (0)