-
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
Merged
Changes from 1 commit
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,19 @@ 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.""" | ||
| 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) | ||
| # Covers stdlib for non-venv interpreters, where sysconfig's stdlib path | ||
| # may not match the runtime location. | ||
| roots.add(sys.base_prefix) | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
|
||
| return tuple(roots) | ||
|
|
||
|
|
||
| def modules_imported_by_cell( | ||
| cell: CellImpl, sys_modules: dict[str, types.ModuleType] | ||
| ) -> set[str]: | ||
|
|
@@ -160,10 +174,26 @@ 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 on the | ||
| # hot per-cell path. Entries are never evicted: a module whose | ||
| # __file__ moves between roots at runtime would not be re-evaluated. | ||
| self._skip: set[str] = set() | ||
|
mscolnick marked this conversation as resolved.
Outdated
|
||
| 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 | ||
|
mscolnick marked this conversation as resolved.
Outdated
|
||
| 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 | ||
| return not f.startswith(self._non_user_roots) | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
mscolnick marked this conversation as resolved.
Outdated
|
||
|
|
||
| def filename_and_mtime( | ||
| self, module: types.ModuleType | ||
| ) -> ModuleMTime | None: | ||
|
|
@@ -206,12 +236,21 @@ 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, stdlib and site-packages | ||
|
mscolnick marked this conversation as resolved.
Outdated
|
||
| modules are skipped via a persistent cache — intended for the per-cell | ||
| hot path. The background ``ModuleWatcher`` keeps the default so that | ||
|
mscolnick marked this conversation as resolved.
Outdated
|
||
| edits to installed packages remain detectable at watcher latency. | ||
|
|
||
| Returns a set of modules that were found to have been modified. | ||
| """ | ||
|
|
||
|
|
@@ -222,12 +261,18 @@ def check( | |
| # and also iterates over it | ||
| with self.lock: | ||
| modified_modules: set[types.ModuleType] = set() | ||
| skip = self._skip if skip_non_user_modules else None | ||
|
mscolnick marked this conversation as resolved.
Outdated
|
||
| # materialize the module keys, since we'll be reloading while | ||
| # iterating | ||
| for modname in list(modules.keys()): | ||
| if skip is not None and modname in skip: | ||
|
mscolnick marked this conversation as resolved.
Outdated
|
||
| continue | ||
| m = modules.get(modname, None) | ||
| if m is None: | ||
| continue | ||
| if skip is not None and not self._is_user_module(m): | ||
|
mscolnick marked this conversation as resolved.
Outdated
|
||
| skip.add(modname) | ||
|
mscolnick marked this conversation as resolved.
Outdated
|
||
| 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.