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.

fix(perf): offer a batch plan only when the loop's key reaches the call (#2923)

Raghav Chamadiya26bcf1b08ca0
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py b/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py
index 124fa9fd6..6c5f4ba93 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py
@@ -243,6 +243,63 @@ def _enclosing_loops(
return loops
+# Loop-header fields that bind the element a for-each loop yields, across the
+# grammars (``left`` Python/JS/C#, ``name`` Java, ``pattern`` Rust/Ruby,
+# ``declarator`` C++ range-for), and the init fields of a counted loop, which
+# bind its index only when they declare (``for (int i = 0; ...)``, not
+# ``for (p = head; ...)``, which walks a list it was handed).
+_ELEMENT_FIELDS = ("left", "name", "pattern", "declarator")
+_INIT_FIELDS = ("initializer", "init")
+# Go keeps both shapes in a child clause; Kotlin declares the element unfielded.
+_LOOP_CLAUSE_KINDS = frozenset({"range_clause", "for_clause"})
+
+
+def _identifiers(node: Node) -> set[bytes]:
+ """Every identifier under *node*, type names excluded."""
+ names: set[bytes] = set()
+ stack = [node]
+ while stack:
+ cur = stack.pop()
+ if cur.type.endswith("identifier") and cur.type != "type_identifier" and cur.text:
+ names.add(cur.text)
+ stack.extend(cur.children)
+ return names
+
+
+def _bound_in(part: Node) -> set[bytes]:
+ """Names one loop-header part binds through an element field or a declaring init field."""
+ nodes = [part.child_by_field_name(field) for field in _ELEMENT_FIELDS]
+ nodes += [
+ node
+ for field in _INIT_FIELDS
+ if (node := part.child_by_field_name(field)) is not None and "declaration" in node.type
+ ]
+ return set().union(*(_identifiers(node) for node in nodes if node is not None))
+
+
+def _loop_bound_names(loop: Node) -> set[bytes]:
+ """The element or index names *loop*'s header binds; empty for a ``while`` loop."""
+ parts = [loop, *(c for c in loop.children if c.type in _LOOP_CLAUSE_KINDS)]
+ unfielded = [c for c in loop.children if c.type == "variable_declaration"]
+ return set().union(*(_bound_in(p) for p in parts), *(_identifiers(c) for c in unfielded))
+
+
+def _loop_key_feeds(call: Node, loop: Node) -> bool:
+ """The loop's element or index appears in *call*, which makes the call per-key.
+
+ A retry or fallback loop (``while (true)``, ``while (tries < max)``), a
+ partial-write loop and a walk down a list handed in bind no element, so
+ nothing per-key reaches the call and there is no set of keys to batch. A key
+ that reaches the call only through a derived local is not counted (recall
+ ceiling).
+ """
+ return bool(_loop_bound_names(loop) & _identifiers(call))
+
+
+def _key_unused(hit: PerfHit) -> bool:
+ return hit.loop is not None and hit.loop.key_unused
+
+
def _overrides(dialect: BasePerfDialect, hook: str) -> bool:
return getattr(type(dialect), hook) is not getattr(BasePerfDialectClass, hook)
@@ -361,9 +418,13 @@ def _collect_perf_hits(
return "grows_with_data"
return "bounded" if found == {"bounded"} else "unknown"
- def loop_facts(node: Node, sink: bool) -> LoopFacts | None:
- """Facts of *node*'s innermost loop; batch and bound only mean something at a sink."""
- if not do_loop_facts:
+ def loop_facts(node: Node, sink: bool, per_call: bool = False) -> LoopFacts | None:
+ """Facts of *node*'s innermost loop; batch and bound only mean something at a sink.
+
+ *per_call* marks the call each iteration makes, a sink or a helper that
+ reaches one: only there does it matter whether the loop's key feeds it.
+ """
+ if not (do_loop_facts or per_call):
return None
loops = _enclosing_loops(node, dialect, loop_kinds, fn_kinds)
if not loops:
@@ -374,6 +435,7 @@ def _collect_perf_hits(
magnitude=magnitude(loops) if do_magnitude else "unknown",
batch=dialect.batch_form(node, loop, probe) if sink and do_batch else None,
concurrency_bound=dialect.concurrency_bound(node, loop) if sink and do_bound else None,
+ key_unused=per_call and not _loop_key_feeds(node, loop),
)
return facts if facts != LoopFacts() else None
@@ -531,7 +593,7 @@ def _collect_perf_hits(
)
if kind is not None:
if loop_depth >= 1:
- facts = loop_facts(call_node, sink=True)
+ facts = loop_facts(call_node, sink=True, per_call=True)
hits.append(
PerfHit("io_in_loop", line, next_func, kind, func_start=next_start, loop=facts)
)
@@ -617,7 +679,7 @@ def _collect_perf_hits(
targets[method] = line
if loop_line:
call_loop_lines.setdefault(next_start, {})[line] = loop_line
- facts = loop_facts(call_node, sink=False)
+ facts = loop_facts(call_node, sink=False, per_call=True)
if facts is not None:
call_facts.setdefault(next_start, {})[line] = facts
if do_lock_io and lock_depth >= 1 and method:
@@ -760,15 +822,18 @@ def _collect_perf_hits(
# Dedup chained sinks: ``result.scalars().all()`` parses as two call nodes
# on one line (the ``.scalars()`` sink and the ``.all()`` materializer) —
- # one logical query, one finding. Collapse per (kind, line, function).
- seen: set[tuple[str, int, str | None]] = set()
+ # one logical query, one finding. Collapse per (kind, line, function),
+ # keeping a call the loop's key reaches when one of them does.
+ kept: dict[tuple[str, int, str | None], int] = {}
deduped: list[PerfHit] = []
for h in _name_lambda_hits(hits, lambda_spans):
key = (h.kind, h.line, h.function)
- if key in seen:
- continue
- seen.add(key)
- deduped.append(h)
+ at = kept.get(key)
+ if at is None:
+ kept[key] = len(deduped)
+ deduped.append(h)
+ elif _key_unused(deduped[at]) and not _key_unused(h):
+ deduped[at] = h
deduped.sort(key=lambda h: (h.line, h.kind))
fn_facts = [
diff --git a/packages/core/src/repowise/core/analysis/health/perf/actionability.py b/packages/core/src/repowise/core/analysis/health/perf/actionability.py
index 8aea1229a..79b6f15d0 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/actionability.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/actionability.py
@@ -247,6 +247,10 @@ def assess_fix(
),
("result_equivalence",),
)
+ if details and all(detail.get("loop_key_unused") for detail in details):
+ # No element or index of the loop reaches the call: a retry,
+ # fallback or partial-write loop, with no set of keys to batch.
+ return FixAssessment(None, ("per_key_call",))
return FixAssessment(
PerformanceFix(
"batch_or_prefetch_io",
diff --git a/packages/core/src/repowise/core/analysis/health/perf/loop_facts.py b/packages/core/src/repowise/core/analysis/health/perf/loop_facts.py
index 97bcc0336..70771959b 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/loop_facts.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/loop_facts.py
@@ -41,6 +41,8 @@ class LoopFacts:
magnitude: LoopMagnitude = "unknown"
batch: BatchForm | None = None
concurrency_bound: str | None = None
+ # No element or index the loop binds reaches the sink call: nothing per-key to batch.
+ key_unused: bool = False
def as_details(self) -> dict[str, Any]:
"""Finding ``details`` keys; defaults are omitted so old findings are unchanged."""
@@ -54,6 +56,8 @@ class LoopFacts:
details["batch_equivalent"] = self.batch.equivalent
if self.concurrency_bound:
details["concurrency_bound"] = self.concurrency_bound
+ if self.key_unused:
+ details["loop_key_unused"] = True
return details
diff --git a/tests/unit/health/test_perf_loop_facts_python.py b/tests/unit/health/test_perf_loop_facts_python.py
index fd84a4db6..1d26bd529 100644
--- a/tests/unit/health/test_perf_loop_facts_python.py
+++ b/tests/unit/health/test_perf_loop_facts_python.py
@@ -277,7 +277,8 @@ def test_batch_none_for_while_loop():
b" await client.table('t').select('*').eq('repo_id', r.id).execute()\n"
b" r = repos.pop() if repos else None\n"
)
- assert facts is None
+ # A cursor loop binds no element, so nothing per-key reaches the call.
+ assert facts is not None and facts.batch is None and facts.key_unused
# ---------------------------------------------------------------------------
diff --git a/tests/unit/health/test_perf_loop_key.py b/tests/unit/health/test_perf_loop_key.py
new file mode 100644
index 000000000..3e4c706c4
--- /dev/null
+++ b/tests/unit/health/test_perf_loop_key.py
@@ -0,0 +1,85 @@
+"""A batch plan needs the loop's own element or index to reach the per-iteration call.
+
+Retry, fallback and partial-write loops repeat one call without a set of keys, so
+"take every key at once" has nothing to take. The walker records that as
+``loop_key_unused`` and the fix assessment declines the batch plan.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.core.analysis.health.complexity import walk_file
+from repowise.core.analysis.health.models import HealthFindingData, Severity
+from repowise.core.analysis.health.perf.opportunities import build_performance_opportunities
+
+
+def _io_details(lang: str, path: str, src: str) -> list[dict]:
+ fc = walk_file(path, lang, src.encode())
+ return [h.loop_facts() for h in fc.perf_hits if h.kind == "io_in_loop"]
+
+
+ ("lang", "path", "src", "unused"),
+ [
+ (
+ "csharp",
+ "a.cs",
+ "class A{ async System.Threading.Tasks.Task M(System.Net.Http.HttpClient c,"
+ " System.Collections.Generic.List<string> urls){"
+ " foreach (var url in urls) { await c.GetStringAsync(url); } } }",
+ False,
+ ),
+ (
+ "csharp",
+ "a.cs",
+ "class A{ async System.Threading.Tasks.Task M(System.Net.Http.HttpClient c, string u){"
+ " while (true) { var r = await c.GetStringAsync(u); if (r != null) break; } } }",
+ True,
+ ),
+ (
+ "javascript",
+ "a.js",
+ "async function f(ids) { for (let i = 0; i < ids.length; i++) {"
+ " await fetch(ids[i]); } }",
+ False,
+ ),
+ (
+ "javascript",
+ "a.js",
+ "async function f(base) { let page = 1; while (true) {"
+ " const r = await fetch(base + page); if (!r.ok) break; page++; } }",
+ True,
+ ),
+ ],
+ ids=["foreach element", "retry loop", "index into keys", "pagination loop"],
+)
+def test_walker_records_whether_the_loop_key_reaches_the_call(lang, path, src, unused):
+ (details,) = _io_details(lang, path, src)
+ assert bool(details.get("loop_key_unused")) is unused
+
+
+def _finding(line: int, **details: object) -> HealthFindingData:
+ return HealthFindingData(
+ biomarker_type="io_in_loop",
+ severity=Severity.MEDIUM,
+ file_path="client.py",
+ function_name="run",
+ line_start=line,
+ line_end=line,
+ details={"boundary_kind": "network", "cross_function": False, **details},
+ health_impact=0.0,
+ dimension="performance",
+ )
+
+
+def test_a_loop_whose_key_never_reaches_the_call_gets_no_batch_plan():
+ (opportunity,) = build_performance_opportunities([_finding(3, loop_key_unused=True)])
+ assert opportunity.fix is None
+ assert opportunity.prerequisites == ("per_key_call",)
+
+
+def test_a_keyed_loop_keeps_the_advisory_batch_plan():
+ (opportunity,) = build_performance_opportunities([_finding(3)])
+ assert opportunity.fix is not None
+ assert opportunity.fix.strategy == "batch_or_prefetch_io"