From 4aaa4be87e2746698d6fa574f5e898f0a146118c Mon Sep 17 00:00:00 2001 From: Maxwell G Date: Mon, 1 Sep 2025 18:13:04 +0200 Subject: [PATCH] Speed %pyproject_save_files up significantly by using sets instead of lists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For packages with a lot of files, it took a really long time to run. Profiling the code revealed most of the time is spent in PurePath.__eq__. The code was using lists but checked if to-be-added paths were not already in them. Considering the order is insignificant (the generated %files list is sorted at the end anyway), the lists were essentially working as (very slow) sets. Using sets instead of lists makes %pyproject_save_files over 20 times faster (51.66 -> 2.39 seconds) for the ansible package (~62k files). Checking if an item is in a list is O(N), checking every added item is O(N**2). Checking if an item is in a set is O(1), checking every added item is O(N). Additionally, with set, it is unnecessary to check for presence before addition, so the code is easier. (Commit message and removal of the check by Miro.) Co-Authored-By: Miro HronĨok --- pyproject-rpm-macros.spec | 1 + pyproject_save_files.py | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pyproject-rpm-macros.spec b/pyproject-rpm-macros.spec index 4920149..f8364b5 100644 --- a/pyproject-rpm-macros.spec +++ b/pyproject-rpm-macros.spec @@ -172,6 +172,7 @@ export HOSTNAME="rpmbuild" # to speedup tox in network-less mock, see rhbz#1856 - Fixes: rhbz#2391290 - On RPM 4.20+ don't put pyproject-macros-specific files in %%buildsubdir - Works around https://github.com/rpm-software-management/rpm/issues/3890 +- Speed %%pyproject_save_files up significantly * Fri Jul 25 2025 Fedora Release Engineering - 1.18.3-2 - Rebuilt for https://fedoraproject.org/wiki/Fedora_43_Mass_Rebuild diff --git a/pyproject_save_files.py b/pyproject_save_files.py index 0a831e8..7ae4193 100644 --- a/pyproject_save_files.py +++ b/pyproject_save_files.py @@ -137,12 +137,11 @@ def add_file_to_module(paths, module_name, module_type, files_dirs, *files): """ for module in paths["modules"][module_name]: if module["type"] == module_type: - if files[0] not in module[files_dirs]: - module[files_dirs].extend(files) + module[files_dirs].update(files) break else: paths["modules"][module_name].append( - {"type": module_type, "files": [], "dirs": [], files_dirs: list(files)} + {"type": module_type, "files": set(), "dirs": set(), files_dirs: set(files)} )