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

Speed up freshening type variables #14323

Merged
merged 8 commits into from
Dec 20, 2022
Merged

Speed up freshening type variables #14323

merged 8 commits into from
Dec 20, 2022

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Dec 20, 2022

Only perform type variable freshening if it's needed, i.e. there is a nested generic callable, since it's fairly expensive. Make the check for generic callables fast by creating a specialized type query visitor base class for queries with bool results. The visitor tries hard to avoid memory allocation in typical cases, since allocation is slow.

This addresses at least some of the performance regression in #14095. This improved self-check performance by about 3% when compiled with mypyc (-O2).

The new visitor class can potentially help with other type queries as well. I'll explore it in follow-up PRs.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Comment on lines +554 to +555
if self.seen_aliases is None:
self.seen_aliases = set()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we just start with an empty set rather than a None? That should simplify this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We start with None since it saves the creation (and deletion) of an empty set in the common code path where we don't use the set at all. It's a micro-optimization for sure, but allocating objects is kind of expensive in CPython and this is a hot code path, so it should make things a little bit faster.

@JukkaL JukkaL merged commit c588852 into master Dec 20, 2022
@JukkaL JukkaL deleted the faster-typevars branch December 20, 2022 15:35
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.

2 participants