Optimise venv identification for mid/large-sized directory trees#4242
Open
alexander-beedie wants to merge 1 commit into
Open
Optimise venv identification for mid/large-sized directory trees#4242alexander-beedie wants to merge 1 commit into
venv identification for mid/large-sized directory trees#4242alexander-beedie wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Overview
When
pyrefly checkruns over a package without a configured interpreter, it auto-discovers a project'svenv.If "pyvenv.cfg" isn't immediately found in the project root or ".venv" / "venv", it falls back to an unfiltered
WalkDir(to depth 3) over the entire project, testing every filename against the interpreter regex.For large projects, this can be a significant fixed cost. For example, profiling showed it taking up ~20% of the total runtime for checking
homeassistant, as that package has ~2,700 directories (!!). For small projects there is no change.Solution
walk_interpretergains a simple "should_descend" predicate that prunes a directory's subtree when the predicate rejects it; a venv-aware pruning rule is supplied infind_in_dir.The prune is a strict superset of the reachable interpreters, so which interpreter the
findresolves is unchanged, but the non-venv source fan-out is skipped.In the extreme
homeassistantcase, this reduces ~2,700opendircalls down to only ~15, making the check essentially free (instead of ~20% of the runtime) 🚀Test Plan
All existing tests pass, and two new
venvdiscovery tests were added (which actually represent the majority of this PR as the code change itself is small).Benchmarks1
Timed across a sample of the
mypy_primerprojects. Speedup is proportional to directory-tree size; significant on large trees, negligible on small flat projects.dirs ≤ 3= how many directories (with depth ≤ 3) in the project.modules= how many modules in the project.before= pyrefly on main branch.after= main + this PR.Footnotes
Test machine: Apple Silicon M3 Max. ↩