diff --git a/news/12322.bugfix.rst b/news/12322.bugfix.rst new file mode 100644 index 00000000000..928df878978 --- /dev/null +++ b/news/12322.bugfix.rst @@ -0,0 +1 @@ +Improve performance ~17% when installing many wheels offline diff --git a/src/pip/_internal/utils/urls.py b/src/pip/_internal/utils/urls.py index 6ba2e04f350..0cbe8ea96e9 100644 --- a/src/pip/_internal/utils/urls.py +++ b/src/pip/_internal/utils/urls.py @@ -2,6 +2,7 @@ import string import urllib.parse import urllib.request +from functools import lru_cache from typing import Optional from .compat import WINDOWS @@ -13,13 +14,22 @@ def get_url_scheme(url: str) -> Optional[str]: return url.split(":", 1)[0].lower() +@lru_cache(maxsize=None) +def _normalized_abs_path_to_url(abs_path: str) -> str: + """ + Convert a normalized absolute path to a file: URL. + """ + url = urllib.parse.urljoin("file:", urllib.request.pathname2url(abs_path)) + return url + + def path_to_url(path: str) -> str: """ Convert a path to a file: URL. The path will be made absolute and have quoted path parts. """ path = os.path.normpath(os.path.abspath(path)) - url = urllib.parse.urljoin("file:", urllib.request.pathname2url(path)) + url = _normalized_abs_path_to_url(path) return url