Skip to content

Commit 703d6ac

Browse files
authored
Merge pull request #8912 from uranusjr/cache-found-candidates
2 parents 75befb5 + dedecf0 commit 703d6ac

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

news/8905.feature

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Cache package listings on index packages so they are guarenteed to stay stable
2+
during a pip command session. This also improves performance when a index page
3+
is accessed multiple times during the command session.

src/pip/_internal/index/collector.py

+2-24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pip._internal.models.link import Link
2222
from pip._internal.models.search_scope import SearchScope
2323
from pip._internal.network.utils import raise_for_status
24+
from pip._internal.utils.compat import lru_cache
2425
from pip._internal.utils.filetypes import ARCHIVE_EXTENSIONS
2526
from pip._internal.utils.misc import pairwise, redact_auth_from_url
2627
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
@@ -36,10 +37,8 @@
3637
List,
3738
MutableMapping,
3839
Optional,
39-
Protocol,
4040
Sequence,
4141
Tuple,
42-
TypeVar,
4342
Union,
4443
)
4544

@@ -50,31 +49,10 @@
5049
HTMLElement = xml.etree.ElementTree.Element
5150
ResponseHeaders = MutableMapping[str, str]
5251

53-
# Used in the @lru_cache polyfill.
54-
F = TypeVar('F')
55-
56-
class LruCache(Protocol):
57-
def __call__(self, maxsize=None):
58-
# type: (Optional[int]) -> Callable[[F], F]
59-
raise NotImplementedError
60-
6152

6253
logger = logging.getLogger(__name__)
6354

6455

65-
# Fallback to noop_lru_cache in Python 2
66-
# TODO: this can be removed when python 2 support is dropped!
67-
def noop_lru_cache(maxsize=None):
68-
# type: (Optional[int]) -> Callable[[F], F]
69-
def _wrapper(f):
70-
# type: (F) -> F
71-
return f
72-
return _wrapper
73-
74-
75-
_lru_cache = getattr(functools, "lru_cache", noop_lru_cache) # type: LruCache
76-
77-
7856
def _match_vcs_scheme(url):
7957
# type: (str) -> Optional[str]
8058
"""Look for VCS schemes in the URL.
@@ -344,7 +322,7 @@ def with_cached_html_pages(
344322
`page` has `page.cache_link_parsing == False`.
345323
"""
346324

347-
@_lru_cache(maxsize=None)
325+
@lru_cache(maxsize=None)
348326
def wrapper(cacheable_page):
349327
# type: (CacheablePageContent) -> List[Link]
350328
return list(fn(cacheable_page.page))

src/pip/_internal/index/package_finder.py

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from pip._internal.models.selection_prefs import SelectionPreferences
2626
from pip._internal.models.target_python import TargetPython
2727
from pip._internal.models.wheel import Wheel
28+
from pip._internal.utils.compat import lru_cache
2829
from pip._internal.utils.filetypes import WHEEL_EXTENSION
2930
from pip._internal.utils.logging import indent_log
3031
from pip._internal.utils.misc import build_netloc
@@ -799,6 +800,7 @@ def process_project_url(self, project_url, link_evaluator):
799800

800801
return package_links
801802

803+
@lru_cache(maxsize=None)
802804
def find_all_candidates(self, project_name):
803805
# type: (str) -> List[InstallationCandidate]
804806
"""Find all available InstallationCandidate for project_name

src/pip/_internal/utils/compat.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import absolute_import, division
88

99
import codecs
10+
import functools
1011
import locale
1112
import logging
1213
import os
@@ -18,7 +19,15 @@
1819
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
1920

2021
if MYPY_CHECK_RUNNING:
21-
from typing import Optional, Text, Tuple, Union
22+
from typing import Callable, Optional, Protocol, Text, Tuple, TypeVar, Union
23+
24+
# Used in the @lru_cache polyfill.
25+
F = TypeVar('F')
26+
27+
class LruCache(Protocol):
28+
def __call__(self, maxsize=None):
29+
# type: (Optional[int]) -> Callable[[F], F]
30+
raise NotImplementedError
2231

2332
try:
2433
import ipaddress
@@ -269,3 +278,16 @@ def ioctl_GWINSZ(fd):
269278
if not cr:
270279
cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
271280
return int(cr[1]), int(cr[0])
281+
282+
283+
# Fallback to noop_lru_cache in Python 2
284+
# TODO: this can be removed when python 2 support is dropped!
285+
def noop_lru_cache(maxsize=None):
286+
# type: (Optional[int]) -> Callable[[F], F]
287+
def _wrapper(f):
288+
# type: (F) -> F
289+
return f
290+
return _wrapper
291+
292+
293+
lru_cache = getattr(functools, "lru_cache", noop_lru_cache) # type: LruCache

0 commit comments

Comments
 (0)