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

Most upvoted comments

Okay, I have a few things that will help improve this:

  • #660: fixes an issue where we were re-configuring a bunch of extra stuff for third-party packages that we should already have had cached
  • #661: fixes an issue where we were looking inside <package>-stubs third-party packages incorrectly, which would cause us to waste cycles in how we were resolving imports and the types from those imports

These 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:

(env) ~/tmp/proj λ pyrefly --version
pyrefly 0.23.1

(env) ~/tmp/proj λ pyrefly dump-config script.py
Default configuration
  Using interpreter: /Users/shantanu/tmp/env/bin/python3
  Covered files:
    /Users/shantanu/tmp/proj/script.py
  Fallback search path (guessed from project_includes): ["/Users/shantanu/tmp/proj", "/Users/shantanu/tmp", "/Users/shantanu", "/Users", "/"]
  Site package path queried from interpreter: ["/Users/shantanu/Library/Application Support/uv/python/cpython-3.12.9-macos-aarch64-none/lib/python3.12", "/Users/shantanu/Library/Application Support/uv/python/cpython-3.12.9-macos-aarch64-none/lib/python3.12/lib-dynload", "/Users/shantanu/tmp/env/lib/python3.12/site-packages"]

(env) ~/tmp/proj λ time pyrefly check --verbose script.py -j 8
DEBUG Checking 1 files (listing took 0.00s)
DEBUG Running with 8 threads (5 MiB stack size)
[00:00:00] ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░       0/0      DEBUG Running epoch 1
 INFO errors shown: 0, errors ignored: 0, modules: 1, transitive dependencies: 19,134, lines: 8,756,230, time: 11.79s, peak memory: physical 2 GiB
pyrefly check --verbose script.py -j 8  73.28s user 3.90s system 650% cpu 11.856 total

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.py to 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.txt from 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:

rm -rf proj
mkdir proj
cd proj

curl https://pastebin.com/raw/5nBviuGM > r.txt  # r.txt from previously in this issue
echo 'import watchfiles' > script.py

rm -rf ~/tmp/env
uv venv ~/tmp/env --python 3.12

source ~/tmp/env/bin/activate
uv pip install -r r.txt
uv pip install pyrefly==0.19

time pyrefly check --verbose script.py -j 8

Output:

DEBUG Checking 1 files (listing took 0.00s)
DEBUG Running with 8 threads
[00:00:00] ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░       0/0      DEBUG Running epoch 1
 INFO errors shown: 0, errors ignored: 0, modules: 1, transitive dependencies: 19,097, lines: 8,713,007, time: 17.00s, peak memory: physical 2.1 GiB

real	0m17.109s
user	2m1.011s
sys	0m4.745s

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 watchfiles fast.

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-deps to make sure pytest isn’t included…

Alright, I have two PRs that should fix different parts of this:

  • #439 should make sure your site-package-path is always included in project-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.
  • #440 is the main fix here, which fixes our globbing logic to not naively enumerate all files from a 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-config and it confirmed that the “Covered files” number was small and the same in both cases

No, 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.