Skip to content

Commit 98d8389

Browse files
Max17190claude
authored andcommitted
perf(watch): rebase each preserved source path once per update
An incremental update calls _StoredSourcePaths.rebase_preserved for every preserved node, edge and hyperedge, and absolute_identity for every stored node and edge of the resolve context. Each call builds Path objects and walks parent directories, though the answer depends only on the stored source_file and roots fixed in __init__. Guava has 337k such items from 3.3k files. Cache both per file, as 1d38e84 did for identity(). A one-file update on Guava goes from 51.4s to 43.8s, with the reconcile step from 8.48s to 1.96s. graph.json, graph.html and GRAPH_REPORT.md are byte-identical on seven repos after a full build, one-file edits, and a delete plus add. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 5044b76)
1 parent 050fc6d commit 98d8389

2 files changed

Lines changed: 106 additions & 8 deletions

File tree

‎graphify/watch.py‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,8 @@ def __init__(
628628
self.existing_source_root = project_root
629629
self._identity_cache: dict[str, str | None] = {}
630630
self._in_watch_root_cache: dict[str, bool] = {}
631+
self._absolute_identity_cache: dict[tuple[str, Path], str | None] = {}
632+
self._rebased_cache: dict[str, str | None] = {}
631633
relative_marker_prefix: str | None = None
632634

633635
root_marker = out / ".graphify_root"
@@ -718,6 +720,19 @@ def normalize(self, source_file: str | None) -> str | None:
718720
return posixpath.normpath(normalized) if normalized else normalized
719721

720722
def absolute_identity(self, source_file: str | None, root: Path) -> str | None:
723+
# An incremental update asks this for every stored node and edge while
724+
# building the resolve context, so cache it per file and root like
725+
# identity() below.
726+
if not isinstance(source_file, str):
727+
return self._absolute_identity_uncached(source_file, root)
728+
key = (source_file, root)
729+
try:
730+
return self._absolute_identity_cache[key]
731+
except KeyError:
732+
identity = self._absolute_identity_cache[key] = self._absolute_identity_uncached(source_file, root)
733+
return identity
734+
735+
def _absolute_identity_uncached(self, source_file: str | None, root: Path) -> str | None:
721736
normalized = self._normalize_source(source_file)
722737
if not normalized:
723738
return normalized
@@ -759,19 +774,31 @@ def is_evicted(self, item: dict, identities: set[str]) -> bool:
759774
return self.identity(item.get("source_file")) in identities
760775

761776
def rebase_preserved(self, item: dict) -> None:
762-
identity = self.identity(item.get("source_file"))
777+
# Called once for every preserved node, edge and hyperedge. The new
778+
# source_file depends only on the old one, so work it out once per file.
779+
source_file = item.get("source_file")
780+
if not isinstance(source_file, str):
781+
rebased = self._rebased_uncached(source_file)
782+
else:
783+
try:
784+
rebased = self._rebased_cache[source_file]
785+
except KeyError:
786+
rebased = self._rebased_cache[source_file] = self._rebased_uncached(source_file)
787+
if rebased is not None:
788+
item["source_file"] = rebased
789+
790+
def _rebased_uncached(self, source_file: str | None) -> str | None:
791+
"""The source_file a preserved item is stored with, or None to leave it as is."""
792+
identity = self.identity(source_file)
763793
if not identity:
764-
return
794+
return None
765795
identity_path = Path(identity)
766796
if not _is_relative_to(identity_path, self.watch_root):
767-
normalized = self.normalize(item.get("source_file"))
768-
if normalized:
769-
item["source_file"] = normalized
770-
return
797+
return self.normalize(source_file) or None
771798
try:
772-
item["source_file"] = identity_path.relative_to(self.project_root).as_posix()
799+
return identity_path.relative_to(self.project_root).as_posix()
773800
except ValueError:
774-
item["source_file"] = identity
801+
return identity
775802

776803

777804
# A source_file that is a URL/virtual scheme (gdoc://, s3://, http://, ...) rather

‎tests/test_watch_identity_cache.py‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,74 @@ def test_cached_answers_match_a_fresh_computation(tmp_path):
5757
assert paths.in_watch_root(f) is expected
5858
assert paths.in_watch_root("src/a.py") is True
5959
assert paths.in_watch_root(outside) is False
60+
61+
62+
def test_rebase_and_absolute_identity_are_computed_once_per_source_file(tmp_path, monkeypatch):
63+
# rebase_preserved runs for every preserved node, edge and hyperedge, and
64+
# absolute_identity for every stored node and edge of an incremental update.
65+
import graphify.watch as watch
66+
67+
calls: list[str | None] = []
68+
69+
def counting_normalize(p, root=None):
70+
calls.append(p)
71+
return _norm_source_file(p, root)
72+
73+
containment_checks: list[Path] = []
74+
real_is_relative_to = watch._is_relative_to
75+
76+
def counting_is_relative_to(path, root):
77+
containment_checks.append(path)
78+
return real_is_relative_to(path, root)
79+
80+
monkeypatch.setattr(watch, "_is_relative_to", counting_is_relative_to)
81+
paths = _paths(tmp_path, counting_normalize)
82+
outside = (tmp_path.parent / "elsewhere" / "x.py").as_posix()
83+
files = ["src/a.py", "lib\\c.py", outside]
84+
85+
def one_pass():
86+
for f in files:
87+
paths.rebase_preserved({"source_file": f})
88+
paths.absolute_identity(f, tmp_path)
89+
90+
one_pass()
91+
first_pass = (len(calls), len(containment_checks))
92+
assert first_pass[0] > 0 and first_pass[1] > 0
93+
for _ in range(50):
94+
one_pass()
95+
assert (len(calls), len(containment_checks)) == first_pass
96+
97+
98+
def test_rebase_preserved_rewrites_each_case_as_before(tmp_path):
99+
project = tmp_path / "proj"
100+
other = tmp_path / "other"
101+
out = project / "graphify-out"
102+
out.mkdir(parents=True)
103+
other.mkdir()
104+
outside = (tmp_path / "elsewhere" / "x.py").as_posix()
105+
in_other = (other / "y.py").as_posix()
106+
for watch_root, cases in (
107+
(project, [
108+
("src/a.py", "src/a.py"),
109+
("./src/../src/a.py", "src/a.py"),
110+
("lib\\c.py", "lib/c.py"),
111+
(outside, outside),
112+
("", ""),
113+
(None, None),
114+
]),
115+
# A watched root outside the project root keeps its absolute path.
116+
(other, [(in_other, in_other), ("src/a.py", "src/a.py")]),
117+
):
118+
paths = _StoredSourcePaths(
119+
{"nodes": [], "links": []},
120+
out=out,
121+
project_root=project,
122+
watch_root=watch_root,
123+
normalize_source=_norm_source_file,
124+
)
125+
for _ in range(2): # first call fills the cache, second reads it
126+
for stored, expected in cases:
127+
item = {"source_file": stored}
128+
paths.rebase_preserved(item)
129+
assert item["source_file"] == expected, (watch_root, stored)
130+
assert paths.absolute_identity(stored, project) == paths._absolute_identity_uncached(stored, project)

0 commit comments

Comments
 (0)