Skip to content

Faster processing of array comparisons with constant offsets #3933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 13, 2025

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Apr 13, 2025

step debugging thru the example made me realize that most of the time we were adding the same constant types to the scope again. This PR detects this situation and does a fast exit to prevent unnecessary scope creations.

grafik

before this PR

time php bin/phpstan analyze bug-12800.php --debug
  13.40s user 0.18s system 99% cpu 13.637 total

after this PR

time php bin/phpstan analyze bug-12800.php --debug
  2.84s user 0.13s system 99% cpu 2.974 total
grafik

closes phpstan/phpstan#12800

@staabm staabm marked this pull request as ready for review April 13, 2025 07:52
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@@ -4428,6 +4428,10 @@ public function addTypeToExpression(Expr $expr, Type $type): self

if ($originalExprType->equals($nativeType)) {
$newType = TypeCombinator::intersect($type, $originalExprType);
if ($newType->isConstantScalarValue()->yes() && $newType->equals($originalExprType)) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't get it, why only for $newType->isConstantScalarValue()->yes()?

And: can we make it faster for the other return in this method too?

Copy link
Contributor Author

@staabm staabm Apr 13, 2025

Choose a reason for hiding this comment

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

I don't get it, why only for $newType->isConstantScalarValue()->yes()?

I went with constant scalars as these matched best the slow case at hand

additionally there are types with weird equals-semantics like ...

  • $mixed->equals($error) => true (because ErrorType extends MixedType)
  • ObjectType

... for which tests start failing if we take the fast-pass without changing the scope

And: can we make it faster for the other return in this method too?

I can't think of a case where this would help

Copy link
Member

Choose a reason for hiding this comment

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

These equals problems should be fixed, they might lead to other bugfixes as well.

@ondrejmirtes ondrejmirtes merged commit cd2ca64 into phpstan:2.1.x Apr 13, 2025
416 of 417 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the bug12800 branch April 13, 2025 19:50
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.

Performance issue with a sample code
3 participants