descend() does unused setup work for every subschema
Maintainers usually reply within 6 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- python
- Domain
- backend, performance
Research direction
Start with descend() and iter_errors() in jsonschema/validators.py, then inspect how __attrs_post_init__ builds _validators and how resolver updates handle subschemas. The issue provides a proposed branch and benchmark commands; compare its lazy validator setup and resolver fast path against current behavior, and run the test suite plus the reported benchmarks. Done means the optimization preserves behavior and produces measurable gains.
Written by the indexing model from the issue text.
Description
While profiling validation of fairly ordinary nested object schemas, most of the time outside the keyword functions goes into descend() setting up the child validator. Three things there do work that is usually thrown away:
- Every
descend()callsevolve(), which runs__attrs_post_init__. That buildsself._validatorsfromapplicable_validators(schema), butdescend()never reads it: it iteratesapplicable_validators(schema)again itself. Onlyiter_errors()uses the list. - When no resolver is passed in,
descend()creates aResourceand callsin_subresource()for every subschema.in_subresource()returns the same resolver when the subschema has no id, which is almost always the case. typegoes throughensure_listplusany()over a generator even when the value is a single string. (This one is minor, and I saw #1120 found the gain small and not there on PyPy, so I'm fine leaving it out.)
Proposed change (about 25 lines, branch here):
- Build
_validatorslazily on first use initer_errors()instead of in__attrs_post_init__. Validators created bydescend()then never build it. - In
descend(), checkspecification.id_of(schema)first and reuseself._resolverwhen it isNone; otherwise do exactly what happens today. SinceResource.id()is justid_of()plus stripping#, behaviour is the same. - Optionally, in
type, check a single string directly before falling back to the current code. The error message is unchanged.
Benchmarks, CPython 3.13 on an M-series Mac. The machine was loaded (load average 9-17), so treat the exact numbers as approximate; runs were interleaved between the two trees.
The repo's own pyperf benchmarks (--fast):
| benchmark | main | branch | |
|---|---|---|---|
| nested_schemas: not nested | 1.23 ms | 0.90 ms | 1.37x |
| nested_schemas: nested * 1 | 1.57 ms | 0.82 ms | 1.92x |
| nested_schemas: nested * 7 | 144 ms | 87.6 ms | 1.64x |
| nested_schemas: nested * 10 | 1.76 s | 0.93 s | 1.89x |
| useless_keywords (geometric mean) | 1.15x |
A small script validating an array of 500 typical objects (properties, nested object, array of strings, required), Draft202012Validator, median of 11 interleaved runs:
| case | main | branch | lazy list only | resolver skip only | type only |
|---|---|---|---|---|---|
is_valid |
13.04 ms | 9.18 ms (1.42x) | 1.12x | 1.15x | 0.98x |
list(iter_errors(...)) |
13.00 ms | 9.09 ms (1.43x) | 1.11x | 1.09x | 1.04x |
{"items": {"type": "string"}}, 1000 strings |
1.61 ms | 1.03 ms (1.56x) | 1.07x | 1.12x | 1.07x |
I did not have a PyPy 3.12 available to check that side.
The full test suite passes on the branch (virtue jsonschema: 8297 passed, 232 skipped, same as main), and ruff check / ruff format --check are clean.
This might be relevant to #1304 as well, though I haven't run the schemas from that issue.
Happy to open a PR with this change if it seems useful/viable.
- Dominant language
- Python
- Stars
- 5k
- Forks
- 741
- Avg merge
- 4d 17h
- Merged PRs (30d)
- 6
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python-jsonschema/jsonschema
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-jsonschema/jsonschema#1610 · 1 comment ·
Maintainers usually reply within 6 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
python-jsonschema/jsonschema#1588 · 2 comments ·
Maintainers usually reply within 6 days
-
Iterator error contexts lose parent links and absolute pathsMay be free again @DawnofGenX claimed this 59 days ago, and no pull request is open. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
python-jsonschema/jsonschema#1584 · 3 comments ·
Maintainers usually reply within 6 days
-
regex format checker crashes with RecursionError on deeply nested patternsPossibly taken @DawnofGenX claimed this 59 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
python-jsonschema/jsonschema#1538 · 1 comment ·
Maintainers usually reply within 6 days
-
is_duration raises an uncaught decimal.Overflow on a duration with a large exponentMay be free again A pull request for this issue was closed without being merged. OpenNeeds Test Upstream
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
python-jsonschema/jsonschema#1511 · 1 comment ·
Maintainers usually reply within 6 days
All issues in python-jsonschema/jsonschema
Similar issues
-
Difficulty 1/5 1-3 hours Newbie friendliness 78/100
web-push-libs/pywebpush#216 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
AgriciDaniel/claude-seo#572 ·
Maintainers usually reply within 5 days
-
area:print-dispatch
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
Maintainers usually reply within 1 day
-
area: docs domain: technical-writing good first issue type: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
Deepak3699/Ai_Mentor#919 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day