Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

refactor(health): move the post-walk performance passes into CrossFunctionPerfStage (#4011)

Raghav Chamadiya51894ea5c035
diff --git a/packages/core/src/repowise/core/analysis/health/crossfn_perf.py b/packages/core/src/repowise/core/analysis/health/crossfn_perf.py
new file mode 100644
index 000000000..003ab4bcb
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/health/crossfn_perf.py
@@ -0,0 +1,91 @@
+"""The performance passes that run after every file is walked and before any is scored."""
+
+from __future__ import annotations
+
+from collections.abc import Callable
+from typing import Any
+
+import structlog
+
+from ...code_origin import CodeOrigin
+from .complexity import FileComplexity
+from .dataflow import FileDataflowCache
+from .perf import (
+ CallGraphIndex,
+ PerfRanker,
+ apply_perf_promotions,
+ collect_blocking_io_under_lock,
+ collect_centrality_gated,
+ collect_crossfn_io_in_loop,
+)
+from .perf.lazy_load import collect_lazy_loads
+from .perf.unbounded_reduction import collect_unbounded_reductions
+from .source_reader import SourceReader
+
+log = structlog.get_logger(__name__)
+
+
+class CrossFunctionPerfStage:
+ """Adds the graph- and repo-dependent ``perf_hits`` to the walked files, in place.
+
+ *origins* is the dict the walk fills, read when the stage runs.
+ """
+
+ def __init__(
+ self,
+ graph: Any,
+ execution_graph: Callable[[], CallGraphIndex | None],
+ origins: dict[str, CodeOrigin],
+ parsed_files: list[Any],
+ read_source: SourceReader,
+ ) -> None:
+ self._graph = graph
+ self._execution_graph = execution_graph
+ self._origins = origins
+ self._parsed_files = parsed_files
+ self._read_source = read_source
+
+ def run(
+ self,
+ walked: list[tuple[Any, FileComplexity]],
+ dataflow: FileDataflowCache,
+ timings: Any | None,
+ ) -> None:
+ """Every post-walk perf pass, in order: promotions read the crossfn hits."""
+ from repowise.core.pipeline.phase_timing import timed
+
+ with timed(timings, "analysis.health.crossfn"):
+ self.apply_crossfn(walked)
+ with timed(timings, "analysis.health.promotions"):
+ apply_perf_promotions(walked, dataflow=dataflow)
+ with timed(timings, "analysis.health.unbounded_reduction"):
+ collect_unbounded_reductions(walked, read_source=self._read_source)
+ with timed(timings, "analysis.health.lazy_load"):
+ collect_lazy_loads(walked, self._parsed_files, read_source=self._read_source)
+
+ def apply_crossfn(self, walked: list[tuple[Any, FileComplexity]]) -> None:
+ """Append cross-function and centrality-gated perf hits to each file, in place.
+
+ Failure-isolated. Without a graph the centrality-gated pass still runs and emits
+ nothing: no marker ships without established centrality.
+ """
+ try:
+ index = self._execution_graph()
+ by_file: dict[str, list] = {}
+ if self._graph is not None and index is not None:
+ for src in (
+ collect_crossfn_io_in_loop(walked, self._graph, index=index),
+ collect_blocking_io_under_lock(walked, self._graph, index=index),
+ ):
+ for path, hits in src.items():
+ by_file.setdefault(path, []).extend(hits)
+ ranker = PerfRanker(index)
+ for path, hits in collect_centrality_gated(walked, ranker, self._origins).items():
+ by_file.setdefault(path, []).extend(hits)
+ for _pf, fcx in walked:
+ extra = by_file.get(_pf.file_info.path)
+ if extra:
+ fcx.perf_hits = [*fcx.perf_hits, *extra]
+ except Exception as exc:
+ log.debug("health_crossfn_perf_failed", error=str(exc))
+ return
diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.py
index 0be8b10ea..e5b881679 100644
--- a/packages/core/src/repowise/core/analysis/health/engine.py
+++ b/packages/core/src/repowise/core/analysis/health/engine.py
@@ -51,6 +51,7 @@ from .biomarkers import FileContext, detect_all
from .biomarkers.function_hotspot import refresh_replayed
from .complexity import FileComplexity, FunctionComplexity, walk_file
from .complexity.languages import has_health_dialect
+from .crossfn_perf import CrossFunctionPerfStage
from .dataflow import FileDataflowCache
from .duplication import ClonePair, DuplicationReport
from .duplication.detector import clone_ranges, union_line_count
@@ -61,16 +62,9 @@ from .history_refresh import BLAME_MARKERS, as_biomarker_result
from .models import HealthFileMetricData, HealthFindingData, HealthReport, Severity
from .perf import (
CallGraphIndex,
- PerfRanker,
- apply_perf_promotions,
build_performance_opportunities,
- collect_blocking_io_under_lock,
- collect_centrality_gated,
- collect_crossfn_io_in_loop,
link_performance_findings,
)
-from .perf.lazy_load import collect_lazy_loads
-from .perf.unbounded_reduction import collect_unbounded_reductions
from .ranking import deduction_by_path
from .refactoring import (
PerformancePlanPolicy,
@@ -831,6 +825,9 @@ class HealthAnalyzer:
self._package_roots_cache: set[str] | None = None
self._execution_graph_cache: CallGraphIndex | None = None
self.test_index = RepoTestIndex(self.graph, self.parsed_files, self._execution_graph)
+ self.perf_stage = CrossFunctionPerfStage(
+ self.graph, self._execution_graph, self._origins, self.parsed_files, self.read_source
+ )
def file_dependents(self) -> dict[str, int]:
"""Each analysed file's dependents count, as the centrality gates read it."""
@@ -1120,7 +1117,7 @@ class HealthAnalyzer:
# and the Extract Method detector read the same lazily parsed
# per-file object, so no file is parsed twice for dataflow.
dataflow_cache = FileDataflowCache(self.read_source)
- self._augment_perf_hits(walked, dataflow_cache, timings)
+ self.perf_stage.run(walked, dataflow_cache, timings)
with timed(timings, "analysis.health.repo_stats"):
inputs = self._repo_inputs(dup_report, fn_mod_p80, dataflow_cache)
with timed(timings, "analysis.health.evaluate"):
@@ -1287,67 +1284,6 @@ class HealthAnalyzer:
log.debug("health_cross_file_oracles_failed", error=str(exc))
return
- def _augment_perf_hits(
- self,
- walked: list[tuple[Any, FileComplexity]],
- dataflow: FileDataflowCache,
- timings: Any | None,
- ) -> None:
- """Every post-walk perf pass, shared by ``analyze`` and ``analyze_async`` so
- the two paths cannot drift apart again. Promotions read the crossfn hits,
- so crossfn runs first."""
- from repowise.core.pipeline.phase_timing import timed
-
- with timed(timings, "analysis.health.crossfn"):
- self._apply_crossfn_perf(walked)
- with timed(timings, "analysis.health.promotions"):
- apply_perf_promotions(walked, dataflow=dataflow)
- with timed(timings, "analysis.health.unbounded_reduction"):
- collect_unbounded_reductions(walked, read_source=self.read_source)
- with timed(timings, "analysis.health.lazy_load"):
- collect_lazy_loads(walked, self.parsed_files, read_source=self.read_source)
-
- def _apply_crossfn_perf(self, walked: list[tuple[Any, FileComplexity]]) -> None:
- """Run the graph-dependent perf passes over the walked files, in place.
-
- Four sources of extra ``perf_hits``, all sharing one
- :class:`CallGraphIndex` (built once over reliable execution edges):
-
- 1. cross-function ``io_in_loop`` / N+1 (PR4);
- 2. cross-function ``blocking_io_under_lock`` (Phase 7b) — the lock→I/O
- reachability case;
- 3. the centrality-gated ``nested_loop_quadratic`` / ``hot_path_sync_io``
- markers (Phase 7b), generated from the walker's per-function facts
- ONLY for a hot function, via the :class:`PerfRanker`.
-
- Each is appended onto the matching file's ``perf_hits`` in place so the
- biomarkers handle every case through one path. Failure-isolated and never
- blocks the report. The cross-function passes are a no-op without a graph;
- the centrality-gated pass ALWAYS runs — without a graph nothing is
- hot, so it emits nothing (precision-first: we never ship a
- centrality-gated marker we cannot establish centrality for).
- """
- try:
- index = self._execution_graph()
- by_file: dict[str, list] = {}
- if self.graph is not None and index is not None:
- for src in (
- collect_crossfn_io_in_loop(walked, self.graph, index=index),
- collect_blocking_io_under_lock(walked, self.graph, index=index),
- ):
- for path, hits in src.items():
- by_file.setdefault(path, []).extend(hits)
- ranker = PerfRanker(index)
- for path, hits in collect_centrality_gated(walked, ranker, self._origins).items():
- by_file.setdefault(path, []).extend(hits)
- for _pf, fcx in walked:
- extra = by_file.get(_pf.file_info.path)
- if extra:
- fcx.perf_hits = [*fcx.perf_hits, *extra]
- except Exception as exc:
- log.debug("health_crossfn_perf_failed", error=str(exc))
- return
-
def _mark_perf_entry_reachability(
self, findings: list[HealthFindingData]
) -> ExecutionRoles | None:
diff --git a/packages/core/src/repowise/core/analysis/health/perf/promotion.py b/packages/core/src/repowise/core/analysis/health/perf/promotion.py
index 784ce20be..e9d18124d 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/promotion.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/promotion.py
@@ -78,7 +78,7 @@ def apply_perf_promotions(
) -> None:
"""Mark provably-independent advisory perf hits ``promoted``, in place.
- Mirrors ``engine._apply_crossfn_perf``: it mutates each file's ``perf_hits``
+ Mirrors ``CrossFunctionPerfStage.apply_crossfn``: it mutates each file's ``perf_hits``
in place and is fully failure-isolated, so a promotion hiccup never blocks
the health report. Must run AFTER the graph-dependent perf passes so the
centrality-gated ``nested_loop_quadratic`` hits are present.
diff --git a/tests/unit/health/test_reachability_wiring.py b/tests/unit/health/test_reachability_wiring.py
index f592ce2f9..89675fe95 100644
--- a/tests/unit/health/test_reachability_wiring.py
+++ b/tests/unit/health/test_reachability_wiring.py
@@ -138,7 +138,7 @@ def test_test_and_performance_passes_share_one_execution_index(tmp_path):
first = analyzer._execution_graph()
analyzer.test_index.reached_files()
- analyzer._apply_crossfn_perf([])
+ analyzer.perf_stage.apply_crossfn([])
assert first is not None
assert analyzer._execution_graph() is first