Skip to content

Performance degradation introduced in 2.1.41 #14452

@Zombaya

Description

@Zombaya

Bug report

Summary

Upgrading PHPStan to the latest version caused a severe performance regression.

Before the upgrade, analysis of the affected code took about 10 minutes. After upgrading, the analysis did not complete even after 45 minutes, and subprocesses eventually timed out.

Regression range

I was able to narrow the issue down to version 2.1.41.

  • 2.1.40: works normally
  • 2.1.41: severe slowdown / extremely long analysis time

Reproduction

I created a minimal repository to reproduce the issue:

https://github.com/Zombaya/phpstan-2_1_41-performance-degradation

I tried to reduce the example as much as possible, but I could not simplify it further without knowing more about how PHPStan analyzes it internally. Removing some seemingly unrelated lines suddenly removed any slowdowns.

Observed behavior

There are two relevant files/classes in the reproduction:

  • Slow.php
  • NeverEnding.php

The files are effectively the same, except that Slow.php has one code block commented out.

That commented-out block still makes analysis on 2.1.41 much slower than on 2.1.40, but it does not cause the analysis to become endlessly slow.

When the code block is present, NeverEnding.php becomes even slower to analyze.

Timing results

File Time on 2.1.40 Time on 2.1.41
Slow.php 0.3s 3.5s
NeverEnding.php 0.3s 14m 50s

Test command

I tested the timings using: vendor/bin/phpstan a --level=0 -vvv --debug src/Slow.php

Sample output of NeverEnding

$ vendor/bin/phpstan a --level=8 -vvv --debug src/NeverEnding.php 
Result cache not used because of debug mode.
/home/int8/phpstan-performance-degradation/src/NeverEnding.php
--- consumed 36.5 MB, total 84.5 MB, took 889.92 s, 0.093 LoC/s
Result cache was not saved because only files were passed as analysed paths.
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 
  Line   NeverEnding.php                                                                                                                                                                           
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 
  12     Method Zombaya\PhpstanPerformanceDegradation\NeverEnding::foo() has parameter $input with generic class Symfony\Component\HttpFoundation\InputBag but does not specify its types: TInput  
         🪪  missingType.generics                                                                                                                                                                  
  69     Parameter #1 $type of static method Zombaya\PhpstanPerformanceDegradation\Shared::calculateThings() expects int|null, bool|float|int|string|null given.                                   
         🪪  argument.type                                                                                                                                                                         
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 


                                                                                                                        
 [ERROR] Found 2 errors                                                                                                 
                                                                                                                        

PHP runtime version: 8.2.30
PHP version for analysis: 8.2.30 (from runtime)

PHPStan version: 2.1.41
PHPStan running from:
/home/int8/phpstan-performance-degradation/vendor/phpstan/phpstan

Extension installer: Not installed

Discovered Composer project root:
/home/int8/phpstan-performance-degradation

Parallel processing scheduler:
# of detected CPU cores:   8
# of analysed files:       1
# of jobs:                 1
# of spawned processes:    1

Elapsed time: 14 minutes 50 seconds
Used memory: 84.5 MB

Code snippet that reproduces the problem

No response

Expected output

I expect the repro-project to be able to be analyzed within 1 second.

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

PHPStan is a very valuable tool in our toolbox to keep our project in a good shape.

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