Skip to content

2.3.6: every run hashes all Composer autoload files, a few seconds slower when only a small part of a big project is analysed #15746

Open

Description

@alfredbez

Bug report

Since 2.3.6 every PHPStan run is several seconds slower in a project with many files in its Composer autoload section, when paths covers only a small part of the project. The extra time is spent before "Result cache restored", so it is also there with a warm result cache and no changes.

We run PHPStan on a large monorepo in many small jobs, each with its own paths. With 2.3.6 each job got about 4 to 5 seconds slower, the same for small and large jobs. A single job that analyses the whole project is only about 1 to 3 seconds slower.

The new result cache behaviour in 2.3.6 is a big win for us (a new class now re-analyses 1 file instead of hundreds), so we would like to keep it. Only the fixed cost per run is the problem.

Code snippet that reproduces the problem

Reproducer repository: https://github.com/alfredbez/phpstan-2.3.6-startup-regression

GitHub Actions run with the numbers in the job logs and job summaries: https://github.com/alfredbez/phpstan-2.3.6-startup-regression/actions/runs/43768242498

The project has 40,000 generated classes in src/Lib (autoloaded with App\ => src/, not analysed) and 10 classes in src/Small (the only entry in paths). No extensions, no baseline, no bootstrap files.

php generate.php 40000
composer install
bin/compare.sh 10 2.3.5 2.3.6   # hyperfine, warm and cold
bin/bisect.sh 2.3.5 2.3.6       # git bisect in phpstan-src

Expected output

About the same run time as 2.3.5 when nothing changed.

Actual output

GitHub runner, PHP 8.4, 40,000 autoloaded classes, 10 analysed files, mean of 10 runs:

2.3.5 2.3.6
Warm, nothing changed 0.97 s 1.41 s
Cold, tmpDir deleted 1.77 s 2.25 s
Warm, OS file cache dropped before each run 1.12 s 3.22 s

The last row is like a CI job on a new machine: the result cache is restored, but the project files were not read yet. We think our CI jobs are close to this case. There each job pays about 4 to 5 seconds.

Did PHPStan help you today? Did it make you happy in any way?

The 2.3.6 result cache changes are really good, thank you. In our monorepo a new class used to re-analyse hundreds of files and now re-analyses one.

Where the time goes

git bisect between 2.3.5 and 2.3.6 points to phpstan/phpstan-src@603c305 ("Track the files of the project's Composer autoload entries like scanned files"). The bisect job in the workflow run shows each step.

Since this commit, ResultCacheManager::getScannedFiles() adds all files of the project's Composer autoload entries to the scanned files and calls getFileHash() (hash_file('sha256', ...)) for each file that is not analysed, on every run. Analysed files can reuse the hash from the result cache when the file stat is the same (hashAnalysedFiles()), but these files cannot.

We added timing around the parts of getScannedFiles() and ran it on our monorepo on a laptop (tens of thousands of PHP files in the autoload section, 2.3.6, warm): reading the autoload paths takes 0.01 s, finding the files 0.1 s, and the hash loop 5.0 s. That is almost all of the difference we see there.

A full analysis does not see this much, because the analysed files are left out before hashing.

Two ideas, you know better if one of them fits:

  • reuse the stat signature check for these files too (this would help local runs, but in CI a fresh checkout changes the stat anyway),
  • a faster hash for this check, if hashing and not reading the files is the main cost on a machine.

We did not try a fix ourselves.

Activity

  1. mergeable commented on Nov 15, 2026

    @mergeable

    This bug report is missing a link to reproduction at phpstan.org/try.

    It will most likely be closed after manual review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions