From 85d59c4f3dfdd9c61e47a0e7e2ced83fb64f1baa Mon Sep 17 00:00:00 2001 From: r-vdp Date: Mon, 4 May 2026 12:17:39 +0200 Subject: [PATCH] nixos/systemd-boot-builder: use a set for GC root lookup has_gc_root() iterated the entire BootFileList for every file on the ESP, giving O(files * roots) comparisons. Build the set of kept paths once and use O(1) membership tests instead. --- .../boot/loader/systemd-boot/systemd-boot-builder.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py b/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py index 6f46d38286c3..1d2feb0d3dc9 100644 --- a/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py +++ b/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py @@ -593,16 +593,10 @@ def remove_extra_files() -> None: def garbage_collect(gc_roots: BootFileList) -> None: - # Check if a file is in the list of gc roots. - def has_gc_root(p: Path) -> bool: - for gc_root in gc_roots: - gc_root_path = BOOT_MOUNT_POINT / gc_root.path - if gc_root_path == p: - return True - return False + keep = {BOOT_MOUNT_POINT / gc_root.path for gc_root in gc_roots} def delete_path(e: os.DirEntry) -> None: - if e.is_file(follow_symlinks=True) and not has_gc_root(Path(e.path)): + if e.is_file(follow_symlinks=True) and Path(e.path) not in keep: os.remove(e.path) for e in os.scandir(BOOT_MOUNT_POINT / NIXOS_DIR):