perf(resolution): skip os.getcwd() in the path-memo key for absolute paths
_source_key and _resolve_cached key their lru_cache on the path and the
cwd, and call os.getcwd() on every lookup, hit or miss. On a 900-file
repo an extract makes about 215k of those calls. os.getcwd() is a
syscall that costs 30 to 40us on macOS, so building the key cost more
than the resolution the memo saved.
An absolute path resolves the same from any cwd, so it now keys with an
empty cwd. A relative path still keys on the cwd, so a chdir still misses
the cache. Path.is_absolute() applies host rules, which is what local
resolution needs: a driveless path on Windows depends on the drive.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 2228f80804)
This commit is contained in:
1 parent
289e870962
commit
16378b4846
4 files changed
+58
-7
No files matched your search
+5
-4
@@ -7956,10 +7956,11 @@ def extract(
|
||||
_XAML_CSHARP_CLASS_CACHE.clear()
|
||||
_MD_LINK_INDEX_CACHE.clear()
|
||||
_SCAN_ROOT_NAMESPACE_CACHE.clear()
|
||||
# Path-resolution memoization (#3500) is keyed by (path, cwd) with no mtime
|
||||
# component, so — like the alias caches above — a symlink repoint or a path
|
||||
# that starts/stops existing between rebuilds in a long-lived `graphify
|
||||
# watch` / MCP process would otherwise replay a stale result. Clear per run.
|
||||
# Path-resolution memoization (#3500) is keyed by path (plus the cwd for a
|
||||
# relative path) with no mtime component, so — like the alias caches above —
|
||||
# a symlink repoint or a path that starts/stops existing between rebuilds in
|
||||
# a long-lived `graphify watch` / MCP process would otherwise replay a stale
|
||||
# result. Clear per run.
|
||||
_cached_realpath.cache_clear()
|
||||
_cached_source_key.cache_clear()
|
||||
|
||||
|
||||
@@ -944,6 +944,18 @@ def _vue_mask_non_script(src: str) -> tuple[str, str | None]:
|
||||
out.append(_blank(src[pos:]))
|
||||
return "".join(out), lang
|
||||
|
||||
def _memo_cwd(path_str: str) -> str:
|
||||
"""The cwd part of a path-memo key: empty for an absolute path (#perf).
|
||||
|
||||
An absolute path resolves the same from any working directory, so only a
|
||||
relative one keys on the cwd. That saves an ``os.getcwd()`` syscall on
|
||||
every memo hit. Host rules are the right ones here: a driveless ``\\x``
|
||||
on Windows depends on the current drive, and ``Path.is_absolute()``
|
||||
keeps it keyed on the cwd.
|
||||
"""
|
||||
return "" if Path(path_str).is_absolute() else os.getcwd()
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=65536)
|
||||
def _cached_source_key(source_file: str, root_str: str, _cwd: str) -> str:
|
||||
"""Resolve-and-relativize one source path, memoized (#perf).
|
||||
@@ -968,7 +980,7 @@ def _cached_source_key(source_file: str, root_str: str, _cwd: str) -> str:
|
||||
def _source_key(source_file: str, root: Path) -> str:
|
||||
if not source_file:
|
||||
return ""
|
||||
return _cached_source_key(source_file, str(root), os.getcwd())
|
||||
return _cached_source_key(source_file, str(root), _memo_cwd(source_file))
|
||||
|
||||
def _node_disambiguation_source_key(node: dict, root: Path) -> str:
|
||||
source_file = str(node.get("source_file", ""))
|
||||
@@ -1135,7 +1147,7 @@ def _cached_realpath(path_str: str, _cwd: str) -> Path:
|
||||
|
||||
|
||||
def _resolve_cached(path: "Path | str") -> Path:
|
||||
"""``Path.resolve()`` with a per-(path, cwd) memo (#perf).
|
||||
"""``Path.resolve()`` with a per-path memo, cwd-keyed when relative (#perf).
|
||||
|
||||
The symbol-resolution passes resolve the same few hundred corpus paths
|
||||
once per FACT — per import, per export, per use, per node — which on
|
||||
@@ -1147,7 +1159,8 @@ def _resolve_cached(path: "Path | str") -> Path:
|
||||
``Path.resolve()`` — callers keep their own try/except — and an
|
||||
exception is never cached.
|
||||
"""
|
||||
return _cached_realpath(str(path), os.getcwd())
|
||||
path_str = str(path)
|
||||
return _cached_realpath(path_str, _memo_cwd(path_str))
|
||||
|
||||
|
||||
def _js_source_path(source_file: str, root: Path) -> Path | None:
|
||||
|
||||
@@ -68,3 +68,22 @@ def test_exception_paths_match_resolve(tmp_path, monkeypatch):
|
||||
monkeypatch.chdir(tmp_path)
|
||||
weird = "a/../b/./c.py"
|
||||
assert _resolve_cached(weird) == Path(weird).resolve()
|
||||
|
||||
|
||||
@needs_cache
|
||||
def test_absolute_path_lookup_skips_getcwd(tmp_path, monkeypatch):
|
||||
"""An absolute path resolves the same from any cwd, so its key leaves the
|
||||
cwd out and a lookup never calls os.getcwd() (a syscall per call)."""
|
||||
target = tmp_path / "a.py"
|
||||
expected = target.resolve()
|
||||
assert _resolve_cached is not None
|
||||
calls = []
|
||||
real_getcwd = os.getcwd
|
||||
monkeypatch.chdir(tmp_path)
|
||||
assert _resolve_cached("a.py") == expected # warm the memo
|
||||
monkeypatch.setattr(os, "getcwd", lambda: calls.append(1) or real_getcwd())
|
||||
assert _resolve_cached(target) == expected
|
||||
assert _resolve_cached(str(target)) == expected
|
||||
assert calls == []
|
||||
assert _resolve_cached("a.py") == expected
|
||||
assert calls, "a relative path keys on the cwd, even on a memo hit"
|
||||
@@ -84,3 +84,21 @@ def test_cache_is_cwd_sensitive(tmp_path, monkeypatch):
|
||||
# And cross-root: from d2, resolving against d1's root must fall back to
|
||||
# the raw path (out-of-root), exactly as the unmemoized code did.
|
||||
assert _source_key("mod.py", d1) == _reference_source_key("mod.py", d1)
|
||||
|
||||
|
||||
@needs_cache
|
||||
def test_absolute_path_lookup_skips_getcwd(tmp_path, monkeypatch):
|
||||
"""An absolute source_file keys without the cwd, so _source_key never
|
||||
calls os.getcwd() for it. A relative one still does."""
|
||||
(tmp_path / "a.py").write_text("x = 1\n", encoding="utf-8")
|
||||
absolute = str(tmp_path / "a.py")
|
||||
assert _cached_source_key is not None
|
||||
calls = []
|
||||
real_getcwd = os.getcwd
|
||||
monkeypatch.chdir(tmp_path)
|
||||
assert _source_key("a.py", tmp_path) == "a.py" # warm the memo
|
||||
monkeypatch.setattr(os, "getcwd", lambda: calls.append(1) or real_getcwd())
|
||||
assert _source_key(absolute, tmp_path) == "a.py"
|
||||
assert calls == []
|
||||
assert _source_key("a.py", tmp_path) == "a.py"
|
||||
assert calls, "a relative path keys on the cwd, even on a memo hit"
|
||||
Reference in new issue
Block a user