Skip to content

Commit a84a940

Browse files
authored
perf: cache requires-python checks & skip debug logging (#13128)
The requires-python check is pretty fast, but when performed for 10000 links, the checks consume a nontrivial amount of time. For example, while installing (pre-cached + --dry-run) a pared down list of homeassistant dependencies (n=117), link evaluation took 15% of the total runtime, with requires-python evaluation accounting for half (7.5%) of that. The cache can be kept pretty small as requires-python specifiers often repeat, and when they do change, it's often in chunks (or between entirely different packages). For example, setuptools has like 1500 links, but only ~12 different `requires-python` specifiers. In addition, _log_skipped_link() is a hot method and unfortunately expensive as it hashes the link on every call. Fortunately, we can return early when debug logging is not enabled. In the same homeassistant run, this saves 0.7% of the runtime.
1 parent 4ad7295 commit a84a940

File tree

3 files changed

+7
-0
lines changed

3 files changed

+7
-0
lines changed

news/13128.feature.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cache ``python-requires`` checks while filtering potential installation candidates.

src/pip/_internal/index/package_finder.py

+5
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,11 @@ def _sort_links(self, links: Iterable[Link]) -> List[Link]:
736736
return no_eggs + eggs
737737

738738
def _log_skipped_link(self, link: Link, result: LinkType, detail: str) -> None:
739+
# This is a hot method so don't waste time hashing links unless we're
740+
# actually going to log 'em.
741+
if not logger.isEnabledFor(logging.DEBUG):
742+
return
743+
739744
entry = (link, result, detail)
740745
if entry not in self._logged_links:
741746
# Put the link at the end so the reason is more visible and because

src/pip/_internal/utils/packaging.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
logger = logging.getLogger(__name__)
1212

1313

14+
@functools.lru_cache(maxsize=32)
1415
def check_requires_python(
1516
requires_python: Optional[str], version_info: Tuple[int, ...]
1617
) -> bool:

0 commit comments

Comments
 (0)