Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

perf(autoreload): cache os.path.realpath results in _is_user_module (#10086)

Vishak Baddurdc8489b4ab4f
diff --git a/marimo/_runtime/reload/autoreload.py b/marimo/_runtime/reload/autoreload.py
index 5292a86a60..f7b4553d00 100644
--- a/marimo/_runtime/reload/autoreload.py
+++ b/marimo/_runtime/reload/autoreload.py
@@ -101,6 +101,17 @@ def _non_user_module_roots() -> tuple[str, ...]:
return tuple(normalized)
+def _normalized_path(f: str) -> str:
+ """Return normcase(realpath(f)), cached to avoid repeated filesystem syscalls.
+
+ os.path.realpath resolves symlinks via filesystem syscalls for every path
+ component. Caching by the raw __file__ string is safe because a given path
+ always resolves to the same real path within a process lifetime.
+ """
+ return os.path.normcase(os.path.realpath(f))
+
+
def modules_imported_by_cell(
cell: CellImpl, sys_modules: dict[str, types.ModuleType]
) -> set[str]:
@@ -208,8 +219,7 @@ class ModuleReloader:
f = safe_getattr(module, "__file__", None)
if not f:
return False
- path = os.path.normcase(os.path.realpath(f))
- return not path.startswith(_non_user_module_roots())
+ return not _normalized_path(f).startswith(_non_user_module_roots())
def filename_and_mtime(
self, module: types.ModuleType
diff --git a/tests/_runtime/reload/test_autoreload.py b/tests/_runtime/reload/test_autoreload.py
index d30c43cec5..9a8cbbcd25 100644
--- a/tests/_runtime/reload/test_autoreload.py
+++ b/tests/_runtime/reload/test_autoreload.py
@@ -554,6 +554,26 @@ class TestSkipCache:
# Stale mtime is cleared so the next edit isn't masked by it.
assert reloader.modules_mtimes.get(py_modname, 0) < 1e12
+ def test_normalized_path_cached_across_checks(self):
+ # Regression guard: os.path.realpath is expensive (filesystem syscalls
+ # per path component). _normalized_path caches it so repeated check()
+ # calls don't re-invoke realpath for the same __file__ string.
+ import marimo._runtime.reload.autoreload as autoreload_mod
+
+ autoreload_mod._normalized_path.cache_clear()
+
+ reloader = ModuleReloader()
+ reloader.check(sys.modules, reload=False, skip_non_user_modules=True)
+ info_after_first = autoreload_mod._normalized_path.cache_info()
+ assert info_after_first.currsize > 0 # cache was populated
+
+ reloader.check(sys.modules, reload=False, skip_non_user_modules=True)
+ info_after_second = autoreload_mod._normalized_path.cache_info()
+ # No new cache entries on the second call — every path was already
+ # cached, so os.path.realpath was not re-invoked.
+ assert info_after_second.currsize == info_after_first.currsize
+ assert info_after_second.hits > info_after_first.hits
+
def test_user_module_reload_still_works(
self, tmp_path: pathlib.Path, py_modname: str
):