Skip to content

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented Dec 19, 2025

In the profiles of #4628 (comment) we can see MutatingScope->createConditionalExpressions as one of the most expensive calls (which gets called via MutatingScope->merge()):

grafik

After this PR, we can see PHPSTAN_FNSR=1 php bin/phpstan analyse src/Analyser/NodeScopeResolver.php -vvv --debug taking the return $this; fast path ~180 times.

It seems make test is 0.5-1 second faster

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

WDYT?

public function mergeWith(?self $otherScope): self
{
if ($otherScope === null) {
if ($otherScope === null || $this === $otherScope) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if ($otherScope === null || $this === $otherScope) {
if ($otherScope === null || $this === $otherScope || $this->equals($otherScope)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the suggested change will make the test-suite fail with


There was 1 failure:

1) PHPStan\Analyser\NodeScopeResolverTest::testFile with data set "tests/PHPStan/Analyser/nsrt/unset-conditional-expressions.php" ('/Users/staabm/workspace/phpst...ns.php')
Failed assertions in /Users/staabm/workspace/phpstan-src/tests/PHPStan/Analyser/nsrt/unset-conditional-expressions.php:

Line 27:
Expected: array<string, mixed>
Actual:   array{}

/Users/staabm/workspace/phpstan-src/tests/PHPStan/Analyser/NodeScopeResolverTest.php:298

@ondrejmirtes
Copy link
Member

Sounds like something to fix. The equals method should probably also check conditionalExpressions property.

@staabm
Copy link
Contributor Author

staabm commented Dec 20, 2025

Sounds like something to fix. The equals method should probably also check conditionalExpressions property.

adding $this->equals($otherScope) in this already hot path will not save us much I think. it will add additional work in the common case, as we most likely will not have equal scopes. perf wise I think it will be counter productive.

if we add checks for conditionalExpressions property in equals(), I think we also need to add it to generalization?

I think we should merge this PR as is.

@ondrejmirtes ondrejmirtes merged commit 7c49de5 into phpstan:2.1.x Dec 20, 2025
633 of 641 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@ondrejmirtes
Copy link
Member

Yeah, it's unlikely that two Scopes will be the same if they're not the same object.

@staabm staabm deleted the scope-copy branch December 20, 2025 08:33
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.

2 participants