Skip to content

Find the analysed files with less work - #7172

Open
SanderMuller wants to merge 4 commits into
phpstan:2.4.xfrom
SanderMuller:fast-directory-walk
Open

SanderMuller wants to merge 4 commits into
phpstan:2.4.xfrom
SanderMuller:fast-directory-walk

Conversation

@SanderMuller

@SanderMuller SanderMuller commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

This is a draft for discussion. It makes finding the analysed files cheaper, mainly for configurations that analyse a broad path and exclude large directories inside it. It also fixes a symlink loop that made PHPStan analyse the same file many times.

Each commit is one change, and each can become its own PR if you prefer that:

  1. FileHelper::normalizePath() returns a path that has nothing to normalize as it is. The directory walk in commit 3 asks the excluder about each directory. Without this commit, that costs 7 ms on Shopware's 5,600 directories.
  2. FileExcluder checks the literal text of a pattern before it calls fnmatch(). That is the text before the first wildcard, the text after the last one, and the longest text between two. Patterns with a bracket expression, and all patterns on Windows, still go to fnmatch() directly.
  3. DirectoryWalker does not read a directory when the scan excluder excludes every file under it. The analyse excluder contains the scan excludes, so both finders can still share one walk. Since Exclude the files under a directory that an exclude pattern matches #7175, a pattern that matches a directory also excludes the files in it, so the walk skips those directories too.
  4. DirectoryWalker does not follow a symlink to a directory it is already in. Before, the walk repeated that directory until the operating system stopped resolving the path. A project with src/loopdir/up -> .. and 2 PHP files gave 66 analysed files and 66 errors, and now gives 2 and 2. Symfony Finder stops there too since 6.4.49.

phpstan/phpstan#9158 asks for both of these changes. I rebuilt its pnpm layout, with two packages in node_modules/.store that link to each other. Without an exclude, 2.3.x finds 33 files there and this PR finds 3. With admin/node_modules/* excluded, 2.3.x walks the same 33 files to keep 1, and this PR walks only src. phpstan/phpstan#13244 describes the same kind of tree, excluded through analyseAndScan. I did not reproduce that one.

Warm runs

Tempest at 98d5fc3, with its own phpstan.neon.dist, which analyses 2,565 files. The time is from the start of the restarted PHPStan process to its shutdown, measured with an --autoload-file hook. That keeps the restart out of the number, and it is the same in all arms. Median of 20 interleaved warm runs, with the first and third quartile:

Config 2.3.x This PR
Tempest's own config (src, packages, tests) 454 ms (442–459) 453 ms (445–457)
Same, but paths: [.] and vendor in excludePaths 500 ms (497–517) 448 ms (438–458)

The reports are identical in both arms. With narrow paths there is nothing to prune, so there is no change. With a broad path, the warm run is 52 ms faster.

The file-finding step alone

Shopware at 061e131a. This is what FileFinder does for both finders: walk, filter and sort. It uses Shopware's own excludePaths, plus an analyse-only exclude, so that the analyse and the scan lists differ. "Warm" means the directory listings from the last run are in tmpDir. Median of 10 interleaved runs:

src + tests, 9,205 files: cold warm whole root, vendor, vendor-bin, var excluded, 30,000 dirs: cold warm
Symfony Finder 5.4 232 ms – 1,126 ms –
Symfony Finder 8.1 246 ms – 1,312 ms –
Symfony Finder 8.1, filter(..., prune: true) 278 ms – 310 ms –
DirectoryWalker on 2.3.x 221 ms 58 ms 1,049 ms 190 ms
This PR 196 ms 42 ms 232 ms 45 ms

Commit 2 gives most of the gain on Shopware's own config. Commit 3 gives almost all of the gain on the whole root. A newer Symfony Finder alone does not help: it is slower than 5.4 for a plain walk, and it has no listings cache.

In the Tally benchmarks, FileExcluderBench does 57.7% fewer instructions and FileFinderBench 48.3% fewer, so their expected numbers need accepting from the tally-results artifact. I measured this as an A/B against a0e20ae in an arm64 VM. The other file benchmarks, DependencyResolverBench included, do not change.

Correctness

  • The analyse and the scan file lists are byte-identical to 2.3.x in all cases above. I measured this, and the numbers above, before I rebased onto Exclude the files under a directory that an exclude pattern matches #7175. Shopware's tests/devops/.../Rules/data/* pattern prunes 18 directories there, so the pattern branch is covered.
  • A fuzzer compared FileExcluder with 2.3.x at a0e20ae on 800,000 random paths and patterns, bracket expressions included, with no difference. It also checked 62,378 pruned directories against 10 random files under each one, and every one of those files was excluded. The excluder from before the rebase gives 9,664 differences, so the fuzzer can find one.
  • A second fuzzer compared normalizePath() with the 2.3.x version on 600,000 random paths, with both separators, with no difference.
  • The two new DirectoryWalkerTest tests fail without their commits.

Setup and what I did not measure

macOS on an Apple M4 Pro, PHP 8.5.13, Turbo extension not loaded, OS file cache warm. I did not measure Linux or Windows. From the code, nothing changes on Windows. The walk falls back to Finder there, the fast path in commit 1 does not apply, and the excluder skips the literal check. I did not run the e2e suite locally.

Closes phpstan/phpstan#9554

🤖 Generated with Claude Code

@SanderMuller
SanderMuller marked this pull request as ready for review November 25, 2026 21:41
@phpstan-bot

Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

@SanderMuller
SanderMuller force-pushed the fast-directory-walk branch 3 times, most recently from 6bb7b06 to b69476a Compare November 27, 2026 16:06
SanderMuller and others added 4 commits November 28, 2026 09:42
A path that starts with a slash and has nothing to collapse or convert
is already what normalizePath() would return. The check is cheaper than
normalizing the path. It still caches the path, so a later call
costs one lookup, as before.

The directory walk asks the excluder about each directory once. Without
this change that first call costs 7 ms on a tree of 5,600 directories.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A path that matches a pattern starts with the text before its first
wildcard, ends with the text after its last one, and contains the text
between two. These checks are cheaper than fnmatch() and rule out most
paths. Patterns with a bracket expression, and all patterns on Windows,
where fnmatch() ignores case, still go to fnmatch() directly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The directory walk read every directory under the analysed paths, and
FileFinder then dropped the files in excluded directories. Now the walk
skips a directory when the scan excluder excludes every file under it.
The analyse excluder excludes these files too, so both finders can
still share the walk.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Such a symlink made the walk repeat the directory until the operating
system stopped resolving the path, and each file in it was analysed
once per repeat. Finder does not follow such a symlink since 6.4.49, and
now the walk does the same. It calls realpath() only for symlinks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@staabm
staabm force-pushed the fast-directory-walk branch from b69476a to 417662f Compare November 28, 2026 08:42
@ondrejmirtes
ondrejmirtes changed the base branch from 2.3.x to 2.4.x December 3, 2026 18:07
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.

Files discovery must prune early

2 participants