descend() does unused setup work for every subschema
維護者通常 6 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 35/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- python
- 領域
- backend, performance
研究方向
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.
由索引模型根據 Issue 內容生成。
描述
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.
- 主要語言
- Python
- 星號
- 5k
- 分支
- 741
- 平均合併
- 4 天 17 小時
- 30 天內合併 PR
- 6
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python-jsonschema/jsonschema 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
python-jsonschema/jsonschema#1610 · 1 則留言 ·
維護者通常 6 天內回覆
-
難度 2/5 1-3 小時 新手友好度 65/100
python-jsonschema/jsonschema#1588 · 2 則留言 ·
維護者通常 6 天內回覆
-
Iterator error contexts lose parent links and absolute paths可能重新可做 @DawnofGenX 於 59 天前認領,目前沒有進行中的 PR。 未關閉
難度 2/5 1-3 小時 新手友好度 76/100
python-jsonschema/jsonschema#1584 · 3 則留言 ·
維護者通常 6 天內回覆
-
regex format checker crashes with RecursionError on deeply nested patterns可能已有人在做 @DawnofGenX 於 59 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
python-jsonschema/jsonschema#1538 · 1 則留言 ·
維護者通常 6 天內回覆
-
is_duration raises an uncaught decimal.Overflow on a duration with a large exponent可能重新可做 關聯的 PR 已關閉且未合併。 未關閉Needs Test Upstream
難度 2/5 1-3 小時 新手友好度 78/100
python-jsonschema/jsonschema#1511 · 1 則留言 ·
維護者通常 6 天內回覆
查看 python-jsonschema/jsonschema 的全部 Issue
相似的 Issue
-
難度 1/5 1 小時以內 新手友好度 92/100
Techiebutler/freeframe#687 ·
維護者通常 1 天內回覆
-
bug verbose GC
難度 2/5 1-3 小時 新手友好度 74/100
IBM/javacore-analyser#677 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 79/100
維護者通常 2 天內回覆
-
area/config comp/plugins P1 platform/qqbot tool/file type/bug
難度 2/5 1-3 小時 新手友好度 75/100
NousResearch/hermes-agent#163932 · 1 則留言 ·
維護者通常 1 天內回覆
-
plugins/dsh: todayStr() computes UTC but is documented as local — journal entries written 00:00–08:00 local land in the previous day's file可能已有人在做 @Beehtrix 今天認領。 未關閉bug
難度 2/5 1-3 小時 新手友好度 82/100
zilliztech/memsearch#907 · 1 則留言 ·
維護者通常 1 天內回覆