Skip to content

Commit 93ec9a2

Browse files
committed
Lazy-evaluate candidates with installed inserted
1 parent c1dc7a8 commit 93ec9a2

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

news/9284.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
New resolver: Always iterate through candidates from indexes lazily to avoid
2+
downloading candidates we do not need.

src/pip/_internal/resolution/resolvelib/found_candidates.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import itertools
2-
import operator
32

43
from pip._vendor.six.moves import collections_abc # type: ignore
54

@@ -32,18 +31,23 @@ def _insert_installed(installed, others):
3231
already-installed package. Candidates from index are returned in their
3332
normal ordering, except replaced when the version is already installed.
3433
35-
Since candidates from index are already sorted by reverse version order,
36-
`sorted()` here would keep the ordering mostly intact, only shuffling the
37-
already-installed candidate into the correct position. We put the already-
38-
installed candidate in front of those from the index, so it's put in front
39-
after sorting due to Python sorting's stableness guarentee.
34+
The implementation iterates through and yields other candidates, but insert
35+
the installed candidate exactly once before we start yielding older or
36+
equivalent candidates, or after all other candidates if they are all newer.
4037
"""
41-
candidates = sorted(
42-
itertools.chain([installed], others),
43-
key=operator.attrgetter("version"),
44-
reverse=True,
45-
)
46-
return iter(candidates)
38+
installed_yielded = False
39+
40+
for candidate in others:
41+
# If the installed candidate if better, yield it first.
42+
if not installed_yielded and installed.version >= candidate.version:
43+
yield installed
44+
installed_yielded = True
45+
46+
yield candidate
47+
48+
# If the installed candidate is older than all other candidates.
49+
if not installed_yielded:
50+
yield installed
4751

4852

4953
class FoundCandidates(collections_abc.Sequence):

0 commit comments

Comments
 (0)