Skip to content

Commit

Permalink
Merge pull request #12664 from notatallshaw/Calculate-candidate-versi…
Browse files Browse the repository at this point in the history
…ons-once-in-`get_applicable_candidates`

Calculate candidate string versions only once in `get_applicable_candidates`
  • Loading branch information
pradyunsg authored Jul 13, 2024
2 parents 3c51be8 + 811ab0b commit 888d2cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
1 change: 1 addition & 0 deletions news/12664.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Minor performance improvement of finding applicable package candidates by not repeatedly calculating their versions
29 changes: 14 additions & 15 deletions src/pip/_internal/index/package_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,24 +452,23 @@ def get_applicable_candidates(
# Using None infers from the specifier instead.
allow_prereleases = self._allow_all_prereleases or None
specifier = self._specifier
versions = {
str(v)
for v in specifier.filter(
# We turn the version object into a str here because otherwise
# when we're debundled but setuptools isn't, Python will see
# packaging.version.Version and
# pkg_resources._vendor.packaging.version.Version as different
# types. This way we'll use a str as a common data interchange
# format. If we stop using the pkg_resources provided specifier
# and start using our own, we can drop the cast to str().
(str(c.version) for c in candidates),

# We turn the version object into a str here because otherwise
# when we're debundled but setuptools isn't, Python will see
# packaging.version.Version and
# pkg_resources._vendor.packaging.version.Version as different
# types. This way we'll use a str as a common data interchange
# format. If we stop using the pkg_resources provided specifier
# and start using our own, we can drop the cast to str().
candidates_and_versions = [(c, str(c.version)) for c in candidates]
versions = set(
specifier.filter(
(v for _, v in candidates_and_versions),
prereleases=allow_prereleases,
)
}

# Again, converting version to str to deal with debundling.
applicable_candidates = [c for c in candidates if str(c.version) in versions]
)

applicable_candidates = [c for c, v in candidates_and_versions if v in versions]
filtered_applicable_candidates = filter_unallowed_hashes(
candidates=applicable_candidates,
hashes=self._hashes,
Expand Down

0 comments on commit 888d2cc

Please sign in to comment.