pyrefly: pyrefly gets really slow when run in environment with a lot of dependencies
Describe the Bug
I have a small 3k line project with not many dependencies
If I install its dependencies in a fresh environment, pyrefly runs in about 200ms:
INFO 17 errors shown, 4 errors ignored, 19 modules, 485 transitive dependencies, 143,623 lines, took 0.20s, peak memory physical 130.6 MiB
However, if I run in a large monolithic Python virtual environment with over a thousand installs, pyrefly runs in ~30s:
INFO 17 errors shown, 4 errors ignored, 19 modules, 17,920 transitive dependencies, 8,128,708 lines, took 26.33s, peak memory physical 1.9 GiB
The project is in a closed source repo, but I will try to see if I can make an open source reproduction.
In the meantime, is there anything I can do to figure out what the extra transitive dependencies pyrefly is checking are? --verbose doesn’t seem to make a difference
pyrefly 0.16.3
About this issue
- Original URL
- State: open
- Created a year ago
- Comments: 36 (19 by maintainers)
Commits related to this issue
- Don't enumerate files and directories that are excluded by a FilteredGlob Summary: [An issue was reported](https://github.com/facebook/pyrefly/issues/360) that mentioned installing many dependencies ... — committed to connernilsen/pyrefly by connernilsen a year ago
- Don't enumerate files and directories that are excluded by a FilteredGlob Summary: [An issue was reported](https://github.com/facebook/pyrefly/issues/360) that mentioned installing many dependencies ... — committed to connernilsen/pyrefly by connernilsen a year ago
- Don't enumerate files and directories that are excluded by a FilteredGlob Summary: [An issue was reported](https://github.com/facebook/pyrefly/issues/360) that mentioned installing many dependencies ... — committed to connernilsen/pyrefly by connernilsen a year ago
- Don't enumerate files and directories that are excluded by a FilteredGlob (#440) Summary: [An issue was reported](https://github.com/facebook/pyrefly/issues/360) that mentioned installing many depen... — committed to connernilsen/pyrefly by connernilsen a year ago
- Don't enumerate files and directories that are excluded by a FilteredGlob (#440) Summary: Pull Request resolved: https://github.com/facebook/pyrefly/pull/440 [An issue was reported](https://github.c... — committed to connernilsen/pyrefly by connernilsen a year ago
- Don't enumerate files and directories that are excluded by a FilteredGlob (#440) Summary: [An issue was reported](https://github.com/facebook/pyrefly/issues/360) that mentioned installing many depen... — committed to connernilsen/pyrefly by connernilsen a year ago
- Don't enumerate files and directories that are excluded by a FilteredGlob (#440) Summary: Pull Request resolved: https://github.com/facebook/pyrefly/pull/440 [An issue was reported](https://github.c... — committed to connernilsen/pyrefly by connernilsen a year ago
- Don't enumerate files and directories that are excluded by a FilteredGlob (#440) Summary: Pull Request resolved: https://github.com/facebook/pyrefly/pull/440 [An issue was reported](https://github.c... — committed to facebook/pyrefly by connernilsen a year ago
- Fix config finding match logic Summary: When type trying to figure out why https://github.com/facebook/pyrefly/issues/360 is happening, I noticed that we're creating tons of extra `ConfigFile`s for s... — committed to connernilsen/pyrefly by connernilsen a year ago
- Change search path stubs searching order Summary: We've been handling search paths incorrectly, since my original understanding of our approach differs from how we actually perform a search for an im... — committed to connernilsen/pyrefly by connernilsen a year ago
- Fix config finding match logic (#660) Summary: When type trying to figure out why https://github.com/facebook/pyrefly/issues/360 is happening, I noticed that we're creating tons of extra `ConfigFile... — committed to connernilsen/pyrefly by connernilsen a year ago
- Fix config finding match logic (#660) Summary: Pull Request resolved: https://github.com/facebook/pyrefly/pull/660 When type trying to figure out why https://github.com/facebook/pyrefly/issues/360 i... — committed to connernilsen/pyrefly by connernilsen a year ago
- Fix config finding match logic (#660) Summary: When type trying to figure out why https://github.com/facebook/pyrefly/issues/360 is happening, I noticed that we're creating tons of extra `ConfigFile... — committed to connernilsen/pyrefly by connernilsen a year ago
- Change search path stubs searching order (#661) Summary: We've been handling search paths incorrectly, since my original understanding of our approach differs from how we actually perform a search f... — committed to connernilsen/pyrefly by connernilsen a year ago
- Fix config finding match logic (#660) Summary: When type trying to figure out why https://github.com/facebook/pyrefly/issues/360 is happening, I noticed that we're creating tons of extra `ConfigFile... — committed to connernilsen/pyrefly by connernilsen a year ago
- Change search path stubs searching order (#661) Summary: Pull Request resolved: https://github.com/facebook/pyrefly/pull/661 We've been handling search paths incorrectly, since my original understan... — committed to connernilsen/pyrefly by connernilsen a year ago
- Fix config finding match logic (#660) Summary: Pull Request resolved: https://github.com/facebook/pyrefly/pull/660 When type trying to figure out why https://github.com/facebook/pyrefly/issues/360 i... — committed to connernilsen/pyrefly by connernilsen a year ago
- Fix config finding match logic (#660) Summary: Pull Request resolved: https://github.com/facebook/pyrefly/pull/660 When type trying to figure out why https://github.com/facebook/pyrefly/issues/360 i... — committed to facebook/pyrefly by connernilsen a year ago
- Change search path stubs searching order (#661) Summary: Pull Request resolved: https://github.com/facebook/pyrefly/pull/661 We've been handling search paths incorrectly, since my original understan... — committed to facebook/pyrefly by connernilsen a year ago
- bump to 0.24.1 Summary: would love to get some bug fixes out ASAP for #360 and #366 Reviewed By: ndmitchell Differential Revision: D78278413 fbshipit-source-id: 7880fc2925b71e3db6c1128057124d4f1e1... — committed to facebook/pyrefly by connernilsen a year ago
Okay, I have a few things that will help improve this:
<package>-stubsthird-party packages incorrectly, which would cause us to waste cycles in how we were resolving imports and the types from those importsThese combined bring my type check times down from ~30s to ~2s, and memory from ~1.8GB to ~450MB.
I’m still not convinced these fully solve the problem though, so I’m going to keep looking. I think we’re still loading extra packages that aren’t reachable, so I’m going to try to confirm that and figure out why that’s the case. I’m still seeing long type check times on newer versions of pandas and pandas-stubs.
Yay, glad you have a repro! 0.23.1 is still slow for me, in case still useful here is my dump-config:
Oh wow, I’m seeing it now. It seems to be related to having the venv located outside of the project directory, but I’m not yet sure why that would be related to how we’re finding imports. I’ll continue digging into this to see what the problem is.
Thanks for your patience with me through all of this!
Got it, I changed some steps in the script (venv in current directorY), thinking it wouldn’t cause problems, so I’ll retry using exactly what you have.
I’m still waiting for the Pyrefly patch release to go through, so you can test out with that once it’s done.
One thing that might be useful is to run
pyrefly dump-config script.pyto output some info about your config, but 0.23.1 will have an extra part of the output that will be useful to have here. I’ll update you once it goes through (I expect within the next hour), and then we can continue debugging.Okay, here’s a shareable repro, building off the
r.txtfrom earlier. Hopefully I haven’t messed up.Note this is maybe due to the large number of transitive dependencies pyrefly discovers, but I’m not sure why pyrefly is discovering these many transitive dependencies…
Script:
Output:
Perhaps interestingly, if you uninstall Pytorch, the time goes down from 17s to about 6s
watchfiles of course only has one dependency officially. Maybe if you go through all the delayed imports you somehow find your way to Pytorch and 19k deps? Note mypy and pyright are able to type check
import watchfilesfast.Ugh, yeah, it is just pytest. Sorry for the noise 😦
Let me go back to my closed source repro and see if I can find something shareable…
Hmm but this might be a red herring because we now install pytest (also 5x slowdown is still less than the 150x slowdown in my closed source case where the number of diagnostics matched). Let me try with
--no-depsto make sure pytest isn’t included…Alright, I have two PRs that should fix different parts of this:
site-package-pathis always included inproject-excludes. I’m still trying to figure out if this is something we should be doing by default. #319 should accomplish something similar, so this PR might not actually be needed.project-includes, then filter them afterward, and instead applies filtering as we go.I tested this on the project setup from above, and it brought down from ~10-20s to ~1s.
Hey @hauntsaninja, I’m coming back around to this, but I’m oncall for our team this week and got stuck handling a few big internal issues that popped up. I’ll be focusing on this more tomorrow or early next week.
I’d tried
pyrefly dump-configand it confirmed that the “Covered files” number was small and the same in both casesNo, not open source and no special pyrefly config. I’ll try to make an open source repro, should be very easy, my guess is installing a random thousand packages and checking some small project would be enough to repro.