Repository navigation
perf(requirements): reuse validated specifier tokens - #1495
Open
DicksonWu654 wants to merge 1 commit into
Open
DicksonWu654 wants to merge 1 commit into
DicksonWu654 wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requirement.__init__buildsSpecifierSet(parsed.specifier), which splits the string and runs each part throughSpecifier._regexagain, although the requirement parser has just matched every part with the same specifier pattern. This adds a privateSpecifierSet._from_validated()that splits the parser's string and sets the same fields thatSpecifierSet.__init__andSpecifier.__init__set: same spelling, order and duplicates, new objects for every requirement, empty caches. The public constructors don't change.Strings that contain
===still use the normal constructor. The arbitrary-equality token may contain commas, so splitting it can produce parts that must still be rejected with the sameInvalidRequirement(for exampledemo===x,y).Trade-off:
_from_validated()sets the slots of both classes directly, so it has to be updated when__init__gains a field. The new tests comparestr,repr,hash, pickling,filter(),contains()andprereleaseshandling withSpecifierSet(text)for a range of specifier strings, and check that two requirements never share specifier objects.Performance
CPU time per pass over the workload, this branch vs
main(713d0d7). Paired AB/BA: two worker processes, one per tree, pinned to the same core and timed alternately; each sample is the median of 5 passes; 15 pairs; speedup is the median per-pair ratio. The host was shared, so CPU time is reported. The Airflow file is the 3.1.0 Python 3.12 constraints file; the 1,169 real requirements add theRequires-Distof the latest apache-airflow, jupyterlab, pandas, poetry, requests, scipy, setuptools and torch.benchmarks/dist_sample.txt(500)benchmarks/dist_sample.txt(500)benchmarks/dist_sample.txt(500)It is independent of the marker parsing PR (#1494); with that one applied first, the same measurement gives Airflow 1.38× / 1.19× and real requirements 1.12× / 1.11× on CPython 3.12 / 3.15 (PyPy 1.22× / 1.33×, 12 pairs).
Testing
prek run --all-files(ruff, ruff format, mypy, typos, ...) passes.main(together with the other two open performance PRs) compared 82,478 cases on CPython 3.12 and PyPy 3.11, covering real requirements, generated and malformed input, exception types and messages, warnings, hashes, ordering, pickling, string subclasses and mutation of the resulting objects: no differences.🤖 Generated with Claude Code