From dd8f7a5aadee6999df91d8273821a3f0257c11e1 Mon Sep 17 00:00:00 2001 From: rmorotti Date: Fri, 21 Jun 2024 13:34:50 +0100 Subject: [PATCH 1/4] PERF: do not recreate the parent dir before extracting each file from a wheel --- news/12782.bugfix.rst | 2 ++ src/pip/_internal/operations/install/wheel.py | 16 +++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 news/12782.bugfix.rst diff --git a/news/12782.bugfix.rst b/news/12782.bugfix.rst new file mode 100644 index 00000000000..b84b2bdacfa --- /dev/null +++ b/news/12782.bugfix.rst @@ -0,0 +1,2 @@ +Improve performance of pip install. When extracting a wheel, +do not recheck/recreate the parent directory before extracting each file. \ No newline at end of file diff --git a/src/pip/_internal/operations/install/wheel.py b/src/pip/_internal/operations/install/wheel.py index a02a193d226..4b582c44c2a 100644 --- a/src/pip/_internal/operations/install/wheel.py +++ b/src/pip/_internal/operations/install/wheel.py @@ -358,12 +358,6 @@ def _getinfo(self) -> ZipInfo: return self._zip_file.getinfo(self.src_record_path) def save(self) -> None: - # directory creation is lazy and after file filtering - # to ensure we don't install empty dirs; empty dirs can't be - # uninstalled. - parent_dir = os.path.dirname(self.dest_path) - ensure_dir(parent_dir) - # When we open the output file below, any existing file is truncated # before we start writing the new contents. This is fine in most # cases, but can cause a segfault if pip has loaded a shared @@ -421,7 +415,7 @@ def make( return super().make(specification, options) -def _install_wheel( +def _install_wheel( # noqa: C901, PLR0915 function is too long name: str, wheel_zip: ZipFile, wheel_path: str, @@ -580,7 +574,15 @@ def is_entrypoint_wrapper(file: "File") -> bool: script_scheme_files = map(ScriptFile, script_scheme_files) files = chain(files, script_scheme_files) + existing_parents = set() for file in files: + # directory creation is lazy and after file filtering + # to ensure we don't install empty dirs; empty dirs can't be + # uninstalled. + parent_dir = os.path.dirname(file.dest_path) + if parent_dir not in existing_parents: + ensure_dir(parent_dir) + existing_parents.add(parent_dir) file.save() record_installed(file.src_record_path, file.dest_path, file.changed) From 68ec1d0b86acc1a945f70926f9f46d841455a8a2 Mon Sep 17 00:00:00 2001 From: rmorotti Date: Wed, 26 Jun 2024 17:25:18 +0100 Subject: [PATCH 2/4] add a new line at end of news --- news/12782.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/12782.bugfix.rst b/news/12782.bugfix.rst index b84b2bdacfa..121d040ce52 100644 --- a/news/12782.bugfix.rst +++ b/news/12782.bugfix.rst @@ -1,2 +1,2 @@ Improve performance of pip install. When extracting a wheel, -do not recheck/recreate the parent directory before extracting each file. \ No newline at end of file +do not recheck/recreate the parent directory before extracting each file. From 0de6341d465b0a4677554295127de87cb90fb1e4 Mon Sep 17 00:00:00 2001 From: morotti Date: Thu, 27 Jun 2024 12:43:00 +0100 Subject: [PATCH 3/4] Update news/12782.bugfix.rst Co-authored-by: Richard Si --- news/12782.bugfix.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/news/12782.bugfix.rst b/news/12782.bugfix.rst index 121d040ce52..84739b862e1 100644 --- a/news/12782.bugfix.rst +++ b/news/12782.bugfix.rst @@ -1,2 +1,2 @@ -Improve performance of pip install. When extracting a wheel, -do not recheck/recreate the parent directory before extracting each file. +Improve pip install performance by only creating required parent +directories once, instead of before extracting every file in the wheel. From 9152609d9bb9462c631b5b65bd2c98f1525beafd Mon Sep 17 00:00:00 2001 From: rmorotti Date: Thu, 27 Jun 2024 13:33:39 +0100 Subject: [PATCH 4/4] newline in news --- news/12782.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/12782.bugfix.rst b/news/12782.bugfix.rst index 84739b862e1..459a2838c32 100644 --- a/news/12782.bugfix.rst +++ b/news/12782.bugfix.rst @@ -1,2 +1,2 @@ Improve pip install performance by only creating required parent -directories once, instead of before extracting every file in the wheel. +directories once, instead of before extracting every file in the wheel.