Skip to content

Commit 888d2cc

Browse files
authored
Merge pull request #12664 from notatallshaw/Calculate-candidate-versions-once-in-get_applicable_candidates
Calculate candidate string versions only once in `get_applicable_candidates`
2 parents 3c51be8 + 811ab0b commit 888d2cc

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

news/12664.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Minor performance improvement of finding applicable package candidates by not repeatedly calculating their versions

src/pip/_internal/index/package_finder.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -452,24 +452,23 @@ def get_applicable_candidates(
452452
# Using None infers from the specifier instead.
453453
allow_prereleases = self._allow_all_prereleases or None
454454
specifier = self._specifier
455-
versions = {
456-
str(v)
457-
for v in specifier.filter(
458-
# We turn the version object into a str here because otherwise
459-
# when we're debundled but setuptools isn't, Python will see
460-
# packaging.version.Version and
461-
# pkg_resources._vendor.packaging.version.Version as different
462-
# types. This way we'll use a str as a common data interchange
463-
# format. If we stop using the pkg_resources provided specifier
464-
# and start using our own, we can drop the cast to str().
465-
(str(c.version) for c in candidates),
455+
456+
# We turn the version object into a str here because otherwise
457+
# when we're debundled but setuptools isn't, Python will see
458+
# packaging.version.Version and
459+
# pkg_resources._vendor.packaging.version.Version as different
460+
# types. This way we'll use a str as a common data interchange
461+
# format. If we stop using the pkg_resources provided specifier
462+
# and start using our own, we can drop the cast to str().
463+
candidates_and_versions = [(c, str(c.version)) for c in candidates]
464+
versions = set(
465+
specifier.filter(
466+
(v for _, v in candidates_and_versions),
466467
prereleases=allow_prereleases,
467468
)
468-
}
469-
470-
# Again, converting version to str to deal with debundling.
471-
applicable_candidates = [c for c in candidates if str(c.version) in versions]
469+
)
472470

471+
applicable_candidates = [c for c, v in candidates_and_versions if v in versions]
473472
filtered_applicable_candidates = filter_unallowed_hashes(
474473
candidates=applicable_candidates,
475474
hashes=self._hashes,

0 commit comments

Comments
 (0)