Skip to content

[FileSystemGlobbing] Use the compiled Touki matcher on .NET 8+ - #132192

Draft
JeremyKuhne wants to merge 1 commit into
dotnet:mainfrom
JeremyKuhne:filesystemglobbing-touki
Draft

JeremyKuhne wants to merge 1 commit into
dotnet:mainfrom
JeremyKuhne:filesystemglobbing-touki

Conversation

@JeremyKuhne

@JeremyKuhne JeremyKuhne commented Aug 12, 2026 •

Copy link
Copy Markdown
Member

Summary

This change uses a focused, internal subset of Touki's compiled glob matcher for Microsoft.Extensions.FileSystemGlobbing.Matcher on .NET 8 and later. It also adds a FileSystemEnumerator fast path for the exact built-in DirectoryInfoWrapper type.

The existing implementation remains in place for netstandard2.0, .NET Framework, custom and derived directory wrappers, unsupported comparison modes, and patterns the focused compiler cannot represent.

No public API or package dependency is added. The reference API source is unchanged.

Relationship To The System.IO Globbing Proposal

This is an optimization of the existing FileSystemGlobbing API, not an implementation of #132060. That proposal remains open and is not API-approved.

Current Touki models the proposal's separation between an immutable reusable matcher definition and independently owned root-bound sessions. This adaptation uses the same lifetime split internally:

Proposed concept This adaptation
Reusable matcher definition Cached ToukiMatcherPlan
Per-enumeration mutable session New ToukiMatcherContext per Execute
Reusable compiled pattern Lazily compiled ToukiPattern
Compiled glob implementation Internal GlobCompiler

The proposed IFileSystemMatcher composition API returns only a Boolean file result. FileSystemGlobbing must additionally identify the winning include to produce FilePatternMatch.Stem. The local exclusion-wins and ordered matchers therefore return that winner without evaluating the pattern twice.

Proposal-only types such as GlobSpecification, IFileSystemMatcher, DirectoryMatchType, and FileSystemPathEnumerator are deliberately not imported or exposed here. Adding them would couple this package to an unapproved public design and add unused code.

The focused port originated from Touki commit 74f6739ce8e70b760814e792c79a83fd5065c9d7 and was revalidated against current Touki 256c9b73c9d9e1c138b4b1a54a639342b7cc851c, including the FileSystemGlobbing parity work in JeremyKuhne/touki#259.

Constraints Of The Existing API

The existing surface necessarily retains work that a purpose-built API can avoid:

  • PatternBuilder remains the validation and normalization authority so exception timing, accepted syntax, parent traversal, and legacy edge cases do not change.
  • DirectoryInfoBase is a public extensibility point. Only the exact built-in wrapper can use the physical FileSystemEnumerator path; derived and custom implementations must continue through virtual enumeration.
  • Matcher is a mutable include/exclude builder. Pattern additions invalidate the cached plan, and each execution creates fresh traversal state.
  • Results must preserve depth-first ordering and materialize PatternMatchingResult, relative paths, FilePatternMatch values, and stems.
  • Ordered rules must preserve the latest include responsible for a matched file, not merely a Boolean result.
  • Culture-based comparisons and compiler-limit cases fall back to the legacy implementation rather than changing behavior.
  • The modern implementation is package-asset-specific. Downlevel targets keep the existing code.

These constraints are why the improvement here is smaller than Touki's direct compiled-matcher results: the inner predicate becomes inexpensive, but the public API still owns traversal compatibility and result construction.

Adapted Performance Win

The unchanged public API benchmark shows lower allocation and flat-to-faster execution across the representative scenarios:

Scenario Baseline Change Allocation change
Literal hit 479.96 ns / 1,736 B 420.93 ns / 1,408 B -18.9%
Literal miss 459.39 ns / 1,640 B 400.09 ns / 1,312 B -20.0%
*.cs hit 465.98 ns / 1,624 B 417.42 ns / 1,408 B -13.3%
**/*.cs, deep input 1,519.32 ns / 6,664 B 1,449.34 ns / 6,424 B -3.6%
Synthetic tree 41.084 us / 117,304 B 39.363 us / 113,280 B -3.4%
Tree with excludes 37.998 us / 99,792 B 37.354 us / 96,784 B -3.0%
Ordered rules 43.363 us / 121,296 B 41.904 us / 110,952 B -8.5%

For a 1,174-file physical Touki checkout, the specialized wrapper and retained overhead reductions changed the adapter's construct-and-execute path from 3.325 ms / 980.48 KB to 2.687 ms / 210.12 KB. A precompiled matcher measured 2.633 ms / 202.29 KB.

These local Windows BenchmarkDotNet runs are short and sequential, so timing changes should be treated directionally. The allocation reductions are stable.

Headroom In A Purpose-Built API

The proposed API can expose compiled span matching directly and stream canonical paths from a matcher-aware enumerator. It does not need to build a virtual file system, calculate FSG stems, retain legacy pattern contexts, or materialize a PatternMatchingResult.

Current Touki measurements cited in #132060 show the difference in achievable scope:

  • Reusable GlobSpecification.IsMatch cases run in roughly 8.5-75 ns with zero managed allocation, 13-89x faster than the closest public Matcher.Match scenarios. The operations return different result shapes, so this is not the expected speedup for this PR.
  • Touki's native SDK-style enumeration benchmark measured 19.71 ms / 359.69 KB versus 24.98 ms / 5,589.55 KB for the existing FileSystemGlobbing path: 1.27x throughput and 93.6% less managed allocation for that workload.
  • A proposed matcher session can return three-state directory proofs for generic pruning and can stream results without FSG-specific stem and result contracts.

This PR captures the compatible win available behind the current API. The API proposal represents the additional headroom available when callers can opt into an immutable compiled matcher and a purpose-built streaming enumeration contract.

Compatibility And Packaging

The source and reference projects gain an explicit net8.0 target. Without it, .NET 8 and .NET 9 consumers would select the netstandard2.0 package asset and remain on the legacy engine.

Consumer Selected implementation
.NET Framework / netstandard2.0 Existing matcher
.NET 8 Touki-backed net8.0 asset
.NET 9 Touki-backed net8.0 asset
.NET 10+ Touki-backed asset for that TFM

All imported types are internal. The package contains no Microsoft.IO.Redist dependency. The pinned source and MIT license are recorded in source headers and THIRD-PARTY-NOTICES.TXT.

Compatibility coverage includes:

  • 46 original Touki FileSystemGlobbing oracle rows.
  • The 28-row current Touki parity matrix.
  • A deterministic generated comparison against the legacy matcher.
  • Literal unsupported syntax, root scoping, repeated execution, and plan invalidation.
  • Ordered exclude/re-include winner and stem behavior.
  • Derived-wrapper fallback and guarded direct-directory pruning.
  • Legacy fallback for unsupported comparisons and compiler limits.

Validation

  • Required baseline: build.cmd clr+libs -rc release.
  • All implementation/reference TFMs and APICompat: 0 warnings, 0 errors.
  • net11.0: 626 passed, 0 failed.
  • net481: 626 passed, 0 failed.
  • Current Touki net10 suite: 7,130 passed, 0 failed, 119 platform skips.
  • Fresh .NET 8 package consumer selected the net8.0 asset and validated matching, stems, and internal-only Touki metadata.
  • Fresh .NET 9 package consumer selected the net8.0 asset.
  • Shipping assets: net8.0, net10.0, net11.0, netstandard2.0, and net462; no new dependency groups.
  • git diff --check, imported-source whitespace checks, and final independent Extensions review passed.

The current runtime test infrastructure cannot restore this test project for net8 because its current xUnit assertion package targets net10. The complete modern suite therefore runs on net11, while isolated consumers validate net8 and net9 package selection.

Follow-Up Validation

  • Run the full CI matrix across supported operating systems.
  • Collect aligned Windows, Linux, and macOS measurements through the runtime performance workflow.
  • Request packaging-owner review of the explicit net8 asset and legal review of the third-party notice.

Note

This pull request description was drafted with GitHub Copilot assistance.

Copilot AI lite review requested due to automatic review settings August 12, 2026 00:53
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-filesystem
See info in area-owners.md if you want to be subscribed.

@JeremyKuhne

Copy link
Copy Markdown
Member Author

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces a new, internal Touki-derived compiled glob matching engine for Microsoft.Extensions.FileSystemGlobbing.Matcher on .NET 8+ (via a new net8.0 asset), while retaining the legacy matcher for downlevel TFMs and unsupported cases. The change also adds a FileSystemEnumerator-based fast path when executing against the exact built-in DirectoryInfoWrapper type.

Changes:

  • Add a net8.0 target and conditional compilation to enable a Touki-backed matching/execution path on .NET 8+.
  • Implement internal Touki-based compiled pattern/matcher planning and a FileSystemEnumerator-based traversal fast path.
  • Expand test coverage to validate parity and key semantics (ordered rules, stems, fallback cases, wrapper behavior).

Reviewed changes

Copilot reviewed 32 out of 32 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
THIRD-PARTY-NOTICES.TXT Adds Touki license notice for the imported internal subset.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/PatternMatchingTests.cs Adds parity/oracle tests and additional behavioral coverage for fallback and edge cases.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/OrderedPatternMatchingTests.cs Adds a regression test for ordered include/exclude “winner” stem behavior.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/FunctionalTests.cs Adds functional tests for wrapper fast path selection and direct exclusion pruning behavior.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Microsoft.Extensions.FileSystemGlobbing.csproj Adds explicit net8.0 targeting and conditional compilation/items for Touki-backed implementation.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/ref/Microsoft.Extensions.FileSystemGlobbing.csproj Adds explicit net8.0 targeting for the ref project to ensure correct asset selection.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Matcher.cs Adds cached internal Touki plan execution path on .NET 8+ with fallback to legacy behavior.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/ToukiPattern.cs Introduces lazy compilation + stem calculation wrapper around legacy patterns for Touki strategies.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/ToukiMatcherContext.cs Adds the Touki execution context, plan creation, and FileSystemEnumerator traversal fast paths.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/ExclusionWinsPatternMatcher.cs Adds Touki-style exclusion-wins include/exclude evaluation returning the winning include pattern.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/OrderedPatternMatcher.cs Adds Touki-style ordered rule evaluation returning the latest winning include pattern.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/AnyGlobStrategy.cs Adds compiled strategy for matching any input.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/CompiledGlobStrategy.cs Adds a general-purpose compiled matcher with NFA-style backtracking and tail anchoring.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/CompiledGlobStrategy.BacktrackState.cs Adds internal backtracking state container for the compiled matcher.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/ContainsGlobStrategy.cs Adds compiled strategy for *needle* segment matching.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/GlobCompiler.cs Adds the internal entry point for compiling patterns into strategies.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/GlobCompiler.Factory.cs Adds the pattern compiler/factory and bytecode encoding logic.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/GlobCompiler.Factory.LiteralCursor.cs Adds helper state used by the compiler to rewrite trailing literal segments during encoding.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/GlobOpCodes.cs Adds opcode definitions for the compiler’s bytecode-in-a-string encoding.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/GlobStarFileNameStrategy.cs Adds specialized strategy for **/<segment> shapes to bypass the general bytecode matcher.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/GlobStrategy.cs Adds the strategy abstraction used by compiled patterns.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/LiteralDirectoryPathStrategy.cs Adds a strategy for matching a fixed directory prefix with a segment matcher.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/LiteralGlobStrategy.cs Adds a strategy for exact literal matching (including split-span comparisons).
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/PrefixGlobStrategy.cs Adds a strategy for prefix* matching.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/PrefixSuffixGlobStrategy.cs Adds a strategy for prefix*suffix matching.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/RecursiveDirectoryPathStrategy.cs Adds a strategy for matching a directory name recursively within a path prefix.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/RootFileNameStrategy.cs Adds root-scoping strategy wrapper for path-unaware patterns.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/Io/Globbing/SuffixGlobStrategy.cs Adds a strategy for *suffix matching.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/Touki/GlobalUsings.cs Adds Touki-internal global using directives for the imported subset.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/PatternContexts/PatternContextLinear.cs Adds a conditional TrackStem toggle to avoid stem tracking overhead on Touki paths.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/PatternContexts/PatternContextRagged.cs Adds a conditional TrackStem toggle to avoid stem tracking overhead on Touki paths.
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Abstractions/DirectoryInfoWrapper.cs Adds an internal accessor to the wrapped DirectoryInfo for the fast path.

Comment on lines +12 to +16
<PropertyGroup Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp' and $([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<UseToukiGlobbing>true</UseToukiGlobbing>
<DefineConstants>$(DefineConstants);USE_TOUKI_GLOBBING</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
Comment on lines +87 to +105
private GlobStrategy? GetStrategy()
{
if (Volatile.Read(ref _compileAttempted))
{
return _strategy;
}

lock (this)
{
if (!_compileAttempted)
{
GlobCompiler.TryCompile(
GetCompiledPattern(_legacyPattern),
_ignoreCase,
out _strategy);
Volatile.Write(ref _compileAttempted, true);
}
}

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

@github-actions github-actions Bot locked and limited conversation to collaborators Nov 14, 2026
@JeremyKuhne JeremyKuhne reopened this Nov 16, 2026

This branch has not been deployed

No deployments
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants