Repository navigation
Conversation
Member
|
Thank you @linluozi! Looks good. Can you add a newsfragment file to complete this pull request? Thanks! |
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.
Symptom
Version.compare()is the hot path when sorting or resolving many versions. Every call rebuilds both numeric triples throughto_tuple()and reads six property descriptors, although_major,_minor,_patchlive in__slots__and are immutable after construction.Root cause
The numeric comparison is routed through the public tuple accessor:
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 originalto_tuple()/ property path, so overriddenmajor,minor,patchorprereleaseproperties still behave as before. 24 lines changed (17 insertions, 7 deletions) insrc/semver/version.py; no public API change, no cache, no new state, no extra memory.Verification
python -m pytest -q-> 553 passed, 1 skipped, 34 warnings. Log: https://github.com/linluozi/semver-opt-lab/blob/main/benchmark/upstream_tests.logcompare()and< <= == != > >=; 464 str/bytes operand checks; 6 subclass pairs with overridden properties. 0 mismatches.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.