From 598e11f62a98ba862f6162571eb0acf29736535e Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Fri, 3 May 2024 18:53:33 -0400 Subject: [PATCH 1/2] Calculate candidate string versions only once in `get_applicable_candidates` --- src/pip/_internal/index/package_finder.py | 29 +++++++++++------------ 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/pip/_internal/index/package_finder.py b/src/pip/_internal/index/package_finder.py index fb270f22f83..0d65ce35f37 100644 --- a/src/pip/_internal/index/package_finder.py +++ b/src/pip/_internal/index/package_finder.py @@ -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, From 811ab0b608e704c9858cc8dcf41e35ffe3afb933 Mon Sep 17 00:00:00 2001 From: Damian Shaw Date: Fri, 3 May 2024 18:57:09 -0400 Subject: [PATCH 2/2] NEWS ENTRY --- news/12664.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/12664.feature.rst diff --git a/news/12664.feature.rst b/news/12664.feature.rst new file mode 100644 index 00000000000..a5f85097c62 --- /dev/null +++ b/news/12664.feature.rst @@ -0,0 +1 @@ +Minor performance improvement of finding applicable package candidates by not repeatedly calculating their versions