Skip to content

perf: add __slots__ to SubtypeContext #19397

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

Closed
wants to merge 1 commit into from

Conversation

ngnpope
Copy link
Contributor

@ngnpope ngnpope commented Jul 8, 2025

Follow up to afd5a38.

master                    6.544s (0.0%) | stdev 0.056s 
slotted-subtypecontext    6.512s (-0.5%) | stdev 0.060s 

Copy link
Contributor

github-actions bot commented Jul 8, 2025

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

Copy link
Collaborator

@sterliakov sterliakov left a comment

Choose a reason for hiding this comment

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

Nice, thanks! This shouldn't harm, but should probably have less impact: one SubtypeContext is reused in nested is_subtype calls, while visitors are created in each of them.

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 8, 2025

Adding __slots__ isn't expected to have any real performance impact after compilation, since mypyc essentially applies the rough equivalent of the __slots__ optimization automatically to compiled (native) classes. So any performance change here is likely noise -- random fluctuations are often about 0.5%, when using the perf_compare.py tool, unfortunately.

A potential workaround to noise is to add a few different optimizations in a branch and measure their overall impact. If they add up to close to 1.0% performance improvement, we can be reasonably sure that they are beneficial in aggregate.

@ngnpope
Copy link
Contributor Author

ngnpope commented Jul 8, 2025

Ok. Good to know! 🙂

@ngnpope
Copy link
Contributor Author

ngnpope commented Jul 8, 2025

Just out of interest, I did another run to see how this and Stanislav's previous one stack over the commit before:

cb3bddd4                  6.356s (0.0%) | stdev 0.042s 
afd5a382                  6.354s (-0.0%) | stdev 0.039s 
slotted-subtypecontext    6.338s (-0.3%) | stdev 0.030s 

So neither of them are significant. I'll leave this up to you as to whether you want to merge or close it - it won't do any harm, but isn't going to make much of a dent as you say.

@sterliakov
Copy link
Collaborator

add a few different optimizations in a branch

I thought about that, but finally ruled this out: mixing multiple updates together means some of them can be masked regressions, with total improvement being caused by other changes. Maybe we should write a benchmark suite for subtype checks instead? Something that doesn't take tens of minutes to run.

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 9, 2025

I thought about that, but finally ruled this out: mixing multiple updates together means some of them can be masked regressions, with total improvement being caused by other changes.

This only makes sense if all the updates are new optimizations that you are working on. If you can't measure the impact of an individual optimization, it can be hard to decide whether to merge it or not. If you have 5 optimizations that together improve performance by 1.0%, it will be easier to make the decision to accept all the optimizations as a group. Even if some of them might actually be regressions, we can live with it since we have a high confidence that together they improve performance significantly. (There still needs to be some plausible justification why each optimization is likely to improve performance.)

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 9, 2025

Let's close this since managing __slots__ adds some minor friction to editing code. This was a good attempt though, and it's not obvious that __slots__ doesn't help generally. However, if you can show that adding __slots__ helps with test performance when running without compilation, we can reconsider some of these.

@JukkaL JukkaL closed this Jul 9, 2025
@ngnpope ngnpope deleted the slotted-subtypecontext branch July 9, 2025 16:51
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