← 输入框|github.com|刷新
Skip to content

compare: read numeric slots directly instead of rebuilding tuples - #518

Open
linluozi wants to merge 1 commit into
python-semver:masterfrom
linluozi:compare-read-slots-directly
Open

linluozi wants to merge 1 commit into
python-semver:masterfrom
linluozi:compare-read-slots-directly

Conversation

@linluozi

Copy link
Copy Markdown

Symptom

Version.compare() is the hot path when sorting or resolving many versions. Every call rebuilds both numeric triples through to_tuple() and reads six property descriptors, although _major, _minor, _patch live in __slots__ and are immutable after construction.

Root cause

The numeric comparison is routed through the public tuple accessor:

v1 = self.to_tuple()[:3]
v2 = other.to_tuple()[:3]
x = _cmp(v1, v2)

That allocates two tuples and performs six property calls per comparison.

Change

Add a fast path when both operands are exactly Version: read the three slots directly and short-circuit on the first differing part. Operands of any subclass keep the original to_tuple() / property path, so overridden major, minor, patch or prerelease properties still behave as before. 24 lines changed (17 insertions, 7 deletions) in src/semver/version.py; no public API change, no cache, no new state, no extra memory.

Verification

  • Upstream suite after the patch: python -m pytest -q -> 553 passed, 1 skipped, 34 warnings. Log: https://github.com/linluozi/semver-opt-lab/blob/main/benchmark/upstream_tests.log
  • Exact differential against the old body: 5,000 distinct operand pairs over 1,620 distinct versions (including 10**20 and 40-digit prerelease identifiers), comparing compare() and < <= == != > >=; 464 str/bytes operand checks; 6 subclass pairs with overridden properties. 0 mismatches.
  • Real input: all SemVer-valid release strings from PyPI JSON for 35 widely used packages (boto3, botocore, awscli, numpy, django, pytest, ...), 6,599 unique versions, 98,970 adjacent comparisons. Median speedup 2.32x; three independent rounds ranged 2.23x-2.33x. Synthetic sizes 1k/10k/100k: 2.18x/2.24x/2.25x (three-round ranges 1.87x-2.18x, 2.18x-2.26x, 2.19x-2.25x).
  • Peak RSS over 1,000,000 comparisons: three paired runs, worst ratio 1.0152x (baseline 16,896-17,152 KB vs optimized 16,896-17,024 KB). Artifact: https://github.com/linluozi/semver-opt-lab/blob/main/benchmark/memcheck.json

All numbers and raw samples are in https://github.com/linluozi/semver-opt-lab (benchmark/, tests/).


This patch was prepared with AI assistance (DeepSeek Harness) and reviewed against the upstream test suite before submission.

@tomschr

tomschr commented Nov 26, 2026

Copy link
Copy Markdown
Member

Thank you @linluozi! Looks good. Can you add a newsfragment file to complete this pull request? Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants