Skip to content

Performance degradation and crashing with irrelevant complex file structure #13244

Open

Description

@mind-bending-forks

Bug report

I have some code that downloads a large quantity of data and then analyses it. The data consists of compressed csv and json files stored into a deeply nested directory structure (~8k files, ~8k symlinks, ~20k directories). Within that structure, related data items have symbolic links between them, both ways.

The data is excluded from source control through use of .gitignore. The data is also excluded from phpstan completely via a .phpstan/config.neon file that has:

parameters:
  excludePaths:
    analyseAndScan:
      - ../data-dir (?)
      - ../analysis-dir (?)

The trailing (?) is to indicate that the directories might not exist at first.

When there is a large quantity of data in those directories, phpstan runs significantly slower than expected and than it would do otherwise:

  • With data present, we have to wait anywhere between 1 min 40 and 3 min for analysis to complete, every single time (~170 MB consumed).
  • Without data present, analysis takes 8 secs (~ 170 MB consumed), faster with caching.

Also, phpstan has occasionally got itself into a state where it keeps crashing while analysing. From the phpstan stack trace (which I don't have a record of now, sorry!), it appeared that it was crashing in phpstan RecursiveDirectoryIterator code, apparently deeply nested following symlinks in the data directory, jumping back and forth between related data items until it hits some recursion limit - even thought it has no good reason to be probing through that directory at all. Clearing phpstan's cache via phpstan clear-result-cache seemed to help with that.

With data present, I also observe when running with -vvv that it is taking ~30 secs just to restore the result cache. Not sure why that is. The resultCache.php file is 266K in size and does not appear to reference the data directories except in relation to them being excluded via the project config.

When running with --debug -vvv I see that 26 files are being analysed, one of which takes ~3 secs to process, one of which takes ~2 secs to process, while all others take a fraction of a second. The time is being taken up by something else.

I think that phpstan may be unnecessarily iterating over all file system objects in the excluded directory for some reason, perhaps sometimes also following symlinks in that directory (and possibly failing to notice that it has returned back to where it started, so permitting infinite looping), and that this is slowing phpstan down significantly. It is slow with or without utilising the cache, but since restoring the cache is reported as a slow process, there may be a problem there too.

There is an opportunity here to improve performance for the benefit of everyone by ensuring that the structure and contents of excluded directories does not impact on the execution of phpstan.

Code snippet that reproduces the problem

No response

Expected output

Same, but with the time taken to analyse the code being independent of and not being slowed down by the content of excluded directories.

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

PHPStan has helped us update modernise (autloading, strict typing throughout, PHP 7 to 8 migration) a PHP library and two internal applications recently.

Activity

  1. mergeable commented on Jul 15, 2025

    @mergeable

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

    It will most likely be closed after manual review.

  2. mind-bending-forks commented on Jul 15, 2025

    @mind-bending-forks
    Author

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

    It will most likely be closed after manual review.

    The specific code is not relevant in this case.

  3. ondrejmirtes commented on Jul 15, 2025

    @ondrejmirtes
    Member

    Hello,
    please create a reproducing repository that shows this behaviour. You've described the problem in depth but it's not actionable. I need to see what's going on myself.

  4. mind-bending-forks commented on Jul 15, 2025

    @mind-bending-forks
    Author

    Hello, please create a reproducing repository that shows this behaviour. You've described the problem in depth but it's not actionable. I need to see what's going on myself.

    https://github.com/mind-bending-forks/phpstan-13244

  5. mind-bending-forks commented on Jul 16, 2025

    @mind-bending-forks
    Author

    On my system, this gives the following times for analysing a single php file with contents <?php declare(strict_types=1); only:

    excluded data files result cache result cache time elapsed time memory
    absent absent 1 sec 2 secs 128.98 MB
    absent present <1 sec <1 secs1 66.98 MB
    present absent 3 secs 8 secs 128.98 MB
    present present <1 sec 3 secs 66.98 MB

    So, comparing the cases where there was no result cache at the start, we see that:

    • time to build/write a result cache was 3 times greater, and
    • elapsed analysis time is 4 times greater

    when irrelevant files are present in an excluded directory. Whereas, it shouldn't make a difference.

    I'm experiencing a much bigger slow down in my real-life case than in this example, but hopefully it's sufficient for probing the issue. The difference in slow downs compared to my real-life case may be because of larger data files, or because of the presence of symlinks in the excluded directories too, or it could be that the problem scales with the size of the application being analysed. Not sure.

    Footnotes

    1. Time was not displayed. It was probably rounded down to zero seconds and treated as empty. ↩

  6. ondrejmirtes commented on Jul 17, 2025

    @ondrejmirtes
    Member

    You could put the data directory completely outside of analysed paths, so that you don't need to exclude it at all, and PHPStan doesn't need to decide for each file if it's supposed to be excluded or not.

  7. mind-bending-forks commented on Jul 18, 2025

    @mind-bending-forks
    Author

    You could put the data directory completely outside of analysed paths, so that you don't need to exclude it at all

    Thanks for pointing out the workaround. You're correct that once you've worked that there is a problem and what it is, it can be worked around by reorganising files in the project so the data directory sits outside the defined scan paths, or by reconfiguring the defined scan paths in PHPStan configuration so as to not include the data directories.

    If you're following the PHPStan documentation to setup your project though:

    https://phpstan.org/user-guide/ignoring-errors#excluding-whole-files

    which has as examples of using analyseAndScan exclude data directories, you might not in the first instance even realise that PHPStan is running slower than it ought to, thinking instead that that is it's normal speed. Once you do realise that it is running unusually slow, you might not know why or that it has anything to do with the structure of non-php files in directories that you have excluded. It took me a while to work it out.

    My report was intending to make you aware that this issue exists, and as per my original post, to note that

    There is an opportunity here to improve performance for the benefit of everyone by ensuring that the structure and contents of excluded directories does not impact on the execution of PHPStan.

    You said that if the data file is outside the scan paths (rather than excluded from within the scan paths), then:

    PHPStan doesn't need to decide for each file if it's supposed to be excluded or not.

    The implication here is that PHPStan needs to every scan/enumerate every single file - even in directories that have been marked as excluded - and then take a decision on each one as to whether it should be excluded or not. I don't think that's true. Could PHPStan iterate over the file structure starting from the top level files and sub-directories, deciding whether they are excluded, and only then go down to the next level for sub-directories that have not been excluded (or have not had all their contents excluded)?

    I'm not sufficiently familiar with the PHPStan code to know what part does what and for what purpose. A search for RecursiveDirectoryIterator led to a FileCacheStorage class that seems to suggest that when PHPStan is doing caching of files, it is not accounting for exclusions at all, and so is iterating over everything. Might this explain why the speed of result cache generation and restoration is affected by files in excluded directories?

  8. ondrejmirtes commented on Jul 21, 2025

    @ondrejmirtes
    Member

    Sure, some optimization could be done. Right now PHPStan asks FileExcluder::isExcludedFromAnalysing() for each file it finds because excludePaths might contain fnmatch patterns.

    The class where the optimization could happen is FileFinder. It uses Symfony Finder.

    There was even a PR I didn't merge phpstan/phpstan-src#2503 based on symfony/symfony#50877 + symfony/symfony#50884.

    When we can update to Symfony 6.4 (and start using filter method on Finder) we'll get the update and optimization automatically.

  9. mind-bending-forks commented on Jul 21, 2025

    @mind-bending-forks
    Author

    Thanks @ondrejmirtes. I see that the documentation for the finder method of Symfony 6.4+ says

    The filter() method includes a second optional argument to prune directories. If set to true, this method completely skips the excluded directories instead of traversing the entire file/directory structure and excluding them later. When using a closure, return false for the directories which you want to prune.

    So yes, that sounds like exactly what would be required to get this enhancement implemented straightforwardly.

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