Skip to content

Rewrite matchFiles without regexp #1483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 49 commits into
base: main
Choose a base branch
from

Conversation

jakebailey
Copy link
Member

@jakebailey jakebailey commented Jul 30, 2025

This code is a hotspot in the critical loading path. In Strada, this was all regexes, which is great in JS where regexes are fast. In Go, not so much, even moreso when we use regexp2 in ECMAScript compatibility mode. We've always wanted to rewrite this, it's just taken a bit.

This PR moves the old implementation to a different file, then I had agent mode write tests and a new implementation, verifying against those tests.

The result so far:

                                          │     old.txt     │               new.txt               │
                                          │     sec/op      │   sec/op     vs base                │
MatchFiles/CommonPattern-20                    193.54µ ± 1%   12.79µ ± 1%  -93.39% (p=0.000 n=10)
MatchFiles/SimpleInclude-20                    79.965µ ± 3%   6.511µ ± 1%  -91.86% (p=0.000 n=10)
MatchFiles/EmptyIncludes-20                    30.021µ ± 1%   4.517µ ± 1%  -84.95% (p=0.000 n=10)
MatchFiles/HiddenDirectories-20                57.053µ ± 1%   9.696µ ± 1%  -83.01% (p=0.000 n=10)
MatchFiles/NodeModulesSearch-20                226.98µ ± 1%   15.08µ ± 1%  -93.36% (p=0.000 n=10)
MatchFiles/LargeFileSystem-20               24458.353µ ± 2%   7.506µ ± 1%  -99.97% (p=0.000 n=10)
MatchFilesLarge/AllFiles-20                 21570.594µ ± 1%   5.188µ ± 1%  -99.98% (p=0.000 n=10)
MatchFilesLarge/Components-20                  2379.1µ ± 1%   261.2µ ± 2%  -89.02% (p=0.000 n=10)
MatchFilesLarge/TestFiles-20                   2835.7µ ± 2%   163.1µ ± 1%  -94.25% (p=0.000 n=10)
MatchFilesLarge/NestedUtilsWithPattern-20      1244.2µ ± 1%   166.4µ ± 1%  -86.63% (p=0.000 n=10)
geomean                                         695.9µ        20.80µ       -97.01%
                                          │    old.txt     │               new.txt                │
                                          │      B/op      │     B/op      vs base                │
MatchFiles/CommonPattern-20                  15.831Ki ± 0%   4.930Ki ± 0%  -68.86% (p=0.000 n=10)
MatchFiles/SimpleInclude-20                   6.798Ki ± 0%   3.047Ki ± 0%  -55.18% (p=0.000 n=10)
MatchFiles/EmptyIncludes-20                   3.804Ki ± 0%   2.359Ki ± 0%  -37.97% (p=0.000 n=10)
MatchFiles/HiddenDirectories-20              10.749Ki ± 0%   4.945Ki ± 0%  -53.99% (p=0.000 n=10)
MatchFiles/NodeModulesSearch-20              20.520Ki ± 0%   7.094Ki ± 0%  -65.43% (p=0.000 n=10)
MatchFiles/LargeFileSystem-20               822.329Ki ± 0%   2.930Ki ± 0%  -99.64% (p=0.000 n=10)
MatchFilesLarge/AllFiles-20                 750.434Ki ± 0%   1.969Ki ± 0%  -99.74% (p=0.000 n=10)
MatchFilesLarge/Components-20                 243.6Ki ± 0%   142.3Ki ± 0%  -41.58% (p=0.000 n=10)
MatchFilesLarge/TestFiles-20                 117.61Ki ± 0%   87.07Ki ± 0%  -25.97% (p=0.000 n=10)
MatchFilesLarge/NestedUtilsWithPattern-20    127.52Ki ± 0%   95.66Ki ± 0%  -24.98% (p=0.000 n=10)
geomean                                       53.81Ki        9.839Ki       -81.72%
                                          │   old.txt    │               new.txt               │
                                          │  allocs/op   │  allocs/op   vs base                │
MatchFiles/CommonPattern-20                   335.0 ± 0%    173.0 ± 0%  -48.36% (p=0.000 n=10)
MatchFiles/SimpleInclude-20                  143.00 ± 0%    94.00 ± 0%  -34.27% (p=0.000 n=10)
MatchFiles/EmptyIncludes-20                   86.00 ± 0%    74.00 ± 0%  -13.95% (p=0.000 n=10)
MatchFiles/HiddenDirectories-20               237.0 ± 0%    178.0 ± 0%  -24.89% (p=0.000 n=10)
MatchFiles/NodeModulesSearch-20               333.0 ± 0%    212.0 ± 0%  -36.34% (p=0.000 n=10)
MatchFiles/LargeFileSystem-20                8829.0 ± 0%    114.0 ± 0%  -98.71% (p=0.000 n=10)
MatchFilesLarge/AllFiles-20                 8190.00 ± 0%    79.00 ± 0%  -99.04% (p=0.000 n=10)
MatchFilesLarge/Components-20                3.108k ± 0%   4.016k ± 0%  +29.21% (p=0.000 n=10)
MatchFilesLarge/TestFiles-20                 1.604k ± 0%   2.021k ± 0%  +26.00% (p=0.000 n=10)
MatchFilesLarge/NestedUtilsWithPattern-20    1.661k ± 0%   2.072k ± 0%  +24.74% (p=0.000 n=10)
geomean                                       849.0         304.6       -64.12%

So, like 35x faster, maybe? 15x in many.

…nation

- Add pathCache to avoid repeated GetNormalizedPathComponents calls
- Replace tspath.CombinePaths with direct string concatenation in visitDirectory
- Reduce memory allocations by 60-70% for large file systems
- Improve performance by 40-50% for large workloads
- Add backwards compatibility wrapper for existing code
@jakebailey jakebailey changed the title Don't use a regex in IsImplicitGlob Rewrite matchFiles without regexp Jul 30, 2025
- Allow *.min.js patterns to match .min.js files when explicitly specified
- Preserve existing behavior where generic * patterns exclude .min.js files
- Add regression tests for both cases
…files

- Add comprehensive differential tests for MatchesExclude, MatchesInclude, and MatchesIncludeWithJsonOnly
- Fix matchesExcludeNew to handle extensionless files matching directory patterns
- Ensure new implementation matches old behavior without referencing old code
@jakebailey jakebailey marked this pull request as ready for review July 31, 2025 22:31
@Copilot Copilot AI review requested due to automatic review settings July 31, 2025 22:31
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR rewrites the TypeScript file matching logic (matchFiles) to eliminate regex usage for performance optimization. The regex-based pattern matching was a performance bottleneck in the critical loading path, particularly since Go's regex (especially regexp2 in ECMAScript compatibility mode) is significantly slower than JavaScript regex.

Key changes:

  • Implements a new glob pattern matching algorithm without regex using string manipulation and recursive segment matching
  • Maintains backward compatibility by keeping the old implementation in old.go for comparison
  • Adds comprehensive test coverage to verify behavioral equivalence between old and new implementations

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
internal/vfs/vfsmatch/vfsmatch.go New main API file exposing the updated file matching functions
internal/vfs/vfsmatch/old.go Refactored old regex-based implementation moved from internal/vfs
internal/vfs/vfsmatch/new.go New regex-free implementation with custom glob pattern matching
internal/vfs/vfsmatch/vfsmatch_test.go Comprehensive test suite comparing old vs new implementations
internal/vfs/vfsmatch/bench_test.go Performance benchmarks demonstrating the speed improvements
internal/tspath/path.go Minor optimization to path component parsing
internal/tsoptions/wildcarddirectories.go Updated to use new vfsmatch API and removed regex dependencies
internal/tsoptions/tsconfigparsing.go Updated to use new vfsmatch API
internal/project/discovertypings.go Updated import to use new vfsmatch API
Baseline files Test output changes reflecting improved file matching behavior

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.

2 participants