-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 performance in union subtyping #15104
Conversation
This is a performance optimisation for subtyping between two unions that are largely the same. Fixes python#14034
This is amazing! It's a huge (>99%) improvement. |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Massive improvement, I compiled this locally and was able to type check the main branches of pydantic and pydantic-core (with our For comparison, mypy 1.2.0 with This is fast enough that it is viable once again for us to use mypy again for type-checking pydantic, but we'd definitely appreciate any further performance improvements that might be possible from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
Awesome, thank you so much. |
This is a performance optimisation for subtyping between two unions that are largely the same. Fixes #14034 This makes @adriangb's example in #14034 (comment) finish basically instantly. I could add it as a unit test? Type checking pydantic core is still not fast — takes like four or five minutes with uncompiled mypy — but at least it's now feasible. I think there's room for doing some optimisation in make_simplified_union that would improve this.
The following code optimises make_simplified_union in the common case that there are exact duplicates in the union. In this regard, this is similar to python#15104 To get this to work, I needed to use partial tuple fallbacks in a couple places (these maybe had the potential to be latent crashes anyway?) There were some interesting things going on with recursive type aliases and type state assumptions This is about a 25% speedup on the pydantic codebase and about a 2% speedup on self check (measured with uncompiled mypy)
Fixes #15192 The following code optimises make_simplified_union in the common case that there are exact duplicates in the union. In this regard, this is similar to #15104 There's a behaviour change in one unit test. I think it's good? We'll see what mypy_primer has to say. To get this to work, I needed to use partial tuple fallbacks in a couple places. These could cause crashes anyway. There were some interesting things going on with recursive type aliases and type state assumptions This is about a 25% speedup on the pydantic codebase and about a 2% speedup on self check (measured with uncompiled mypy)
This is a performance optimisation for subtyping between two unions that are largely the same.
Fixes #14034
This makes @adriangb's example in #14034 (comment) finish basically instantly. I could add it as a unit test?
Type checking pydantic core is still not fast — takes like four or five minutes with uncompiled mypy — but at least it's now feasible. I think there's room for doing some optimisation in make_simplified_union that would improve this.