Repository navigation
Description
Feature request
Last night I came up with an idea how to optimize TypeCombinator::union(). It's one of the most heavily used methods in PHPStan. In PHPStan itself, it's responsible for about 7.5% of total run time. So the ROI here might be really big.
One of the worst cases is when the method gets a huge UnionType as the first argument (for example with 64 types), and another single type as another argument. These types are then collapsed into a single flat array of 65 types. And these types are all compared against each other.
But what if we knew that the first 64 types are already normalized? That's why I added this information: phpstan/phpstan-src@96e4443
So instead of 2080 comparisons (65 * 64 / 2), we'd just have to do 64 comparisons.
The idea is that we'd introduce $buckets (one bucket = array<Type>) and inside these buckets, we'd know that the types are not subtypes of each other, that they are normalized inside one bucket.
And after that we'd compare buckets against each other. The logic would be as follows:
- If type A from bucket J is a supertype of type B from bucket K: remove type B from bucket K. If bucket K becomes empty, remove bucket K.
- If type B from bucket K is a supertype of type A from bucket J: remove type A from bucket J, remove type B from bucket K, add a new bucket that only contains type B. If bucket J becomes empty, remove it. If bucket K becomes empty, remove it too.
And at the end, we'd be able to collapse all of the buckets into a single array of $types :)
Currently the main logic of union is these few lines: https://github.com/phpstan/phpstan-src/blob/da8413c493cbb9d800228f996836207e9598920c/src/Type/TypeCombinator.php#L316-L339
A lot of stuff around this in the union method is just about optimization, especially the scalar type handling. It's possible we'd be able to get rid of a lot of this code if the main logic became faster.
I think that @rajyan or @rvanvelzen are well-equipped to try this out, so feel free (but don't feel obligated) to do so. This idea might make you interested same as it made me interested :)
Thank you.
Activity
ondrejmirtes commentedon Dec 9, 2022
Here I'm trying it out (as a new
simpleUnionmethod) but it doesn't work for me fully yet: https://gist.github.com/ondrejmirtes/256ab5841f02ebfe36b2c7f46ebe972drajyan commentedon Dec 10, 2022
I’m interested in this optimization too! I’m starting by understanding deeper what the current implementation is doing.
ondrejmirtes commentedon Dec 10, 2022
The union() method does type normalization - compares each type against each other and keeps only the supertypes. See: https://phpstan.org/developing-extensions/type-system#type-normalization
ondrejmirtes commentedon Dec 10, 2022
This is my current attempt: https://github.com/phpstan/phpstan-src/tree/typecombinator-opti
It's not faster yet 😅 And not all tests pass. I suspect I've got some of the
$k--etc. wrong and the buckets aren't completely compared against each other (some items are skipped errorneously).rajyan commentedon Dec 11, 2022
I think the bucket logic might be implemented easier? We can treat a normalized UnionType as a bucket, and implement union of normalized UnionTypes in
compareTypesInUnion.ondrejmirtes commentedon Dec 11, 2022
I have no idea what you mean, but youcan try it out 😊
rajyan commentedon Dec 11, 2022
I hope this PR can explain my idea
phpstan/phpstan-src#2078
normalized UnionType === bucket of (UnionType)->getTypes()
other types === bucket of one item
If my understanding is correct, what we need to think about this bucket idea is that this logic
can make the bucket not normalized in the process
ex.
comparing the two buckets with
compareTypesInUnionmakeswhich makes bucket "not normalized" (if my understanding of normalized is correct) because int<1, 14>|15 can be int<1,15>
Also, with the current implementation, union of ConstantArray is not working.
ondrejmirtes commentedon Dec 11, 2022
Yeah, you're right, that complicates stuff... The type can change when coming out of compareTypesInUnion. So if the type A "wins" but it's different than the input coming out of compareTypesInUnion, we also need to remove the type from both buckets and add a new bucket with that type?
rajyan commentedon Dec 11, 2022
I don't think this solves the problem, because at the end when merging the buckets, same problem occurs. We need to flatten the bucket types and run the N * (N-1) / 2 to merge buckets in this example.
I think we need to separate
isSuperTypeOfand other unions. The bucket logic should work with simpleisSuperTypeOfchecksOther unions
For
IntegerRangeTypeandConstantIntegerTypewe can sort the types, so the operation for union should cost only O(NlogN).For ArrayTypes, I haven't came up with a good optimization...
ondrejmirtes commentedon Jan 19, 2023
I'm eager to see phpstan/phpstan-src#2078 finished but we don't need to keep this open since phpstan/phpstan-src#2078 is probably the right approach and the only shot we get at finishing this :)
github-actions commentedon Feb 20, 2023
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.