Skip to content

Faster DuplicateClassDeclarationRule#6038

Merged
staabm merged 3 commits into
phpstan:2.2.xfrom
staabm:class
Jul 11, 2026
Merged

Faster DuplicateClassDeclarationRule#6038
staabm merged 3 commits into
phpstan:2.2.xfrom
staabm:class

Conversation

@staabm

@staabm staabm commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

inspired by 11aebc1

since DuplicateClassDeclarationRule is the only caller of MemoizingReflector::reflectAllClasses, lets optimize the rule for its very specific use-case instead of memoizing only parts of the costs in MemoizingReflector.

DuplicateFunctionDeclarationRule will be optimized in a separate PR.


before this PR:

➜  phpstan-src git:(class) hyperfine 'php -d memory_limit=450M bin/phpstan analyze -v --debug src/Analyser/Ignore' -i
Benchmark 1: php -d memory_limit=450M bin/phpstan analyze -v --debug src/Analyser/Ignore
  Time (mean ± σ):      2.547 s ±  0.019 s    [User: 2.204 s, System: 0.338 s]
  Range (min … max):    2.522 s …  2.583 s    10 runs

after this PR:

➜  phpstan-src git:(class) ✗ hyperfine 'php -d memory_limit=450M bin/phpstan analyze -v --debug src/Analyser/Ignore' -i
Benchmark 1: php -d memory_limit=450M bin/phpstan analyze -v --debug src/Analyser/Ignore
  Time (mean ± σ):      2.392 s ±  0.014 s    [User: 2.082 s, System: 0.304 s]
  Range (min … max):    2.376 s …  2.416 s    10 runs

@staabm staabm marked this pull request as ready for review July 11, 2026 15:51
@phpstan-bot

Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

@staabm staabm requested a review from VincentLanglet July 11, 2026 15:53
Comment on lines -43 to -46
foreach ($allClasses as $reflectionClass) {
if ($reflectionClass->getName() !== $className) {
continue;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

before this PR, the rule iterated all reflected classes, for every class found while analysis.

after the PR we built a map once and than lookup said map so we don't need to iterate the possible big array of all reflected classes over and over.

@staabm staabm merged commit fbecbb8 into phpstan:2.2.x Jul 11, 2026
675 of 676 checks passed
@staabm staabm deleted the class branch July 11, 2026 20:16
staabm referenced this pull request Jul 11, 2026
Each call swept every source locator, re-reading and re-parsing all stub
files evicted from the parser cache. DuplicateClassDeclarationRule and
DuplicateFunctionDeclarationRule call these for every class/function
node during stub validation, causing ~4900 redundant file reads per
process on phpstan-src self-analysis.
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.

3 participants