From 9fe43c903e5beadee4871034b8567b9b45feb438 Mon Sep 17 00:00:00 2001 From: Ryan McClelland Date: Fri, 14 Aug 2026 09:03:16 -0700 Subject: [PATCH] perf(scan): drop quadratic file exclusion check exclude_single_files collected every excluded name into a list, then filtered the input against it with `f not in excluded_files`. That membership test rescans the list once per file, so a platform folder holding tens of thousands of loose files spends most of the call in list scans rather than pattern matching. Decide per file in a single pass instead. Behavior is unchanged, including for duplicate file names. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01TLTDjv3niosCXZWtjKELK2 --- backend/handler/filesystem/base_handler.py | 23 +++++++++++----------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/backend/handler/filesystem/base_handler.py b/backend/handler/filesystem/base_handler.py index f746b7b81..f113bbc5d 100644 --- a/backend/handler/filesystem/base_handler.py +++ b/backend/handler/filesystem/base_handler.py @@ -271,24 +271,23 @@ class FSHandler: cnfg = cm.get_config() excluded_extensions = cnfg.EXCLUDED_SINGLE_EXT excluded_names = cnfg.EXCLUDED_SINGLE_FILES - excluded_files: list[str] = [] - - for file_name in files: - file_name_lower = file_name.lower() + def is_excluded(file_name: str) -> bool: # Check whether the filename ends with any excluded extension entry. - if any(file_name_lower.endswith("." + ext) for ext in excluded_extensions): - excluded_files.append(file_name) - continue + if any( + file_name.lower().endswith("." + ext) for ext in excluded_extensions + ): + return True # Check if the file name matches a pattern in the excluded list. - if file_name in excluded_names or any( + return file_name in excluded_names or any( fnmatch.fnmatch(file_name, name) for name in excluded_names - ): - excluded_files.append(file_name) + ) - # Return files that are not in the filtered list. - return [f for f in files if f not in excluded_files] + # Deciding per file keeps this linear. Collecting the exclusions first and + # then filtering against that list rescans it once per file, which gets + # expensive on platforms holding tens of thousands of files. + return [f for f in files if not is_excluded(f)] async def make_directory(self, path: str) -> None: """