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

Skip prerelease comparison in Version.compare when the result is known - #511

Merged
tomschr merged 2 commits into
python-semver:masterfrom
g4lb:perf/compare-equal-prerelease
Nov 15, 2026
Merged

tomschr merged 2 commits into
python-semver:masterfrom
g4lb:perf/compare-equal-prerelease

Conversation

@g4lb

@g4lb g4lb commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Version.compare now returns 0 when both prerelease parts are equal. It also returns the result without _nat_cmp when only one version has a prerelease. The output does not change.

Benchmark (CPython 3.15) Before After Change
compare, equal versions, no prerelease 0.88 µs 0.43 µs -51%
compare, equal prereleases 1.28 µs 0.43 µs -66%
compare, release with prerelease 1.88 µs 0.88 µs -53%
sort 400 versions, same core, mixed prereleases 3.01 ms 2.29 ms -24%

I measured no slowdown on different prereleases, long prerelease strings, long numeric identifiers, different core versions, or parse. I used 20 interleaved rounds and a Mann-Whitney test (Apple M5, CPython 3.10 and 3.15). I did not measure other architectures or PyPy.

Checks:

  • tox -e py310,py315 passes.
  • mypy src, ruff check and docformatter --check src give the same output as on master.
  • Old and new outputs matched on 1,000,000 inputs.

g4lb added 2 commits November 15, 2026 10:43
Version.compare now returns early when both prerelease parts are equal,
or when only one of the two versions has a prerelease.

@tomschr tomschr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @g4lb, that's a great change! 👍❤️

Ready to be merged. 🚀

@tomschr
tomschr merged commit b630316 into python-semver:master Nov 15, 2026
17 checks passed
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