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): a call in a for loop's iterable belongs to the loop around it (#3034)

Raghav Chamadiya55089126819c
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 5ab12a542..9e44896c8 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
@@ -256,14 +256,20 @@ def _enclosing_loops(
node: Node, dialect: BasePerfDialect, loop_kinds: frozenset[str], fn_kinds: frozenset[str]
) -> list[Node]:
"""The data-dependent loops around *node* in its function, innermost first;
- walked up only for hits."""
+ walked up only for hits. A loop whose iterable holds *node* is not one."""
loops: list[Node] = []
cur = node.parent
for _ in range(64):
if cur is None or cur.type in fn_kinds:
break
if cur.type in loop_kinds and cur.is_named and not dialect.is_constant_loop(cur):
- loops.append(cur)
+ iterable = dialect.iterable_node(cur)
+ # ``for row in result.all():`` runs ``.all()`` once per pass of the loop
+ # around it, so the for loop itself is not what repeats it.
+ if iterable is None or not (
+ iterable.start_byte <= node.start_byte and node.end_byte <= iterable.end_byte
+ ):
+ loops.append(cur)
cur = cur.parent
return loops
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py
index 90874141a..71b62e11f 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py
@@ -318,6 +318,15 @@ class BasePerfDialect:
"""True when this loop walks its data a chunk at a time (already batched)."""
return False
+ def iterable_node(self, node: Node) -> Node | None:
+ """The expression a for-each loop evaluates once before its first pass, or
+ ``None`` (the default, and any loop whose header runs every pass).
+
+ A call inside it runs once per *enclosing* loop pass, not once per pass of
+ this loop, so loop facts attribute it to the loop around this one.
+ """
+ return None
+
@staticmethod
def _steps_by_chunk(update: Node | None) -> bool:
"""``i += 100`` / ``i += BATCH``: a counter that advances more than one at a time."""
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py
index f1a821a40..d43a601fc 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/python.py
@@ -467,6 +467,9 @@ class PythonPerfDialect(BasePerfDialect):
return first.text.decode("utf-8", "replace")
return None
+ def iterable_node(self, node: Node) -> Node | None:
+ return node.child_by_field_name("right") if node.type == "for_statement" else None
+
# ``itertools.batched``, ``more_itertools.chunked`` and hand-rolled peers.
_CHUNKING_CALLS: frozenset[str] = frozenset(
{"batched", "chunked", "ichunked", "chunks", "iter_chunks", "grouper"}
diff --git a/tests/unit/health/test_perf_io_in_loop.py b/tests/unit/health/test_perf_io_in_loop.py
index a6dc9b004..f126a9585 100644
--- a/tests/unit/health/test_perf_io_in_loop.py
+++ b/tests/unit/health/test_perf_io_in_loop.py
@@ -531,3 +531,60 @@ def test_a_hit_is_named_for_its_enclosing_function(source: str, expected: str |
if not hits:
pytest.skip("typescript grammar unavailable")
assert {h.function for h in hits} == {expected}
+
+
+_BATCHED_CLEAR = (
+ b"from sqlalchemy import select\n"
+ b"async def clear(session, repository_id, paths):\n"
+ b" for i in range(0, len(paths), _BATCH_SIZE):\n"
+ b" rows = await session.execute(\n"
+ b" select(Row).where(Row.path.in_(paths[i : i + _BATCH_SIZE]))\n"
+ b" )\n"
+ b" for row in rows.scalars().all():\n"
+ b" row.score = None\n"
+)
+
+_N_PLUS_ONE = (
+ b"from sqlalchemy import select\n"
+ b"async def clear(session, repository_id, paths):\n"
+ b" for path in paths:\n"
+ b" rows = await session.execute(select(Row).where(Row.path == path))\n"
+ b" for row in rows.scalars().all():\n"
+ b" row.score = None\n"
+)
+
+
+def _loop_hits(source: bytes):
+ fc = walk_file("f.py", "python", source)
+ return [h for h in fc.perf_hits if h.kind == "io_in_loop"]
+
+
+def test_reading_a_result_inside_a_chunked_loop_is_already_batched():
+ """The chunk loop steps by a batch size and queries ``.in_`` over one slice; the
+ inner ``for row in rows.scalars().all()`` runs once per chunk, not once per row."""
+ hits = _loop_hits(_BATCHED_CLEAR)
+ assert {h.line for h in hits} == {4, 7}
+ assert all(h.loop is not None and h.loop.chunked for h in hits)
+ findings = IoInLoopDetector().detect(_ctx(hits))
+ assert findings and all(f.details.get("chunked_iteration") for f in findings)
+
+
+def test_a_per_item_query_still_reports_as_not_batched():
+ hits = _loop_hits(_N_PLUS_ONE)
+ assert {h.line for h in hits} == {4, 5}
+ assert not any(h.loop is not None and h.loop.chunked for h in hits)
+ findings = IoInLoopDetector().detect(_ctx(hits))
+ assert findings and not any(f.details.get("chunked_iteration") for f in findings)
+
+
+def test_a_query_in_the_header_of_an_inner_loop_belongs_to_the_outer_loop():
+ source = (
+ b"from sqlalchemy import select\n"
+ b"async def f(session, users):\n"
+ b" for u in users:\n"
+ b" for r in (await session.execute(select(T).where(T.id == u.id))).scalars().all():\n"
+ b" r.x = 1\n"
+ )
+ hits = _loop_hits(source)
+ assert hits and all(h.loop_line == 3 for h in hits)
+ assert not any(h.loop is not None and h.loop.chunked for h in hits)