You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We now have some tooling that helps with investigating performance bottlenecks in mypy, and I've had some success using these to improve the performance of mypy. It would be good to document these workflows so that more contributors can help with this work.
Nice! FWIW I started looking at why colours is so slow. It turns out more than half of all time is spent on type-checking just two expressions. Both involve a long list (few dozens) of np.array() calls nested within dict(zip()). I am currently looking at special-casing np.array() similar to how we special-case list literals and/or adding an expression cache.
I vaguely remember seeing a repo one time which is a mypy/mypyc benchmark "service" which uses the github runner to compare self-check performance between 2 specific commits, but now I can't find it. Does this ring any bells for any of you?
It turns out more than half of all time is spent on type-checking just two expressions. Both involve a long list (few dozens) of np.array() calls nested within dict(zip()).
Do you by perhaps remember which colour expressions @ilevkivskyi ? Because I'd like to see if I can do something about those. It currently takes 9 minutes to run in mypy_primer, which for scipy-stubs is a huge CI performance bottlenece (everything else runs in <2 minutes).
Huh, am I? I basically use the same config as numpy and ruff do, so I kinda assumed that mypy_primer would compile it by default, tbh...
Also there should be no bottleneck anymore, I fixed it like a month ago.
Hmm, maybe we're talking about a different bottleneck, or perhaps the something changed about the colour codebase in the meantime 🤔. Either way, what I was talking about can be seen in e.g. https://github.com/scipy/scipy-stubs/actions/runs/17163275474/job/48697488986?pr=831, namely that colour takes ~530 seconds, whereas the next slowest project runs in ~106 seconds.
Two days ago I did manage to reduce that by one minute colour-science/colour#1358, but even with that it's still over 4 times slower than the next thing.
Anyway, I'll try and see if I can get mypy to compile then, which I'm guessing is gonna help quite a bit. Thanks for the tip!
Activity
jorenham commentedon Jul 17, 2025
Is there also tooling that allows me to see if/where there are specific parts of my stubs that are a performance bottleneck?
JukkaL commentedon Jul 17, 2025
Yes, we have something like that, but I haven't used it in a long time. I'll look into it as well.
JukkaL commentedon Jul 17, 2025
I started writing the documentation here: https://github.com/python/mypy/wiki/Profiling-and-Optimizing-Mypy
It's still work in progress, but there is already something there.
JukkaL commentedon Jul 18, 2025
Now it has at least the basics covered.
cc @ilevkivskyi @sterliakov who've been looking at performance bottlenecks.
ilevkivskyi commentedon Jul 18, 2025
Nice! FWIW I started looking at why
coloursis so slow. It turns out more than half of all time is spent on type-checking just two expressions. Both involve a long list (few dozens) ofnp.array()calls nested withindict(zip()). I am currently looking at special-casingnp.array()similar to how we special-case list literals and/or adding an expression cache.sterliakov commentedon Jul 18, 2025
Thanks, that's a nice writeup!
@ilevkivskyi then #19408 is related - also an expression cache for
colour:)jorenham commentedon Jul 18, 2025
Anything I can help with on the numpy side of things?
BobTheBuidler commentedon Aug 6, 2025
I vaguely remember seeing a repo one time which is a mypy/mypyc benchmark "service" which uses the github runner to compare self-check performance between 2 specific commits, but now I can't find it. Does this ring any bells for any of you?
brianschubert commentedon Aug 6, 2025
Perhaps you're thinking of https://github.com/sterliakov/mypy-issues?
BobTheBuidler commentedon Aug 6, 2025
jorenham commentedon Aug 24, 2025
Do you by perhaps remember which
colourexpressions @ilevkivskyi ? Because I'd like to see if I can do something about those. It currently takes 9 minutes to run in mypy_primer, which for scipy-stubs is a huge CI performance bottlenece (everything else runs in <2 minutes).update: I'm guessing it's https://github.com/colour-science/colour/blob/ae990bd75ce8d4238f4d41c7f6a99e5b15e91ce6/colour/characterisation/datasets/colour_checkers/chromaticity_coordinates.py . Optimizing it seems pretty straightforward luckily.
ilevkivskyi commentedon Aug 25, 2025
@jorenham Why do you run interpreted mypy in CI? Also there should be no bottleneck anymore, I fixed it like a month ago.
jorenham commentedon Aug 25, 2025
Huh, am I? I basically use the same config as numpy and ruff do, so I kinda assumed that mypy_primer would compile it by default, tbh...
Hmm, maybe we're talking about a different bottleneck, or perhaps the something changed about the
colourcodebase in the meantime 🤔. Either way, what I was talking about can be seen in e.g. https://github.com/scipy/scipy-stubs/actions/runs/17163275474/job/48697488986?pr=831, namely thatcolourtakes ~530 seconds, whereas the next slowest project runs in ~106 seconds.Two days ago I did manage to reduce that by one minute colour-science/colour#1358, but even with that it's still over 4 times slower than the next thing.
Anyway, I'll try and see if I can get mypy to compile then, which I'm guessing is gonna help quite a bit. Thanks for the tip!
ilevkivskyi commentedon Aug 25, 2025
I guess you are, on current master compiled mypy should be able to check colour in couple minutes or so (depending on hardware).
I am talking about my original comment, now there is no obvious hot spot in colour code where mypy spends most of the time
jorenham commentedon Aug 25, 2025
Ah I see, that explains. So no need for running mypy_primer with
--mypyc-compile-level=3then.ilevkivskyi commentedon Aug 25, 2025
I see you are using last PyPI version of mypy in CI. That one doesn't have a lot of recent perf improvements (including the fix I am talking about).