Skip to content

Speed up freshening type variables#14323

Merged
JukkaL merged 8 commits intomasterfrom
faster-typevars
Dec 20, 2022
Merged

Speed up freshening type variables#14323
JukkaL merged 8 commits intomasterfrom
faster-typevars

Conversation

@JukkaL
Copy link
Copy Markdown
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
Copy Markdown
Contributor

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

Comment thread mypy/type_visitor.py
Comment on lines +554 to +555
if self.seen_aliases is None:
self.seen_aliases = set()
Copy link
Copy Markdown
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
Copy Markdown
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