Skip to content

Commit e18a82f

Browse files
authored
Merge pull request #13656 from notatallshaw/pre-compute-Python-Requirement-speicifier
Precompute Python Requirements Candidate Lookup
2 parents c912b5c + 4d65ba6 commit e18a82f

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

news/13656.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Precompute Python Requirements on each candidate, reducing time of long resolutions.

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ def __init__(self, specifier: SpecifierSet, match: Candidate) -> None:
164164
self._hash: int | None = None
165165
self._candidate = match
166166

167+
# Pre-compute candidate lookup to avoid repeated specifier checks
168+
if specifier.contains(match.version, prereleases=True):
169+
self._candidate_lookup: CandidateLookup = (match, None)
170+
else:
171+
self._candidate_lookup = (None, None)
172+
167173
def __str__(self) -> str:
168174
return f"Python {self.specifier}"
169175

@@ -197,9 +203,7 @@ def format_for_error(self) -> str:
197203
return str(self)
198204

199205
def get_candidate_lookup(self) -> CandidateLookup:
200-
if self.specifier.contains(self._candidate.version, prereleases=True):
201-
return self._candidate, None
202-
return None, None
206+
return self._candidate_lookup
203207

204208
def is_satisfied_by(self, candidate: Candidate) -> bool:
205209
assert candidate.name == self._candidate.name, "Not Python candidate"

0 commit comments

Comments
 (0)