KEMBAR78
Speed up the default plugin by JukkaL · Pull Request #19385 · python/mypy · GitHub
Skip to content

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Jul 6, 2025

Fix two kinds of inefficiency in the default plugin:

  • Pre-calculate various set objects, since set construction is pretty slow
  • Nested imports are pretty slow, so avoid doing them in the fast path

I also had to refactor things a little in order to optimize the nested imports.

This speeds up self check by about 2.2%. The default plugin is called a lot.

@JukkaL JukkaL requested a review from ilevkivskyi July 6, 2025 12:34
@github-actions
Copy link
Contributor

github-actions bot commented Jul 6, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Nice. I am thinking however, is it possible to refactor the plugins to avoid the nested imports altogether? I mean there will be still penalty for people who do use functools, dataclasses, ctypes etc. I remember I did something similar for checkmember refactor by introducing some "interface classes".

@JukkaL
Copy link
Collaborator Author

JukkaL commented Jul 7, 2025

Is it possible to refactor the plugins to avoid the nested imports altogether? ... I remember I did something similar for checkmember refactor by introducing some "interface classes".

Probably? I didn't look into the root cause of the import cycle (I'm not even sure if I even checked if there is an import cycle -- I just assumed because of the imports within functions.)

@JukkaL JukkaL merged commit 3557e22 into master Jul 7, 2025
19 checks passed
@JukkaL JukkaL deleted the mypyc-micro-opt-2 branch July 7, 2025 09:09
JukkaL added a commit that referenced this pull request Jul 16, 2025
Use precalculated set objects in more places.

This is similar to #19385. Some cases were still unoptimized.

I used trace logging (#19457) to identify functions where we were
creating many set objects, and I noticed that these were unncessary.

This is a part of a set of micro-optimizations that improve self check
performance by ~5.5%.
JukkaL added a commit that referenced this pull request Jul 16, 2025
Use precalculated set objects in more places.

This is similar to #19385. Some cases were still unoptimized.

I used trace logging (#19457) to identify functions where we were
creating many set objects, and I noticed that these were unnecessary.

This is a part of a set of micro-optimizations that improve self check
performance by ~5.5%.
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.

3 participants