descend() does unused setup work for every subschema

未关闭
#1,603 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 6 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python

调研方向

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:

  1. Every descend() calls evolve(), which runs __attrs_post_init__. That builds self._validators from applicable_validators(schema), but descend() never reads it: it iterates applicable_validators(schema) again itself. Only iter_errors() uses the list.
  2. When no resolver is passed in, descend() creates a Resource and calls in_subresource() for every subschema. in_subresource() returns the same resolver when the subschema has no id, which is almost always the case.
  3. type goes through ensure_list plus any() 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 _validators lazily on first use in iter_errors() instead of in __attrs_post_init__. Validators created by descend() then never build it.
  • In descend(), check specification.id_of(schema) first and reuse self._resolver when it is None; otherwise do exactly what happens today. Since Resource.id() is just id_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 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python-jsonschema/jsonschema 的其他 Issue

查看 python-jsonschema/jsonschema 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。