Cache pkgutil namespace probes during import resolution#4017
Open
alexander-beedie wants to merge 1 commit into
Open
Cache pkgutil namespace probes during import resolution#4017alexander-beedie wants to merge 1 commit into
pkgutil namespace probes during import resolution#4017alexander-beedie wants to merge 1 commit into
Conversation
This comment has been minimized.
This comment has been minimized.
4b4b0fc to
56651d1
Compare
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Contributor
|
I can try to find someone with a windows machine to do some benchmarking |
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
Resolving a module and its submodules walks each path component, and every intermediate package's
__init__.pyis probed withis_pkgutil_namespace.Crucially the same
__init__files are re-opened/re-read once per resolved submodule, leading to a lot of redundant calls (each of which is aFile::open+ 4KiBread+ a regex to check forpkgutil.extend_path).Solution
Memoise the result in a new
pkgutil_cacheonDirEntryCache, keyed by__init__path, sharing the same "per-loader, never-invalidated" lifetime as the existing entry cache. The existingcachename was updated toentry_cacheto help differentiate/clarify now there are two caches.Impact
Reduces ~14,900 reads to ~1,150 when testing against the
corepackage.(One per unique
__init__reached, for a ~92% reduction).Didn't see a meaningful performance impact on macOS (cached reads are cheap?). I'd expect a more noticeable positive change on Windows though.
This is additive to #3994, with respect to #3993.
Test Plan
No type-inference logic changes; tests return identical before/after test results.