-
Notifications
You must be signed in to change notification settings - Fork 1.1k
perf(autoreload): skip stdlib/site-packages on per-cell check #9629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+251
−2
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| import modulefinder | ||
| import os | ||
| import sys | ||
| import sysconfig | ||
| import threading | ||
| import traceback | ||
| import types | ||
|
|
@@ -73,6 +74,31 @@ def safe_hasattr(obj: M, attr: str) -> bool: | |
| return False | ||
|
|
||
|
|
||
| def _non_user_module_roots() -> tuple[str, ...]: | ||
| """Filesystem prefixes that hold stdlib + site-packages modules. | ||
|
|
||
| Each entry is normalized and terminated with a separator so that a raw | ||
| prefix check on a normalized path matches whole directory boundaries | ||
| (e.g. `/usr/lib/python3.13/` does not match `/usr/lib/python3.13-mine/`). | ||
| """ | ||
| roots: set[str] = set() | ||
| for key in ("stdlib", "platstdlib", "purelib", "platlib"): | ||
| p = sysconfig.get_path(key) | ||
|
mscolnick marked this conversation as resolved.
|
||
| if p: | ||
| roots.add(p) | ||
| # Fallback for builds where sysconfig's stdlib path is missing or | ||
| # differs from the runtime location of the stdlib. | ||
| roots.add(os.path.dirname(os.__file__)) | ||
|
|
||
| normalized: set[str] = set() | ||
| for r in roots: | ||
| n = os.path.normcase(os.path.realpath(r)) | ||
| if not n.endswith(os.sep): | ||
| n += os.sep | ||
| normalized.add(n) | ||
| return tuple(normalized) | ||
|
|
||
|
|
||
| def modules_imported_by_cell( | ||
| cell: CellImpl, sys_modules: dict[str, types.ModuleType] | ||
| ) -> set[str]: | ||
|
|
@@ -160,10 +186,29 @@ def __init__(self) -> None: | |
| # for thread-safety | ||
| self.lock = threading.Lock() | ||
| self._module_dependency_finder = ModuleDependencyFinder() | ||
| # Names known to live in stdlib/site-packages. Populated lazily by | ||
| # callers that pass `skip_non_user_modules=True` (the hot per-cell | ||
| # path), and then consulted by every `check()` call. Entries are | ||
| # never evicted: a module whose `__file__` moves between roots at | ||
| # runtime would not be re-evaluated. | ||
| self._skip: set[str] = set() | ||
| self._non_user_roots = _non_user_module_roots() | ||
|
mscolnick marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Timestamp existing modules | ||
| self.check(modules=sys.modules, reload=False) | ||
|
|
||
| def _is_user_module(self, module: types.ModuleType) -> bool: | ||
| """True for modules whose source lives outside stdlib/site-packages. | ||
|
|
||
| Editable installs (e.g. `pip install -e .`) point `__file__` at the | ||
| source tree, so they are correctly classified as user code. | ||
| """ | ||
| f = safe_getattr(module, "__file__", None) | ||
| if not f: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. false positive on c libraries? Unsure, but I think so. Maybe that's fine |
||
| return False | ||
| path = os.path.normcase(os.path.realpath(f)) | ||
| return not path.startswith(self._non_user_roots) | ||
|
|
||
| def filename_and_mtime( | ||
| self, module: types.ModuleType | ||
| ) -> ModuleMTime | None: | ||
|
|
@@ -206,12 +251,23 @@ def cell_uses_stale_modules(self, cell: CellImpl) -> bool: | |
| ) | ||
|
|
||
| def check( | ||
| self, modules: dict[str, types.ModuleType], reload: bool | ||
| self, | ||
| modules: dict[str, types.ModuleType], | ||
| reload: bool, | ||
| *, | ||
| skip_non_user_modules: bool = False, | ||
| ) -> set[types.ModuleType]: | ||
| """Check timestamps of modules, optionally reload them. | ||
|
|
||
| Also patches existing objects with hot-reloaded ones. | ||
|
|
||
| When `skip_non_user_modules` is True, modules whose `__file__` is | ||
| under stdlib/site-packages are added to a persistent skip set and | ||
| won't be stat-ed on this or future calls. Intended for the per-cell | ||
| hot path. Once populated, the skip set short-circuits every caller | ||
| — including the background `ModuleWatcher` — so edits inside | ||
| installed packages are not hot-reloaded. | ||
|
|
||
| Returns a set of modules that were found to have been modified. | ||
| """ | ||
|
|
||
|
|
@@ -225,9 +281,14 @@ def check( | |
| # materialize the module keys, since we'll be reloading while | ||
| # iterating | ||
| for modname in list(modules.keys()): | ||
| if modname in self._skip: | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
|
||
| continue | ||
| m = modules.get(modname, None) | ||
| if m is None: | ||
| continue | ||
| if skip_non_user_modules and not self._is_user_module(m): | ||
|
mscolnick marked this conversation as resolved.
Outdated
|
||
| self._skip.add(modname) | ||
| continue | ||
|
|
||
| module_mtime = self.filename_and_mtime(m) | ||
| if module_mtime is None: | ||
|
|
||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.