Repository navigation
perf(detect): per-entry string matching for ignore rules — no pathlib objects per pattern per file - #8
Merged
Conversation
…gnore evaluation _is_ignored's _eval called Path.relative_to(anchor) — and often relative_to(root) — for EVERY pattern on EVERY walked entry, re-parsed each pattern's flags per entry, re-split the relative path per pattern, and re-stat'ed is_dir() per matched directory-only pattern. On Python 3.12+ relative_to walks parents quadratically in depth, so a real 76,579-file vault with a 28,968-file ignored dir (graphify-notes/ in .graphifyignore) had detect() pinned at 99% CPU for 52+ minutes inside pathlib._format_parsed_parts (py-spy, live). Now: the relative path is computed once per entry per distinct anchor via a lexical parts-prefix helper (_lexical_relative — exactly relative_to's comparison, Windows casefold included), pattern flags are parsed once and cached, segment/prefix splits and the is_dir stat happen at most once per entry. Directory-level pruning (one evaluation excludes a whole ignored subtree from os.walk) is pinned by regression tests counting walk descents and evaluation totals. Semantics proven unchanged: 38,000-case differential fuzz old-vs-new on Python 3.12.14 and 3.14.4, zero mismatches; full detect/watch/extract suites green. Synthetic 300-pattern deep tree: 12.0s -> 3.3s; per-scan relative_to calls 65,732 -> 2,047 on a 1,000-file corpus, 61 patterns. Co-Authored-By: Claude Fable 5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The defect
Caught live with py-spy on a real 76,579-file vault where 28,968 files sit inside a directory listed in
.graphifyignore(graphify-notes/, regenerated on every map change). The scan process sat pinned at 99% CPU for 52+ minutes:_evalcalledPath.relative_to(anchor)— and usually a secondrelative_to(root)— per pattern, per walked entry, re-parsed every pattern's flags per entry, re-split the relative path per pattern, and re-statedis_dir()per matched directory-only pattern. On Python 3.12+relative_towalksparents(quadratic in path depth, allocating a Path per step), so pathlib machinery dominated the profile exactly as the stack shows.What was verified before fixing
Instrumented
detect()on a synthetic tree (2,000 files under an ignored dir, 1,000 kept files, 61 patterns), at v8 HEAD (detect.py byte-identical to installed 0.9.48):_is_scan_ignoredevaluation,os.walknever descended (0 dirs visited beneath it) — so the walk itself does not pay per-file for a pruned subtree; it is now pinned by regression tests so it can never regress.relative_tocalls for ~1,050 evaluated entries (~63 per entry — one per pattern), 1.7 s on a toy tree; scaling entries × patterns × depth² reproduces the 50+ minute vault scan.The fix
_lexical_relative(): the relative path is computed once per entry per distinct anchor by a lexical parts-prefix comparison — exactlyPurePath.relative_to's rule, including Windows casefolding — no Path construction per pattern._parse_ignore_pattern(): pattern flags (!, trailing/, embedded/, strip) parsed once per distinct pattern string, cached.is_dir()stat) computed at most once per entry.Cost per scanned entry: from O(patterns) pathlib constructions (each O(depth²) on 3.12+) + O(patterns) stats/splits, down to O(distinct anchors) string slices + O(patterns) cached-regex fnmatch tests. For an ignored directory: one evaluation for the whole subtree (pinned by tests).
Proof of unchanged semantics
_is_ignored(negations, dir-only, anchored,**, NFC/NFD, ghosts, out-of-root, anchors above root, cached + uncached) on Python 3.12.14 and 3.14.4: zero mismatches.Numbers
relative_tocalls, 1,000-file corpus, 61 patternsdetect()wall, same treedetect()wall, deep tree + 300-pattern chainTests added
test_ignored_dir_pruned_walk_never_lists_contents— walk never descends an ignored dir (tracked via monkeypatchedos.walk); siblings still detected; dir reported once.test_scan_ignore_cost_is_per_directory_not_per_file— 2,000 files under an ignored dir: exactly 1 evaluation for the subtree, total evaluations O(dirs + kept files), counted via monkeypatched_is_scan_ignored/_is_ignored.test_is_ignored_no_per_pattern_path_construction— 201 patterns, one deep file: ≤6relative_tocalls (old code: 800+; fails on the old code, verified).test_string_matcher_preserves_pattern_forms— dir-only vs same-named file, glob + basename negation last-match-wins, anchored root-only,**, intermediate-segment match.Offline runs, all green:
tests/test_detect.py+tests/test_ignore_file_encoding.py+tests/test_extract_cli.py+tests/test_watch.py→ 432 passed;py_compile+ruff checkclean.🤖 Generated with Claude Code