Skip to content

TypeCombinator optimization idea #8499

Closed

Description

@ondrejmirtes

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

  1. ondrejmirtes commented on Dec 9, 2022

    @ondrejmirtes
    MemberAuthor

    Here I'm trying it out (as a new simpleUnion method) but it doesn't work for me fully yet: https://gist.github.com/ondrejmirtes/256ab5841f02ebfe36b2c7f46ebe972d

  2. rajyan commented on Dec 10, 2022

    @rajyan
    Contributor

    I’m interested in this optimization too! I’m starting by understanding deeper what the current implementation is doing.

  3. ondrejmirtes commented on Dec 10, 2022

    @ondrejmirtes
    MemberAuthor

    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

  4. ondrejmirtes commented on Dec 10, 2022

    @ondrejmirtes
    MemberAuthor

    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).

  5. rajyan commented on Dec 11, 2022

    @rajyan
    Contributor

    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.

  6. ondrejmirtes commented on Dec 11, 2022

    @ondrejmirtes
    MemberAuthor

    I have no idea what you mean, but youcan try it out 😊

  7. rajyan commented on Dec 11, 2022

    @rajyan
    Contributor

    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

    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.

    can make the bucket not normalized in the process

    ex.

    bucket 1
    [int<1. 10>, 15]
    bucket 2
    [int<10, 14>]
    

    comparing the two buckets with compareTypesInUnion makes

    bucket 1
    [int<1. 14>, 15]
    bucket 2
    [] => deleted
    

    which 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.

  8. ondrejmirtes commented on Dec 11, 2022

    @ondrejmirtes
    MemberAuthor

    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?

  9. rajyan commented on Dec 11, 2022

    @rajyan
    Contributor

    we also need to remove the type from both buckets and add a new bucket with that type?

    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 isSuperTypeOf and other unions. The bucket logic should work with simple isSuperTypeOf checks

    Other unions
    For IntegerRangeType and ConstantIntegerType we 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...

  10. ondrejmirtes commented on Jan 19, 2023

    @ondrejmirtes
    MemberAuthor

    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 :)

  11. github-actions commented on Feb 20, 2023

    @github-actions

    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.

  12. locked as resolved and limited conversation to collaborators on Feb 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions