Skip to content
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

Fix slow enum-union with lots of cases #2985

Merged
merged 4 commits into from Mar 22, 2024
Merged

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Mar 22, 2024

closes phpstan/phpstan#10772

this effectively reverts 0a39b42 but fixes a nasty perf problem

the reproducer of #10772 took >2 minutes before this change and finishes instantly after it

$ time ../phpstan-src/bin/phpstan analyse --debug
Note: Using configuration file C:\dvl\Workspace\stan-long\phpstan.neon.
C:\dvl\Workspace\stan-long\src\test.php


 [OK] No errors



real    0m2.669s
user    0m0.000s
sys     0m0.000s

Todo: regression test

@staabm
Copy link
Contributor Author

staabm commented Mar 22, 2024

Todo: regression test

not sure where to put the regression test, since we need a composer.json/lock. in the past we used AnalyzerIntegration test, but this doesn't have composer.json project level.

should I do a end-2-end test? if so, how/where to put a "timeout" for it?


alternatively I can try to reduce the existing test to something without external composer dependencies

@staabm
Copy link
Contributor Author

staabm commented Mar 22, 2024

(added regression test)

@@ -200,8 +201,7 @@ public static function union(Type ...$types): Type
if ($types[$i] instanceof StringType && !$types[$i] instanceof ClassStringType) {
$hasGenericScalarTypes[ConstantStringType::class] = true;
}
$enumCases = $types[$i]->getEnumCases();
Copy link
Contributor

Choose a reason for hiding this comment

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

If the issue is with getEnumCases be slow.

Shouldn't be introduced a new method isSomething, implemented by all types in order to avoid the error message

Doing instanceof PHPStan\\\\Type\\\\Enum\\\\EnumCaseObjectType is error\\-prone and deprecated\\

?

Then

if ($types[$i]->isSomething()->yes()) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure its worth a new type-method just for this bottleneck

@ondrejmirtes ondrejmirtes merged commit 944673f into phpstan:1.10.x Mar 22, 2024
436 of 441 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants