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

Open
wants to merge 1 commit into
base: master
Choose a base branch
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.

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