Speed up the type keyword (commit ready)

Open
#1,605 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 6 days

Nobody has claimed this yet.

Assessment

This issue has not been assessed yet.

Description

type in _keywords.py calls any() on a generator expression. A loop that returns at the first type that matches is faster. The checks, their order, and the error message do not change.

Benchmark (Draft202012Validator) Before After Change
{"type": "integer"} with 5 532 ns 411 ns -22.7%
An object with 7 properties that have a type 24.5 µs 22.5 µs -8.2%
An array of 1000 numbers 1.96 ms 1.81 ms -7.8%

I measured no slowdown on 29 inputs (Apple M5, CPython 3.14.6). The test suite, ruff, and mypy pass. Old and new results matched on the JSON-Schema-Test-Suite and on 1M random inputs.

New contributors cannot open pull requests here now. The commit is https://github.com/g4lb/jsonschema/commit/32b8ee8029af3764dc2ed294a527f1495a67712e. I can open a pull request if you allow it.

Dominant language
Python
Stars
5k
Forks
741
Avg merge
4d 17h
Merged PRs (30d)
6

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-jsonschema/jsonschema

All issues in python-jsonschema/jsonschema

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.