Skip to content

perf(detect): per-entry string matching for ignore rules — no pathlib objects per pattern per file - #8

Merged
Azeem1985 merged 1 commit into
v8from
fix/scan-ignore-pruning
Aug 24, 2026
Merged

Azeem1985 merged 1 commit into
v8from
fix/scan-ignore-pruning

Conversation

@Azeem1985

Copy link
Copy Markdown

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:

_format_parsed_parts (pathlib.py:432) / _from_parsed_parts / __getitem__ / __iter__ / __contains__
is_relative_to (pathlib.py:700) / relative_to (pathlib.py:679)
_eval (graphify/detect.py:1339)
_is_ignored (graphify/detect.py:1374)
_is_scan_ignored (graphify/detect.py:1395)
_ignored_for_scan (graphify/detect.py:1586)
detect (graphify/detect.py:1678)
detect_incremental (graphify/detect.py:2245)

_eval called Path.relative_to(anchor) — and usually a second relative_to(root) — per pattern, per walked entry, re-parsed every pattern's flags per entry, re-split the relative path per pattern, and re-stated is_dir() per matched directory-only pattern. On Python 3.12+ relative_to walks parents (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):

  • Directory-level pruning already holds at the walk: the ignored dir cost exactly 1 _is_scan_ignored evaluation, os.walk never 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.
  • The per-pattern pathlib construction is the real burn: 65,732 relative_to calls 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 — exactly PurePath.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.
  • Per-entry state (root-relative path, segment splits, "/"-joined prefixes, NFC name, is_dir() stat) computed at most once per entry.
  • Walk loop reuses the child Path and documents the one-evaluation-per-pruned-dir contract.

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

  • 38,000-case differential fuzz of old vs new _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.
  • All 267 pre-existing detect/ignore tests pass unmodified.

Numbers

measure (synthetic trees) before after
relative_to calls, 1,000-file corpus, 61 patterns 65,732 2,047 (≈2/entry)
detect() wall, same tree 1.48 s 0.54 s
detect() wall, deep tree + 300-pattern chain 12.0 s 3.3 s
evaluations for a 2,000-file ignored dir 1 (pruned) 1 (pruned, now test-pinned)

Tests added

  • test_ignored_dir_pruned_walk_never_lists_contents — walk never descends an ignored dir (tracked via monkeypatched os.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: ≤6 relative_to calls (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 check clean.

🤖 Generated with Claude Code

…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]>
@Azeem1985
Azeem1985 merged commit 5ee116d into v8 Aug 24, 2026
@Azeem1985
Azeem1985 deleted the fix/scan-ignore-pruning branch August 24, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant