Optimize file system enumeration pattern matching with specialized delegates#123041
Optimize file system enumeration pattern matching with specialized delegates#123041
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-io |
…legates Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs
Outdated
Show resolved
Hide resolved
…, combine IsDirectory check with matcher Co-authored-by: stephentoub <[email protected]>
src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs
Outdated
Show resolved
Hide resolved
…in delegates Co-authored-by: stephentoub <[email protected]>
src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs
Outdated
Show resolved
Hide resolved
…ds, entryType) tuple Co-authored-by: stephentoub <[email protected]>
|
@EgorBot -amd -intel -arm |
… default Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
...braries/System.Runtime/tests/System.IO.FileSystem.Tests/Enumeration/PatternTransformTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <[email protected]>
src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerableFactory.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemName.cs
Outdated
Show resolved
Hide resolved
…elds to s_camelCase Co-authored-by: stephentoub <[email protected]>
|
@EgorBot -amd -intel -arm |
|
@copilot run the code review skill |
Here's my review of the current state of the PR: Correctness — No issues found:
Performance — Two observations:
💡 The Tests — Good coverage:
No blocking issues found. The PR looks correct and well-structured. |
Description
File system enumeration was re-checking pattern types on every entry to determine if fast paths apply (
*literal→ EndsWith). This moves pattern analysis to enumerable creation time and creates specialized delegates upfront.Changes:
GetPredicate<T>that analyzes patterns once and returns optimizedFileSystemEnumerable<T>.FindPredicatedelegates:*→ always-true predicate (with IsDirectory/IsFile check)literal→Equalsfor exact filename matching (e.g., "log.txt" across directory hierarchies)*literal→EndsWith(existing optimization, moved earlier)literal*→StartsWith*literal*→Containsprefix*suffix→StartsWith+EndsWithwith minimum length checkSearchValues<char>(s_simpleWildcards,s_extendedWildcards) to internal static readonly fields inFileSystemNamefor shared useexpressionin lambdas and compute span slices inline on each invocation to avoid string allocations and minimize capture overhead(useExtendedWildcards, entryType)tuple to return distinct delegates without capturing unnecessary variables(startsWithStar, endsWithStar)tuple for cleaner code organizationCustomer Impact
Performance improvement for file enumeration with common glob patterns. Each file entry now calls a simple string operation instead of re-evaluating pattern structure and potentially running the full matching algorithm.
Regression
No. This is a performance optimization.
Testing
FileSystemNameTests.csfor literal patterns,literal*(StartsWith),*literal*(Contains), andprefix*suffixpatternsPatternTransformTests.csthat reference existingSimpleMatchDataandWin32MatchDatatheory data fromFileSystemNameTests, creating actual files and validating enumeration results for each pattern type through the full enumeration path including the optimized delegatesRisk
Low. The optimization preserves existing behavior by falling back to full pattern matching for any pattern not matching the fast paths. All existing tests pass unchanged.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.