Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
perf(graph): release the symbol subgraph copy once its two kernels have scored it (#2869)
Raghav Chamadiyad372a6f6c46a
diff --git a/packages/core/src/repowise/core/ingestion/graph/_metrics.py b/packages/core/src/repowise/core/ingestion/graph/_metrics.pyindex 527b9fd7f..56ac383bb 100644--- a/packages/core/src/repowise/core/ingestion/graph/_metrics.py+++ b/packages/core/src/repowise/core/ingestion/graph/_metrics.py@@ -358,6 +358,7 @@ class MetricsMixin:log.warning("Symbol PageRank did not converge, using uniform scores")n = sub.number_of_nodes()self._symbol_pagerank_cache = {node: 1.0 / n for node in sub.nodes()}+ self._release_symbol_subgraph()return self._symbol_pagerank_cachedef symbol_betweenness_centrality(self) -> dict[str, float]:@@ -369,8 +370,20 @@ class MetricsMixin:self._symbol_betweenness_cache = {}return self._symbol_betweenness_cacheself._symbol_betweenness_cache = self._betweenness_with_disk_cache("symbol", sub)+ self._release_symbol_subgraph()return self._symbol_betweenness_cache+ def _release_symbol_subgraph(self) -> None:+ """Drop the cached symbol subgraph once both symbol kernels have scored it.++ It is a full copy of every symbol node and call edge, and these two+ kernels are its only readers; kept, it sat beside the graph for the+ rest of the run. A later caller gets it rebuilt on demand.+ """+ if self._symbol_pagerank_cache is not None and self._symbol_betweenness_cache is not None:+ with self._subgraph_lock:+ self._symbol_subgraph_cache = None+def betweenness_scoring(self, kind: str) -> Any | None:"""Return the provenance of *kind*'s betweenness, or ``None`` if unscored.diff --git a/tests/unit/ingestion/test_subgraph_cache.py b/tests/unit/ingestion/test_subgraph_cache.pyindex a9ff4a110..96b3c4b5c 100644--- a/tests/unit/ingestion/test_subgraph_cache.py+++ b/tests/unit/ingestion/test_subgraph_cache.py@@ -53,6 +53,16 @@ class TestSubgraphCache:gb = _builder(tmp_path)assert gb.symbol_subgraph() is gb.symbol_subgraph()+ def test_symbol_subgraph_released_once_both_kernels_scored(self, tmp_path: Path) -> None:+ gb = _builder(tmp_path)+ gb.symbol_pagerank()+ # Betweenness has not read it yet, so the copy must survive.+ assert gb._symbol_subgraph_cache is not None+ gb.symbol_betweenness_centrality()+ assert gb._symbol_subgraph_cache is None+ # A later reader still gets the same subgraph, rebuilt on demand.+ assert set(gb.symbol_subgraph().nodes) == set(gb.symbol_pagerank())+def test_contents_unchanged_by_caching(self, tmp_path: Path) -> None:gb = _builder(tmp_path)sub = gb.file_subgraph()