-
Notifications
You must be signed in to change notification settings - Fork 283
perf: Add fast path for Version to Version comparison by skipping _key property
#1083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
notatallshaw
wants to merge
3
commits into
pypa:main
Choose a base branch
from
notatallshaw:speed-up-sorting-by-fast-pathing-comparison
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+264
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -548,6 +548,172 @@ def _key(self) -> CmpKey: | |||||||
| ) | ||||||||
| return self._key_cache | ||||||||
|
|
||||||||
| def __hash__(self) -> int: | ||||||||
| return hash(self._key) | ||||||||
|
|
||||||||
| # Override comparison methods to use direct _key_cache access | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| def __lt__(self, other: _BaseVersion) -> bool: | ||||||||
| if isinstance(other, Version): | ||||||||
| if self._key_cache is None: | ||||||||
| self._key_cache = _cmpkey( | ||||||||
| self._epoch, | ||||||||
| self._release, | ||||||||
| self._pre, | ||||||||
| self._post, | ||||||||
| self._dev, | ||||||||
| self._local, | ||||||||
| ) | ||||||||
| if other._key_cache is None: | ||||||||
| other._key_cache = _cmpkey( | ||||||||
| other._epoch, | ||||||||
| other._release, | ||||||||
| other._pre, | ||||||||
| other._post, | ||||||||
| other._dev, | ||||||||
| other._local, | ||||||||
| ) | ||||||||
| return self._key_cache < other._key_cache | ||||||||
|
|
||||||||
| if not isinstance(other, _BaseVersion): | ||||||||
| return NotImplemented | ||||||||
|
|
||||||||
| return super().__lt__(other) | ||||||||
|
|
||||||||
| def __le__(self, other: _BaseVersion) -> bool: | ||||||||
| if isinstance(other, Version): | ||||||||
| if self._key_cache is None: | ||||||||
| self._key_cache = _cmpkey( | ||||||||
| self._epoch, | ||||||||
| self._release, | ||||||||
| self._pre, | ||||||||
| self._post, | ||||||||
| self._dev, | ||||||||
| self._local, | ||||||||
| ) | ||||||||
| if other._key_cache is None: | ||||||||
| other._key_cache = _cmpkey( | ||||||||
| other._epoch, | ||||||||
| other._release, | ||||||||
| other._pre, | ||||||||
| other._post, | ||||||||
| other._dev, | ||||||||
| other._local, | ||||||||
| ) | ||||||||
| return self._key_cache <= other._key_cache | ||||||||
|
|
||||||||
| if not isinstance(other, _BaseVersion): | ||||||||
| return NotImplemented | ||||||||
|
|
||||||||
| return super().__le__(other) | ||||||||
|
|
||||||||
| def __eq__(self, other: object) -> bool: | ||||||||
| if isinstance(other, Version): | ||||||||
| if self._key_cache is None: | ||||||||
| self._key_cache = _cmpkey( | ||||||||
| self._epoch, | ||||||||
| self._release, | ||||||||
| self._pre, | ||||||||
| self._post, | ||||||||
| self._dev, | ||||||||
| self._local, | ||||||||
| ) | ||||||||
| if other._key_cache is None: | ||||||||
| other._key_cache = _cmpkey( | ||||||||
| other._epoch, | ||||||||
| other._release, | ||||||||
| other._pre, | ||||||||
| other._post, | ||||||||
| other._dev, | ||||||||
| other._local, | ||||||||
| ) | ||||||||
| return self._key_cache == other._key_cache | ||||||||
|
|
||||||||
| if not isinstance(other, _BaseVersion): | ||||||||
| return NotImplemented | ||||||||
|
|
||||||||
| return super().__eq__(other) | ||||||||
|
|
||||||||
| def __ge__(self, other: _BaseVersion) -> bool: | ||||||||
| if isinstance(other, Version): | ||||||||
| if self._key_cache is None: | ||||||||
| self._key_cache = _cmpkey( | ||||||||
| self._epoch, | ||||||||
| self._release, | ||||||||
| self._pre, | ||||||||
| self._post, | ||||||||
| self._dev, | ||||||||
| self._local, | ||||||||
| ) | ||||||||
| if other._key_cache is None: | ||||||||
| other._key_cache = _cmpkey( | ||||||||
| other._epoch, | ||||||||
| other._release, | ||||||||
| other._pre, | ||||||||
| other._post, | ||||||||
| other._dev, | ||||||||
| other._local, | ||||||||
| ) | ||||||||
| return self._key_cache >= other._key_cache | ||||||||
|
|
||||||||
| if not isinstance(other, _BaseVersion): | ||||||||
| return NotImplemented | ||||||||
|
|
||||||||
| return super().__ge__(other) | ||||||||
|
|
||||||||
| def __gt__(self, other: _BaseVersion) -> bool: | ||||||||
| if isinstance(other, Version): | ||||||||
| if self._key_cache is None: | ||||||||
| self._key_cache = _cmpkey( | ||||||||
| self._epoch, | ||||||||
| self._release, | ||||||||
| self._pre, | ||||||||
| self._post, | ||||||||
| self._dev, | ||||||||
| self._local, | ||||||||
| ) | ||||||||
| if other._key_cache is None: | ||||||||
| other._key_cache = _cmpkey( | ||||||||
| other._epoch, | ||||||||
| other._release, | ||||||||
| other._pre, | ||||||||
| other._post, | ||||||||
| other._dev, | ||||||||
| other._local, | ||||||||
| ) | ||||||||
| return self._key_cache > other._key_cache | ||||||||
|
|
||||||||
| if not isinstance(other, _BaseVersion): | ||||||||
| return NotImplemented | ||||||||
|
|
||||||||
| return super().__gt__(other) | ||||||||
|
|
||||||||
| def __ne__(self, other: object) -> bool: | ||||||||
| if isinstance(other, Version): | ||||||||
| if self._key_cache is None: | ||||||||
| self._key_cache = _cmpkey( | ||||||||
| self._epoch, | ||||||||
| self._release, | ||||||||
| self._pre, | ||||||||
| self._post, | ||||||||
| self._dev, | ||||||||
| self._local, | ||||||||
| ) | ||||||||
| if other._key_cache is None: | ||||||||
| other._key_cache = _cmpkey( | ||||||||
| other._epoch, | ||||||||
| other._release, | ||||||||
| other._pre, | ||||||||
| other._post, | ||||||||
| other._dev, | ||||||||
| other._local, | ||||||||
| ) | ||||||||
| return self._key_cache != other._key_cache | ||||||||
|
|
||||||||
| if not isinstance(other, _BaseVersion): | ||||||||
| return NotImplemented | ||||||||
|
|
||||||||
| return super().__ne__(other) | ||||||||
|
|
||||||||
| @property | ||||||||
| @_deprecated("Version._version is private and will be removed soon") | ||||||||
| def _version(self) -> _Version: | ||||||||
|
|
||||||||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this here (and also in the base class written exactly the same way)?