Use System.IO.Enumeration for faster recursive path traversal#112
Merged
Conversation
kthompson
marked this pull request as ready for review
June 27, 2026 19:03
Replaces the manual BFS traverser with a single FileSystemEnumerable using RecurseSubdirectories. Glob matching is pushed into the enumeration: - ShouldRecursePredicate (PathMatcher.CanRecurse) prunes subtrees that cannot match the pattern before any IO is done on them - ShouldIncludePredicate (PathMatcher.IsFullMatch) performs the full glob match; only matched entries are materialized into FileSystemInfo objects - Each path is visited exactly once -- no HashSet dedup needed PathMatcher uses span-based matching (no string[] allocation per path) mirroring GlobEvaluator's ** backtracking semantics. A bare ** pattern still emits the search root for behavioral parity. Dead code removed: PathTraverserEnumerable (manual BFS), GetFiles/ GetDirectories + _dirCache in TraverseOptions (now a plain flags carrier), MockTraverseOptions/MockFileSystemNode and their System.IO.Abstractions package dependencies. Benchmarks (net8.0, Apple M3) vs origin/develop: PathTraversalSparseMatch 1,622,130 ns -> 714,784 ns (-56%, -70% alloc) PathTraversalDirectories 689,596 ns -> 535,730 ns (-22%, -46% alloc) PathTraversal 29,635 ns -> 30,148 ns (+2%, -18% alloc) Closes #33 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
kthompson
force-pushed
the
kthompson-io-enumeration-path-traversal
branch
from
June 27, 2026 19:17
e533151 to
053f726
Compare
kthompson
enabled auto-merge (rebase)
June 27, 2026 19:18
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.
Why
The old traverser used a manual BFS loop that called
GetFiles()/GetDirectories()per directory, allocatingDirectoryInfo[]arrays andFileSystemInfoobjects for every entry regardless of whether it matched the glob pattern. For sparse patterns like**/*.csproj, this resulted in large allocations (Gen2 GC pressure) and slow traversal.This PR rewires path traversal to use a single
FileSystemEnumerable<FileSystemInfo>withRecurseSubdirectories = true, pushing glob matching directly into the enumeration predicates.Approach
Two new types do the work:
PathMatcher-- span-basedIsFullMatchandCanRecursemethods that mirrorGlobEvaluator's**backtracking semantics without allocating a string array for path segments.RecursiveGlobEnumerable-- wraps a singleFileSystemEnumerablewith:ShouldRecursePredicatecallingPathMatcher.CanRecurseto prune entire subtrees that cannot possibly match the patternShouldIncludePredicatecallingPathMatcher.IsFullMatchfor the full match checkToFileSystemInfo) only runs on matched entriesEach path is visited exactly once by the OS, so no
HashSetdedup is needed. Entries that don't match are never materialized intoFileSystemInfoobjects.The one behavioral subtlety: a bare
**pattern also matches the search root itself (zero directories deep). SinceFileSystemEnumerablenever visits the root,RecursiveGlobEnumerableemits it explicitly when the pattern is all-wildcard.Removed dead code
With the new traverser in place, the old per-directory machinery is no longer needed:
PathTraverserEnumerable.cs(manual BFS)TraverseOptionsreduced to a plain flags carrier (noGetFiles/GetDirectories, no_dirCache)MockTraverseOptions,MockFileSystemNode, and their unit testsSystem.IO.Abstractionspackage references fromGlob.TestsBenchmark results (net8.0, Apple M3)
Comparing against
origin/developwith identical benchmark definitions ([MemoryDiagnoser]added to baseline for fair comparison):**/*.cs)**/*.csproj)**/bin)Parse/match benchmarks are within noise and allocations are unchanged. Gen2 GC pressure disappears entirely for sparse traversal patterns.
Test coverage
All 179 tests pass (183 minus the 4
MockTraverseOptionsunit tests that tested the now-deleted code). Real-filesystem coverage inPathTraverserTestscontinues to exercise all traversal contracts: dedup, symlink handling, re-enumeration, case-insensitivity,emitFiles/emitDirectoriesflags, rooted patterns.Closes #33