The #3252 repoint step in _resolve_cross_file_imports scanned every
edge in the corpus once per Python file with import targets, so the
pass cost files x edges.
Index the edges by source_file once, the first time a file needs it,
and visit only the importing file's edges. Each index entry keeps the
edge's position in all_edges, so a file's edges are visited in the
same order as before. The loop only rewrites edge["target"], so the
index stays valid while it runs.
On a corpus with 4,115 Python files and 360,996 edges, graphify update
went from 3:36 to 2:55 with an unchanged graph topology.
The new test counts full passes over all_edges: 10 before the fix
(8 importing files + 2), at most 3 after.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Fjj5WaN6sDPLQ2gWGnkGob
(cherry picked from commit c35850fa83)
This commit is contained in:
1 parent
f9fbd6daf0
commit
a328198f84
2 files changed
+67
-3
No files matched your search
@@ -3045,6 +3045,13 @@ def _resolve_cross_file_imports(
|
||||
node_by_id = {node["id"]: node for node in all_nodes if node.get("id")} if all_nodes else {}
|
||||
repointed_from: set[str] = set()
|
||||
TYPE_REPOINT_RELATIONS = frozenset({"references", "inherits", "implements", "extends"})
|
||||
# The #3252 repoint below only ever touches an edge whose source_file belongs
|
||||
# to the importing file. Index the edges by source_file once, when the first
|
||||
# file needs it, instead of scanning all of them per file (files x edges,
|
||||
# #4980). Each entry keeps the edge's position in all_edges, so a file's
|
||||
# edges are visited in the same order as before.
|
||||
edges_by_source: dict[object, list[tuple[int, dict]]] = {}
|
||||
edges_indexed = False
|
||||
|
||||
for file_result, path in zip(per_file, paths):
|
||||
str_path = str(path)
|
||||
@@ -3222,9 +3229,15 @@ def _resolve_cross_file_imports(
|
||||
# Repoint AST type-reference and inheritance edges from sourceless stubs
|
||||
# to the exact imported target definition (#3252).
|
||||
if all_edges and import_targets:
|
||||
for edge in all_edges:
|
||||
if edge.get("source_file") not in file_srcs:
|
||||
continue
|
||||
if not edges_indexed:
|
||||
for position, edge in enumerate(all_edges):
|
||||
edges_by_source.setdefault(edge.get("source_file"), []).append((position, edge))
|
||||
edges_indexed = True
|
||||
file_edges = sorted(
|
||||
(entry for src in file_srcs for entry in edges_by_source.get(src, ())),
|
||||
key=lambda entry: entry[0],
|
||||
)
|
||||
for _, edge in file_edges:
|
||||
if edge.get("relation") not in TYPE_REPOINT_RELATIONS:
|
||||
continue
|
||||
tgt = edge.get("target")
|
||||
|
||||
@@ -6313,6 +6313,57 @@ def test_3252_metadata_preservation(tmp_path):
|
||||
assert node_by_id[param_ref["target"]]["source_file"] == "models.py"
|
||||
|
||||
|
||||
def test_3252_repoint_visits_only_the_importing_files_edges(tmp_path, monkeypatch):
|
||||
"""#4980: the #3252 repoint pass must not scan every edge once per file.
|
||||
|
||||
It used to loop over all of ``all_edges`` for each Python file with import
|
||||
targets, so the pass cost files x edges. Counting full passes over
|
||||
``all_edges`` shows the count no longer grows with the number of importing
|
||||
files, and every consumer's reference is still repointed.
|
||||
"""
|
||||
import graphify.extract as extract_mod
|
||||
|
||||
(tmp_path / "models.py").write_text("class User:\n pass\n", encoding="utf-8")
|
||||
consumers = [tmp_path / f"service_{i}.py" for i in range(8)]
|
||||
for i, path in enumerate(consumers):
|
||||
path.write_text(
|
||||
f"from models import User\ndef get_user_{i}(u: User) -> User:\n pass\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
class _CountingEdges(list):
|
||||
full_passes = 0
|
||||
|
||||
def __iter__(self):
|
||||
self.full_passes += 1
|
||||
return super().__iter__()
|
||||
|
||||
passes: list[int] = []
|
||||
real_resolve = extract_mod._resolve_cross_file_imports
|
||||
|
||||
def counting_resolve(per_file, paths, all_nodes=None, all_edges=None, **kwargs):
|
||||
edges = _CountingEdges(all_edges or [])
|
||||
result = real_resolve(per_file, paths, all_nodes, edges, **kwargs)
|
||||
passes.append(edges.full_passes)
|
||||
return result
|
||||
|
||||
monkeypatch.setattr(extract_mod, "_resolve_cross_file_imports", counting_resolve)
|
||||
res = extract([tmp_path / "models.py", *consumers], root=tmp_path, cache_root=tmp_path)
|
||||
|
||||
assert passes, "cross-file import resolution did not run"
|
||||
# One pass builds the per-file index; two more collect the still-referenced ids.
|
||||
# The old code added one full pass per importing file (8 here).
|
||||
assert max(passes) <= 3, passes
|
||||
|
||||
node_by_id = {n["id"]: n for n in res["nodes"]}
|
||||
param_refs = [
|
||||
e for e in res["edges"]
|
||||
if e.get("relation") == "references" and e.get("context") == "parameter_type"
|
||||
]
|
||||
assert len(param_refs) == len(consumers)
|
||||
assert all(node_by_id[e["target"]]["source_file"] == "models.py" for e in param_refs)
|
||||
|
||||
|
||||
def test_python_external_calls_survive_real_incremental_context(tmp_path):
|
||||
"""A changed caller shares an external stub with an unchanged caller."""
|
||||
import subprocess
|
||||
|
||||
Reference in new issue
Block a user