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
feat(perf): read session.get as a query, and prove fewer batch plans than before (#2513)
Raghav Chamadiya2076c69832ae
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/models.py b/packages/core/src/repowise/core/analysis/health/complexity/models.pyindex 59ceb5dea..db67aca44 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/models.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/models.py@@ -323,9 +323,9 @@ class PerfFnFacts:nested_loop_line: int = 0blocking_sink_kind: str | None = Noneblocking_sink_line: int = 0- # ``(call_line, magnitude)`` for loop-nested calls whose loop magnitude is known,- # so a cross-function hit reports the trip count of the loop that pays it.- loop_call_magnitudes: tuple[tuple[int, str], ...] = ()+ # ``(call_line, facts)`` for loop-nested calls whose loop settles a fact, so a+ # cross-function hit reports the trip count and chunking of the loop that pays it.+ loop_call_facts: tuple[tuple[int, LoopFacts], ...] = ()@dataclassdiff --git a/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py b/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.pyindex 6059807e3..3c7cede51 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@@ -94,23 +94,6 @@ _HOT_PATH_SINK_KINDS = frozenset({"subprocess", "filesystem"})# (``synchronized(repo.find(id)){…}``) runs BEFORE the lock is taken._LOCK_BODY_KINDS = frozenset({"block", "statement_block", "compound_statement", "do_block"})-# Non-semantic wrapper nodes tree-sitter inserts between a ``call`` and its-# enclosing ``await`` — parenthesising an awaited call (``await (foo())``) adds a-# ``parenthesized_expression`` hop, so the immediate-parent ``await`` check would-# miss it and wrongly read the call as un-awaited. Walk up through these before-# testing for ``await``.-_AWAIT_WRAPPER_KINDS = frozenset({"parenthesized_expression"})---def _is_awaited(node: Node) -> bool:- """Whether ``node`` is (transitively, through parenthesising wrappers) the- operand of an ``await``. Mirrors the old immediate-parent substring test but- first skips non-semantic wrappers so ``await (foo())`` reads as awaited."""- parent = node.parent- while parent is not None and parent.type in _AWAIT_WRAPPER_KINDS:- parent = parent.parent- return parent is not None and "await" in parent.type-def _perf_func_name(node: Node) -> str | None:if node.type == "function_body":@@ -259,14 +242,11 @@ def _collect_perf_hits(do_bound = _overrides(dialect, "concurrency_bound")do_loop_facts = do_chunked or do_magnitude or do_batch or do_bound+ do_in_place = _overrides(dialect, "runs_in_place")+def probe(call: Node) -> str | None:- return dialect.sink_kind(- dialect.callee_root_name(call) or "",- dialect.callee_method_name(call) or "",- awaited=_is_awaited(call),- is_attribute=dialect.callee_is_attribute(call),- io_names=io_names,- has_db_import=has_db_import,+ return dialect.call_sink_kind(+ call, awaited=dialect.is_awaited(call), io_names=io_names, has_db_import=has_db_import)def magnitude(loops: list[Node]) -> str:@@ -307,8 +287,8 @@ def _collect_perf_hits(fn_acc: dict[int, tuple[str | None, dict[str, int], dict[str, int], list[str | None], list]] = {}- # func_start -> {call_line: magnitude} for the loop-call targets above.- call_magnitudes: dict[int, dict[int, str]] = {}+ # func_start -> {call_line: facts} for the loop-call targets above.+ call_facts: dict[int, dict[int, LoopFacts]] = {}def _acc(func_start: int, func_name: str | None@@ -358,9 +338,11 @@ def _collect_perf_hits(# inline invocation, so those are cleared too — accepted (favouring# precision), and the Go IIFE case is the idiomatic defer-in-loop fix.# …except the lambda a block-iteration combinator passes as its body,- # which runs per element rather than later (Kotlin ``ids.forEach { … }``).- body_scope = (- entering_fn and do_block_loop and _is_block_loop_body_scope(node, dialect, call_kinds)+ # which runs per element rather than later (Kotlin ``ids.forEach { … }``),+ # and a closure the dialect proves runs in place (an awaited limiter call).+ body_scope = entering_fn and (+ (do_block_loop and _is_block_loop_body_scope(node, dialect, call_kinds))+ or (do_in_place and dialect.runs_in_place(node)))next_loop_depth = loop_depth if (body_scope or not entering_fn) else 0next_lock_depth = lock_depth if (body_scope or not entering_fn) else 0@@ -401,7 +383,7 @@ def _collect_perf_hits(if t in call_kinds:method = dialect.callee_method_name(node) or ""root_name = dialect.callee_root_name(node) or ""- awaited = _is_awaited(node)+ awaited = dialect.is_awaited(node)line = node.start_point[0] + 1if do_bare_call_marker:# A call that is its own iteration construct (``.reduce`` with an@@ -409,13 +391,8 @@ def _collect_perf_hits(bare = dialect.bare_call_marker(root_name, method, node)if bare is not None:hits.append(PerfHit(bare, line, next_func, "", func_start=next_start))- kind = dialect.sink_kind(- root_name,- method,- awaited=awaited,- is_attribute=dialect.callee_is_attribute(node),- io_names=io_names,- has_db_import=has_db_import,+ kind = dialect.call_sink_kind(+ node, awaited=awaited, io_names=io_names, has_db_import=has_db_import)if kind is not None:if loop_depth >= 1:@@ -503,12 +480,9 @@ def _collect_perf_hits(targets = _acc(next_start, next_func)[0]if method not in targets:targets[method] = line- if do_magnitude:- facts = loop_facts(node, sink=False)- if facts is not None and facts.magnitude != "unknown":- call_magnitudes.setdefault(next_start, {})[line] = (- facts.magnitude- )+ facts = loop_facts(node, sink=False)+ if facts is not None:+ call_facts.setdefault(next_start, {})[line] = factsif do_lock_io and lock_depth >= 1 and method:# A non-sink call under a held lock: a candidate entry for the# cross-function ``blocking_io_under_lock`` reachability pass.@@ -644,7 +618,7 @@ def _collect_perf_hits(nested_loop_line=misc[0],blocking_sink_kind=misc[1],blocking_sink_line=misc[2],- loop_call_magnitudes=tuple(sorted(call_magnitudes.get(start, {}).items())),+ loop_call_facts=tuple(sorted(call_facts.get(start, {}).items())),)for start, (name, loop_targets, lock_targets, sink, misc) in fn_acc.items()if (loop_targets or lock_targets or sink[0] is not None or misc[0] or misc[1] is not None)diff --git a/packages/core/src/repowise/core/analysis/health/engine.py b/packages/core/src/repowise/core/analysis/health/engine.pyindex 99bfeb9b4..701fce51f 100644--- a/packages/core/src/repowise/core/analysis/health/engine.py+++ b/packages/core/src/repowise/core/analysis/health/engine.py@@ -111,6 +111,9 @@ log = structlog.get_logger(__name__)# and a declining call's arguments are not scanned, so an assertion passed as an# argument still does not stand in for the header's oracle.#+# v24: ``session.get(Model, key)`` is a db sink, an awaited TS limiter closure runs+# in its loop, and Go/Java loops report chunking; a v23 store has none of them.+## v23: loop-shaped perf findings carry the loop's magnitude, the bulk form of a# per-key sink and any concurrency bound around it; a v22 store has none.#@@ -247,7 +250,7 @@ log = structlog.get_logger(__name__)# forms. Files that were counted untested and are not become tested, which# moves untested-hotspot findings and the scores that carry them, on every# language with a prefix or spec convention rather than Ruby alone.-HEALTH_ANALYZER_VERSION = 23+HEALTH_ANALYZER_VERSION = 24def walked_functions(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.pyindex 07a20c07b..3df6270b0 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@@ -176,6 +176,14 @@ class BasePerfDialect:return Falsereturn fn.type in self.attribute_callee_kinds+ @staticmethod+ def is_awaited(node: Node) -> bool:+ """*node* is the operand of an ``await``, through parentheses (``await (foo())``)."""+ parent = node.parent+ while parent is not None and parent.type == "parenthesized_expression":+ parent = parent.parent+ return parent is not None and "await" in parent.type+# -- sink classification (the lexicon) ------------------------------------def sink_kind(@@ -192,6 +200,24 @@ class BasePerfDialect:is an *execution sink*, else ``None`` ("not an I/O round-trip")."""return None+ def call_sink_kind(+ self, call: Node, *, awaited: bool, io_names: dict[str, str], has_db_import: bool+ ) -> str | None:+ """:meth:`sink_kind` of a call node; override when the arguments decide it."""+ return self.sink_kind(+ self.callee_root_name(call) or "",+ self.callee_method_name(call) or "",+ awaited=awaited,+ is_attribute=self.callee_is_attribute(call),+ io_names=io_names,+ has_db_import=has_db_import,+ )++ def runs_in_place(self, closure: Node) -> bool:+ """*closure* runs to completion where it is written, like the loop body+ around it (``await limit(() => call())``), rather than being stored for later."""+ return False+# -- loop / string / async predicates -------------------------------------def is_constant_loop(self, node: Node) -> bool:@@ -279,6 +305,25 @@ class BasePerfDialect:"""True when this loop walks its data a chunk at a time (already batched)."""return False+ @staticmethod+ def _steps_by_chunk(update: Node | None) -> bool:+ """``i += 100`` / ``i += BATCH``: a counter that advances more than one at a time."""+ if update is None or not any(c.type == "+=" for c in update.children):+ return False+ step = update.child_by_field_name("right")+ if step is not None and step.type == "expression_list": # Go wraps the right side+ step = step.named_children[0] if step.named_child_count == 1 else None+ if step is None:+ return False+ if step.type == "identifier":+ return True+ try:+ return step.type in ("number", "int_literal", "decimal_integer_literal") and float(+ (step.text or b"").decode().replace("_", "")+ ) != 1+ except ValueError:+ return False+# -- promotion facts (perf/loop_facts.py); ``None`` means "not settled" ----# The grammar's ``await <expr>`` node, and the key-hop node kinds with the@@ -292,6 +337,8 @@ class BasePerfDialect:branch_kinds: frozenset[str] = frozenset()exit_kinds: frozenset[str] = frozenset()scope_kinds: frozenset[str] = frozenset()+ # Methods that grow a sequence in call order (``append`` / ``push``).+ sequence_appends: frozenset[str] = frozenset()def loop_magnitude(self, loop: Node, probe: SinkProbe) -> LoopMagnitude | None:"""Whether the loop's iterable grows with data or has a fixed small bound."""@@ -351,6 +398,29 @@ class BasePerfDialect:cur = cur.parentreturn not any(n.type in self.exit_kinds for n in self._walk(body, prune=self.scope_kinds))+ def _exits_early(self, body: Node) -> bool:+ """An iteration can end the loop, so fanned out, the iterations it skips would run."""+ return any(+ n.type in self.exit_kinds and n.type != "continue_statement"+ for n in self._walk(body, prune=self.scope_kinds)+ )++ def _builds_in_order(self, body: Node) -> bool:+ """The body appends to a sequence or yields, so what it builds follows the+ loop's key order, which one bulk read does not keep."""+ stack = [body]+ while stack:+ node = stack.pop()+ if "yield" in node.type or (+ node.child_by_field_name("function") is not None+ and self.callee_method_name(node) in self.sequence_appends+ ):+ return True+ # A closure that runs in place is part of the body; any other one runs later.+ if node == body or node.type not in self.scope_kinds or self.runs_in_place(node):+ stack.extend(node.children)+ return False+def _only_io_in_body(self, sink: Node, body: Node, probe: SinkProbe) -> bool:"""*sink* is the loop body's only I/O sink and its only await.diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/go.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/go.pyindex c015aa72a..247838eb5 100644--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/go.py+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/go.py@@ -98,6 +98,11 @@ GO_RESOURCE_CTORS: frozenset[tuple[str, str]] = frozenset()# ``sync.Mutex`` / ``sync.RWMutex`` acquisition (the contention side only).GO_LOCK_METHODS: frozenset[str] = frozenset({"Lock", "RLock"})+# ``slices.Chunk`` (Go 1.23) and hand-rolled peers, matched on the call's+# rightmost name so a local helper (``Batch(xs, n)``) counts too.+GO_CHUNK_CALLS: frozenset[str] = frozenset(+ {"Chunk", "chunk", "Batch", "batch", "Chunks", "chunks"}+)class GoPerfDialect(BasePerfDialect):@@ -217,6 +222,21 @@ class GoPerfDialect(BasePerfDialect):return self._dotted_path(named[-1])return None+ def is_chunked_loop(self, node: Node) -> bool:+ """``for i := 0; i < n; i += step``, or ``range slices.Chunk(xs, n)``."""+ if node.type != "for_statement":+ return False+ clause = next((c for c in node.children if c.type == "for_clause"), None)+ if clause is not None:+ return self._steps_by_chunk(clause.child_by_field_name("update"))+ clause = next((c for c in node.children if c.type == "range_clause"), None)+ if clause is None:+ return False+ right = clause.child_by_field_name("right")+ if right is None or right.type != "call_expression":+ return False+ return self.callee_method_name(right) in GO_CHUNK_CALLS+@staticmethoddef _has_static_pattern_arg(node: Node) -> bool:"""True if the call's first argument is a compile-time string literal.diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/java.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/java.pyindex 712bd4ab1..f8d5feb85 100644--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/java.py+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/java.py@@ -97,6 +97,10 @@ JAVA_RESOURCE_CTORS: frozenset[str] = frozenset({"RestTemplate", "OkHttpClient"}JAVA_RESOURCE_METHODS: frozenset[str] = frozenset({"getConnection"})# ``java.util.concurrent.locks.Lock`` acquisition (the contention side only).JAVA_LOCK_METHODS: frozenset[str] = frozenset({"lock", "lockInterruptibly"})+# ``Lists.partition`` / ``ListUtils.partition`` / ``Iterables.partition`` and+# hand-rolled peers, matched on the call's method name (the receiver is not+# gated: a local helper counts too).+_PARTITION_CALLS: frozenset[str] = frozenset({"partition", "chunked", "batches"})class JavaPerfDialect(BasePerfDialect):@@ -232,5 +236,19 @@ class JavaPerfDialect(BasePerfDialect):return "lock_in_loop"return None+ def is_chunked_loop(self, node: Node) -> bool:+ """``for (int i = 0; i < n; i += step)``, or ``for (var c : Lists.partition(xs, n))``."""+ if node.type == "for_statement":+ return self._steps_by_chunk(node.child_by_field_name("update"))+ if node.type == "enhanced_for_statement":+ value = node.child_by_field_name("value")+ if value is None or value.type != "method_invocation":+ return False+ name = value.child_by_field_name("name")+ if name is None or name.text is None:+ return False+ return name.text.decode("utf-8", "replace") in _PARTITION_CALLS+ return False+DIALECT = JavaPerfDialect()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.pyindex 47bd9a101..df2964b47 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@@ -171,7 +171,6 @@ _PY_RESULT_PROJECTIONS: frozenset[str] = frozenset({"all", "scalars", "fetchall", "json", "data", "rows"})_PY_BOUND_NAME_RE = re.compile(r"(?i)retr|attempt")-_PY_MUTATE_METHODS: frozenset[str] = frozenset({"append", "extend", "add", "insert"})# A per-key call that limits or orders its rows is not the read one IN query makes._PY_PER_KEY_LIMITS: frozenset[str] = frozenset({@@ -182,9 +181,10 @@ _PY_PER_KEY_LIMITS: frozenset[str] = frozenset(_PY_ONE_ROW: frozenset[str] = _PY_PER_KEY_LIMITS | {"scalar"}_PY_READ_CAPS: frozenset[str] = _PY_ONE_ROW - {"order", "order_by", "offset"}_PY_WRITES: frozenset[str] = frozenset({"update", "insert", "upsert", "values"})-_PY_LOOP_EXITS: frozenset[str] = frozenset({"break_statement", "return_statement"})_PY_SCOPES: frozenset[str] = frozenset({"function_definition", "lambda", "class_definition"})_PY_SEM_NAME_RE = re.compile(r"(?i)(sem|semaphore|limiter|limit)$")+# A model class is CapWords; an ALL_CAPS constant (``API_URL``) is not.+_PY_MODEL_NAME_RE = re.compile(r"^[A-Z]\w*[a-z]\w*$")class PythonPerfDialect(BasePerfDialect):@@ -287,6 +287,54 @@ class PythonPerfDialect(BasePerfDialect):return "network"return None+ def call_sink_kind(+ self, call: Node, *, awaited: bool, io_names: dict[str, str], has_db_import: bool+ ) -> str | None:+ kind = super().call_sink_kind(+ call, awaited=awaited, io_names=io_names, has_db_import=has_db_import+ )+ orm_get = self._orm_get(call) if kind is None else None+ if orm_get is not None:+ # ``get`` alone is ``dict.get`` / ``requests.get``; the model-class shape+ # plus db evidence in the file is what makes it ``Session.get``.+ db = has_db_import or io_names.get(self.callee_root_name(call) or "") == "db"+ return "db" if db and not self._key_used_earlier(call, orm_get[1]) else None+ return kind++ def _key_used_earlier(self, call: Node, key: Node) -> bool:+ """An earlier call in this loop body gave the same session the same key, so the+ row may already sit in the session and ``get`` answers from memory."""+ loop = call.parent+ while loop is not None and loop.type not in ("for_statement", "while_statement"):+ if loop.type in _PY_SCOPES:+ return False+ loop = loop.parent+ body = loop.child_by_field_name("body") if loop is not None else None+ session = (self.callee_root_name(call) or "").encode()+ for node in self._walk(body, prune=_PY_SCOPES) if body is not None else ():+ args = node.child_by_field_name("arguments") if node.type == "call" else None+ if args is None or node.end_byte > call.start_byte:+ continue+ texts = {arg.text for arg in args.children}+ same_session = session in texts or self.callee_root_name(node) == session.decode()+ if key.text in texts and same_session:+ return True+ return False++ def _orm_get(self, call: Node) -> tuple[str, Node] | None:+ """``(Model, key)`` of a ``x.get(Model, key)`` primary-key lookup."""+ if self.callee_method_name(call) != "get" or not self.callee_is_attribute(call):+ return None+ args = call.child_by_field_name("arguments")+ named = [c for c in args.children if c.is_named] if args is not None else []+ if any(c.type in ("list_splat", "dictionary_splat") for c in named):+ return None+ positional = [c for c in named if c.type != "keyword_argument"]+ model = self._dotted_path(positional[0]) if len(positional) == 2 else None+ if model is None or not _PY_MODEL_NAME_RE.match(model.rsplit(".", 1)[-1]):+ return None+ return model, positional[1]+def is_constant_loop(self, node: Node) -> bool:"""True if a Python for-loop iterates a compile-time-constant bound.@@ -710,6 +758,7 @@ class PythonPerfDialect(BasePerfDialect):{"break_statement", "continue_statement", "return_statement", "raise_statement"})scope_kinds = _PY_SCOPES+ sequence_appends = frozenset({"append", "extend", "insert"})def loop_magnitude(self, loop: Node, probe: SinkProbe) -> LoopMagnitude | None:if loop.type != "for_statement":@@ -753,17 +802,37 @@ class PythonPerfDialect(BasePerfDialect):if method == "range" and not member:return self._range_magnitude(expr, loop, probe, hop=hop)if probe(expr) in ("db", "network"):- # A read capped in the query (``.limit(n)``) is as large as its cap.- capped = any(- n.type == "call" and self.callee_method_name(n) in _PY_READ_CAPS- for n in self._walk(expr)- )+ # A read capped in the query is as large as its cap; a statement or result+ # built before the loop (``stmt = ....limit(n)``) is read one assignment back.+ names = [n.text for n in self._walk(expr) if n.type == "identifier"]+ parts = [expr, *filter(None, (self._reaching_rhs(loop, name) for name in names))]+ capped = any(self._caps_read(n, loop) for part in parts for n in self._walk(part))return None if capped else "grows_with_data"if member and method in _PY_RESULT_PROJECTIONS:receiver = expr.child_by_field_name("function").child_by_field_name("object")return self._grows(self._magnitude(receiver, loop, probe, hop=hop))return None+ def _caps_read(self, node: Node, loop: Node) -> bool:+ """``.limit(n)`` and peers, or ``.in_()`` over one chunk of keys+ (``xs[i:i + N]``, or the element of a loop that walks ``xs`` in chunks)."""+ method = self.callee_method_name(node) if node.type == "call" else None+ if method in _PY_READ_CAPS:+ return True+ args = node.child_by_field_name("arguments") if method == "in_" else None+ keys = next((c for c in args.children if c.is_named), None) if args else None+ if keys is None:+ return False+ if keys.type == "subscript":+ return self._slice_magnitude(keys) == "bounded"+ outer = loop+ while outer is not None and keys.type == "identifier":+ target = outer.child_by_field_name("left") if outer.type == "for_statement" else None+ if target is not None and target.text == keys.text and self.is_chunked_loop(outer):+ return True+ outer = outer.parent+ return False+def _range_magnitude(self, call: Node, loop: Node, probe: SinkProbe, *, hop: bool) -> LoopMagnitude | None:@@ -836,7 +905,7 @@ class PythonPerfDialect(BasePerfDialect):node.type == "call"and self.callee_is_attribute(node)and self.callee_root_name(node) == iterable- and self.callee_method_name(node) in _PY_MUTATE_METHODS+ and self.callee_method_name(node) in self.sequence_appends):return None # a worklist: its keys are not known before the looprefs = [n for n in self._walk(sink) if n.type == "identifier" and n.text == target.text]@@ -846,12 +915,21 @@ class PythonPerfDialect(BasePerfDialect):call = self._keyed_filter(sink, refs[0], methods)if call is None:return None+ if "delete" in methods:+ # Batched, a delete also removes rows a skipped iteration would have left.+ kept = self._unconditional(sink, body)+ else:+ kept = not self._builds_in_order(body)+ orm_get = self._orm_get(sink)+ if orm_get is not None and orm_get[1] == refs[0]:+ # The whole element as the key may be a composite (tuple) key, which+ # ``primary_key[0].in_`` does not match; only ``r.id`` names one column.+ kept = Falseequivalent = (- not methods & _PY_PER_KEY_LIMITS+ kept+ and not methods & _PY_PER_KEY_LIMITSand not self._limited_downstream(sink, body)and self._only_io_in_body(sink, body, probe)- # Batched, a delete also removes rows a skipped iteration would have left.- and ("delete" not in methods or self._unconditional(sink, body)))return BatchForm(call, equivalent)@@ -876,6 +954,9 @@ class PythonPerfDialect(BasePerfDialect):def _keyed_filter(self, sink: Node, ref: Node, methods: set[str | None]) -> str | None:"""The bulk filter when *sink* selects or deletes by equality on *ref*."""+ orm_get = self._orm_get(sink)+ if orm_get is not None and self._is_key_of(orm_get[1], ref):+ return f"select({orm_get[0]}).where(inspect({orm_get[0]}).primary_key[0].in_(keys))"for node in self._walk(sink):if node.type == "call" and self.callee_method_name(node) == "eq":# supabase-py: ``.table(t).select(...).eq("col", key)``@@ -903,9 +984,8 @@ class PythonPerfDialect(BasePerfDialect):return Nonedef concurrency_bound(self, sink: Node, loop: Node) -> str | None:- body = self.loop_body(loop) or loop- if any(n.type in _PY_LOOP_EXITS for n in self._walk(body, prune=_PY_SCOPES)):- return None # fanned out, it would run the iterations the exit skips+ if self._exits_early(self.loop_body(loop) or loop):+ return Nonecur = sink.parentwhile cur is not None and cur != loop:if cur.type == "with_statement" and any(c.type == "async" for c in cur.children):diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.pyindex a6023803c..23eb95efd 100644--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py@@ -8,6 +8,7 @@ and the TS branches of the walker (``_has_async_modifier`` and thefrom __future__ import annotations+import refrom typing import TYPE_CHECKING, ClassVarfrom ..loop_facts import BatchForm@@ -84,6 +85,11 @@ _TS_RESOURCE_CTORS: frozenset[str] = frozenset(})+# A concurrency limiter, named by convention (``limit`` / ``this.sem``), or by the+# method async-mutex (``runExclusive``) and bottleneck (``schedule``) run work through.+_LIMITER_NAME_RE = re.compile(r"(?i)(sem|semaphore|limiter|limit|mutex)$")+_LIMITER_METHODS: frozenset[str] = frozenset({"runExclusive", "schedule"})+class TsJsPerfDialect(BasePerfDialect):language = "typescript"@@ -338,20 +344,7 @@ class TsJsPerfDialect(BasePerfDialect):``for...of`` over a ``chunk(xs, n)`` / lodash ``_.chunk(xs, n)`` call."""if node.type == "for_statement":- inc = node.child_by_field_name("increment")- if inc is None or inc.type != "augmented_assignment_expression":- return False- if not any(c.type == "+=" for c in inc.children):- return False- right = inc.child_by_field_name("right")- if right is None:- return False- if right.type == "number" and right.text is not None:- try:- return float(right.text.decode("utf-8", "replace")) != 1- except ValueError:- return False- return right.type == "identifier" # a named step is not a literal 1+ return self._steps_by_chunk(node.child_by_field_name("increment"))if node.type in self._ITERATION_LOOP_KINDS:right = node.child_by_field_name("right")if right is None or right.type != "call_expression":@@ -368,7 +361,7 @@ class TsJsPerfDialect(BasePerfDialect):_FN_KINDS: frozenset[str] = frozenset({"function_declaration", "function_expression", "arrow_function", "method_definition"})- _MUTATOR_METHODS: frozenset[str] = frozenset({"push", "unshift", "splice"})+ sequence_appends = frozenset({"push", "unshift", "splice"})await_kind = "await_expression"key_hops: ClassVar[dict[str, str]] = {"member_expression": "object",@@ -483,9 +476,49 @@ class TsJsPerfDialect(BasePerfDialect):return self._magnitude_of_expr(self._reaching_assignment(loop, right.text), probe)return magnitude- # No ``concurrency_bound``: a limiter wraps its await in a closure- # (``limit(() => call())``), and the walker does not count a closure as the- # loop body, so no hit could reach it.+ def _limiter_of(self, closure: Node) -> str | None:+ """The limiter an awaited call runs *closure* through (``await limit(() => f(x))``).++ Awaited, the closure finishes before the loop moves on, so it is the loop+ body. Stored, returned or not awaited, it runs later and is not. A curried+ ``pLimit(2)(...)`` builds a fresh limiter per call, which bounds nothing.+ """+ args = closure.parent+ if closure.type != "arrow_function" or args is None or args.type != "arguments":+ return None+ call = args.parent+ if call.type != "call_expression" or not self.is_awaited(call):+ return None+ fn = call.child_by_field_name("function")+ if fn is None or fn.type not in ("identifier", "member_expression"):+ return None+ name = self.callee_method_name(call) or ""+ if not (_LIMITER_NAME_RE.search(name) or name in _LIMITER_METHODS):+ return None+ return (fn.text or b"").decode() or None++ def runs_in_place(self, closure: Node) -> bool:+ return self._limiter_of(closure) is not None++ def is_awaited(self, node: Node) -> bool:+ # ``await limit(() => fetch(x))`` awaits what the closure's expression body returns.+ if super().is_awaited(node):+ return True+ parent = node.parent+ if parent is None or parent.child_by_field_name("body") != node:+ return False+ return self._limiter_of(parent) is not None++ def concurrency_bound(self, sink: Node, loop: Node) -> str | None:+ if self._exits_early(self.loop_body(loop) or loop):+ return None+ cur = sink.parent+ while cur is not None and cur != loop:+ limiter = self._limiter_of(cur)+ if limiter:+ return limiter+ cur = cur.parent+ return Nonedef batch_form(self, sink: Node, loop: Node, probe: SinkProbe) -> BatchForm | None:"""Prisma ``m.findUnique/findFirst/delete({ where: { f: key } })`` -> its bulk form."""@@ -508,7 +541,7 @@ class TsJsPerfDialect(BasePerfDialect):if (node.type == "call_expression"and self.callee_root_name(node) == iterable- and self.callee_method_name(node) in self._MUTATOR_METHODS+ and self.callee_method_name(node) in self.sequence_appends):return None # a worklist: its keys are not known before the looprefs = [n for n in self._walk(sink) if n.type == "identifier" and n.text == target.text]@@ -525,7 +558,7 @@ class TsJsPerfDialect(BasePerfDialect):)return BatchForm(f"{text}.findMany({{ where: {{ {field}: {{ in: keys }} }} }})",- only_io and method == "findUnique",+ only_io and method == "findUnique" and not self._builds_in_order(body),)def _where_key(self, sink: Node, ref: Node) -> str | None:diff --git a/packages/core/src/repowise/core/analysis/health/perf/sink_reach.py b/packages/core/src/repowise/core/analysis/health/perf/sink_reach.pyindex baa243044..8be4dc37e 100644--- a/packages/core/src/repowise/core/analysis/health/perf/sink_reach.py+++ b/packages/core/src/repowise/core/analysis/health/perf/sink_reach.py@@ -141,7 +141,6 @@ def _hits_for_function(carry_loop: bool,) -> list[PerfHit]:from ..complexity import PerfHit- from .loop_facts import LoopFactsa_sid = index.resolve_function(path, fact.func_start)if a_sid is None:@@ -151,7 +150,7 @@ def _hits_for_function(return []extra: dict[str, Any] = {"func_start": fact.func_start} if carry_func_start else {}- magnitudes = dict(fact.loop_call_magnitudes) if carry_loop else {}+ loop_facts = dict(fact.loop_call_facts) if carry_loop else {}hits: list[PerfHit] = []seen: set[str] = set()for target_name, call_line in entries(fact):@@ -177,9 +176,7 @@ def _hits_for_function(detail=sink_kind.get(info.sink, ""),path=(a_sid, *chain),resolution_basis=basis,- loop=LoopFacts(magnitude=magnitudes[call_line])- if call_line in magnitudes- else None,+ loop=loop_facts.get(call_line),**extra,))diff --git a/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.py b/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.pyindex aadc94783..48543b185 100644--- a/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.py+++ b/packages/core/src/repowise/core/analysis/health/perf/unbounded_reduction.py@@ -47,6 +47,7 @@ class _FileScan:scopes: frozenset[str]def is_db_read(self, call: Node) -> bool:+ # The name lexicon, not ``call_sink_kind``: a ``session.get`` reads one row.return (self.dialect.sink_kind(self.dialect.callee_root_name(call) or "",diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/performance_fix.py b/packages/core/src/repowise/core/analysis/health/refactoring/performance_fix.pyindex 49057c80b..328f4ea33 100644--- a/packages/core/src/repowise/core/analysis/health/refactoring/performance_fix.py+++ b/packages/core/src/repowise/core/analysis/health/refactoring/performance_fix.py@@ -37,7 +37,7 @@ _API_STEPS: dict[str, tuple[str | None, str]] = {"batch_or_prefetch_io": (None,"Collect the keys before the loop and make one call with {api} in place of the "- "per-key filter",+ "per-key call",),"parallelize_independent_awaits": (None,diff --git a/tests/fixtures/perf_corpus/golden_plans.json b/tests/fixtures/perf_corpus/golden_plans.jsonindex 431c262d8..4068da69a 100644--- a/tests/fixtures/perf_corpus/golden_plans.json+++ b/tests/fixtures/perf_corpus/golden_plans.json@@ -57,7 +57,7 @@"safety": "proven","steps": [{- "action": "Collect the keys before the loop and make one call with .in_(\"repo_id\", keys) in place of the per-key filter",+ "action": "Collect the keys before the loop and make one call with .in_(\"repo_id\", keys) in place of the per-key call","applicability": "mechanical","file_path": "src/app/owners.py","line": 20,@@ -1099,7 +1099,7 @@"safety": "advisory","steps": [{- "action": "Collect the keys before the loop and make one call with .in_(\"repo_id\", keys) in place of the per-key filter",+ "action": "Collect the keys before the loop and make one call with .in_(\"repo_id\", keys) in place of the per-key call","applicability": "judgment","file_path": "src/app/commits.py","line": 30,diff --git a/tests/unit/health/test_model_version_coupling.py b/tests/unit/health/test_model_version_coupling.pyindex 08f259995..0e09bd4ad 100644--- a/tests/unit/health/test_model_version_coupling.py+++ b/tests/unit/health/test_model_version_coupling.py@@ -22,7 +22,7 @@ from repowise.core.analysis.health.refactoring.identity import (# The three stamps as they stand together. Moving either identity model means# moving the analyzer with it, and updating this tuple in the same commit.-_STAMPS = (23, 2, 2)+_STAMPS = (24, 2, 2)def test_the_identity_models_and_the_analyzer_stamp_are_pinned_together() -> None:diff --git a/tests/unit/health/test_perf_chunked_go_java.py b/tests/unit/health/test_perf_chunked_go_java.pynew file mode 100644index 000000000..029772539--- /dev/null+++ b/tests/unit/health/test_perf_chunked_go_java.py@@ -0,0 +1,285 @@+"""Unit tests for ``is_chunked_loop`` on the Go and Java perf dialects.++Mirrors ``test_perf_loop_facts_ts.py``: goes through the real walker+(``walk_file``) so the tests also cover the sink-detection gate each hit+depends on (a hit only carries ``.loop`` facts when its call is first+classified as an I/O sink). Precision over recall: every negative asserts+``loop is None`` or ``not loop.chunked``, never a guess.+"""++from __future__ import annotations++from repowise.core.analysis.health.complexity import walk_file+++def _io_hits(src: str, lang: str):+ fc = walk_file(f"f.{lang}", lang, src.encode())+ return [h for h in fc.perf_hits if h.kind == "io_in_loop"]+++# ---------------------------------------------------------------------------+# Go+# ---------------------------------------------------------------------------+++def test_go_counted_loop_with_non_unit_step_is_chunked():+ src = """+package sample+import "net/http"+func f(urls []string) {+ for i := 0; i < len(urls); i += 10 {+ http.Get(urls[i])+ }+}+"""+ hits = _io_hits(src, "go")+ assert len(hits) == 1+ assert hits[0].loop is not None and hits[0].loop.chunked is True+++def test_go_counted_loop_with_named_step_is_chunked():+ src = """+package sample+import "net/http"+func f(urls []string, step int) {+ for i := 0; i < len(urls); i += step {+ http.Get(urls[i])+ }+}+"""+ hits = _io_hits(src, "go")+ assert len(hits) == 1+ assert hits[0].loop is not None and hits[0].loop.chunked is True+++def test_go_range_over_slices_chunk_is_chunked():+ src = """+package sample+import "net/http"+func f(urls []string) {+ for _, chunk := range slices.Chunk(urls, 10) {+ http.Get(chunk[0])+ }+}+"""+ hits = _io_hits(src, "go")+ assert len(hits) == 1+ assert hits[0].loop is not None and hits[0].loop.chunked is True+++def test_go_range_over_named_batch_helper_is_chunked():+ src = """+package sample+import "net/http"+func f(urls []string) {+ for _, b := range Batch(urls, 5) {+ http.Get(b[0])+ }+}+"""+ hits = _io_hits(src, "go")+ assert len(hits) == 1+ assert hits[0].loop is not None and hits[0].loop.chunked is True+++def test_go_unit_increment_is_not_chunked():+ src = """+package sample+import "net/http"+func f(urls []string) {+ for i := 0; i < len(urls); i++ {+ http.Get(urls[i])+ }+}+"""+ hits = _io_hits(src, "go")+ assert len(hits) == 1+ assert hits[0].loop is None or not hits[0].loop.chunked+++def test_go_literal_step_of_one_is_not_chunked():+ src = """+package sample+import "net/http"+func f(urls []string) {+ for i := 0; i < len(urls); i += 1 {+ http.Get(urls[i])+ }+}+"""+ hits = _io_hits(src, "go")+ assert len(hits) == 1+ assert hits[0].loop is None or not hits[0].loop.chunked+++def test_go_countdown_is_not_chunked():+ # ``i >= 0`` (a literal condition bound) trips ``is_constant_loop`` and+ # suppresses the sink entirely, so the bound is a variable here to isolate+ # the ``is_chunked_loop`` predicate under test.+ src = """+package sample+import "net/http"+func f(urls []string, step int, lowest int) {+ for i := len(urls) - 1; i >= lowest; i -= step {+ http.Get(urls[i])+ }+}+"""+ hits = _io_hits(src, "go")+ assert len(hits) == 1+ assert hits[0].loop is None or not hits[0].loop.chunked+++def test_go_range_over_plain_slice_is_not_chunked():+ src = """+package sample+import "net/http"+func f(urls []string) {+ for _, u := range urls {+ http.Get(u)+ }+}+"""+ hits = _io_hits(src, "go")+ assert len(hits) == 1+ assert hits[0].loop is None or not hits[0].loop.chunked+++# ---------------------------------------------------------------------------+# Java+# ---------------------------------------------------------------------------+++def test_java_counted_loop_with_non_unit_step_is_chunked():+ src = """+class C {+ void f(java.util.List<Integer> ids) {+ for (int i = 0; i < ids.size(); i += 10) {+ repo.findByIdIn(ids.get(i));+ }+ }+}+"""+ hits = _io_hits(src, "java")+ assert len(hits) == 1+ assert hits[0].loop is not None and hits[0].loop.chunked is True+++def test_java_counted_loop_with_named_step_is_chunked():+ src = """+class C {+ void f(java.util.List<Integer> ids, int step) {+ for (int i = 0; i < ids.size(); i += step) {+ repo.findByIdIn(ids.get(i));+ }+ }+}+"""+ hits = _io_hits(src, "java")+ assert len(hits) == 1+ assert hits[0].loop is not None and hits[0].loop.chunked is True+++def test_java_enhanced_for_over_lists_partition_is_chunked():+ src = """+class C {+ void f(java.util.List<Integer> ids) {+ for (java.util.List<Integer> chunk : Lists.partition(ids, 10)) {+ repo.findByIdIn(chunk);+ }+ }+}+"""+ hits = _io_hits(src, "java")+ assert len(hits) == 1+ assert hits[0].loop is not None and hits[0].loop.chunked is True+++def test_java_enhanced_for_over_list_utils_partition_is_chunked():+ src = """+class C {+ void f(java.util.List<Integer> ids) {+ for (java.util.List<Integer> chunk : ListUtils.partition(ids, 10)) {+ repo.findByIdIn(chunk);+ }+ }+}+"""+ hits = _io_hits(src, "java")+ assert len(hits) == 1+ assert hits[0].loop is not None and hits[0].loop.chunked is True+++def test_java_enhanced_for_over_named_partition_call_is_chunked():+ src = """+class C {+ void f(java.util.List<Integer> ids) {+ for (java.util.List<Integer> chunk : partition(ids, 10)) {+ repo.findByIdIn(chunk);+ }+ }+}+"""+ hits = _io_hits(src, "java")+ assert len(hits) == 1+ assert hits[0].loop is not None and hits[0].loop.chunked is True+++def test_java_unit_increment_is_not_chunked():+ src = """+class C {+ void f(java.util.List<Integer> ids) {+ for (int i = 0; i < ids.size(); i++) {+ repo.findByIdIn(ids.get(i));+ }+ }+}+"""+ hits = _io_hits(src, "java")+ assert len(hits) == 1+ assert hits[0].loop is None or not hits[0].loop.chunked+++def test_java_literal_step_of_one_is_not_chunked():+ src = """+class C {+ void f(java.util.List<Integer> ids) {+ for (int i = 0; i < ids.size(); i += 1) {+ repo.findByIdIn(ids.get(i));+ }+ }+}+"""+ hits = _io_hits(src, "java")+ assert len(hits) == 1+ assert hits[0].loop is None or not hits[0].loop.chunked+++def test_java_countdown_is_not_chunked():+ src = """+class C {+ void f(java.util.List<Integer> ids, int step) {+ for (int i = ids.size() - 1; i >= 0; i -= step) {+ repo.findByIdIn(ids.get(i));+ }+ }+}+"""+ hits = _io_hits(src, "java")+ assert len(hits) == 1+ assert hits[0].loop is None or not hits[0].loop.chunked+++def test_java_enhanced_for_over_plain_list_is_not_chunked():+ src = """+class C {+ void f(java.util.List<Integer> ids) {+ for (Integer id : ids) {+ repo.findByIdIn(id);+ }+ }+}+"""+ hits = _io_hits(src, "java")+ assert len(hits) == 1+ assert hits[0].loop is None or not hits[0].loop.chunkeddiff --git a/tests/unit/health/test_perf_crossfn.py b/tests/unit/health/test_perf_crossfn.pyindex 847db4259..eeff8bbf8 100644--- a/tests/unit/health/test_perf_crossfn.py+++ b/tests/unit/health/test_perf_crossfn.py@@ -554,3 +554,14 @@ def test_crossfn_hit_carries_the_owning_loops_magnitude(tmp_path):)(finding,) = IoInLoopDetector().detect(ctx)assert finding.details["loop_magnitude"] == "grows_with_data"+++def test_crossfn_hit_carries_the_owning_loops_chunking(tmp_path):+ """A caller that already walks its rows a batch at a time is not told to batch."""+ source = _SCHEDULER.replace(+ " for repo in repos:\n out.append(self._poll(repo))",+ " for batch in batched(repos, 100):\n out.append(self._poll(batch))",+ )+ walked, graph = _build(tmp_path, {"scheduler.py": source})+ (hit,) = [h for hs in collect_crossfn_io_in_loop(walked, graph).values() for h in hs]+ assert hit.loop is not None and hit.loop.chunkeddiff --git a/tests/unit/health/test_perf_loop_facts_python.py b/tests/unit/health/test_perf_loop_facts_python.pyindex ec5432203..0a59e5282 100644--- a/tests/unit/health/test_perf_loop_facts_python.py+++ b/tests/unit/health/test_perf_loop_facts_python.py@@ -8,6 +8,8 @@ paired with the refusal(s) that protect precision (SPEC.md "Dialect hooks").from __future__ import annotations+import pytest+from repowise.core.analysis.health.complexity import walk_filefrom repowise.core.analysis.health.perf.loop_facts import LoopFacts@@ -456,3 +458,165 @@ def test_magnitude_sees_through_an_empty_fallback():b" sb.table('t').select('*').eq('id', row['id']).execute()\n")assert facts.magnitude == "grows_with_data"+++# ---------------------------------------------------------------------------+# session.get(Model, key): a db sink+# ---------------------------------------------------------------------------+++ ("call", "db_import", "sink"),+ [+ ("await session.get(Repo, i)", True, True),+ ("await session.get(models.Repo, i)", True, True),+ ("await session.get(DBUser, i)", True, True),+ ("await self.get(User, i)", False, False), # no db evidence in the file+ ("await session.get(User)", True, False),+ ("d.get(i, None)", True, False),+ ("cache.get('k', None)", True, False),+ ("os.environ.get('KEY', i)", True, False),+ ("requests.get(API_URL, i)", True, False), # an ALL_CAPS constant, not a model+ ("session.get(*args)", True, False),+ ],+)+def test_session_get_sink_shape(call, db_import, sink):+ header = "from sqlalchemy import select\n" if db_import else ""+ src = f"{header}async def f(session, self, d, cache, args, ids):\n for i in ids:\n {call}\n"+ hits = _hits(src.encode())+ assert [h.detail for h in hits] == (["db"] if sink else [])+++# ---------------------------------------------------------------------------+# session.get(Model, key): the batch form+# ---------------------------------------------------------------------------+++def test_batch_session_get_equivalent():+ facts = _loop(+ b"from sqlalchemy import select\n"+ b"async def f(session, rows):\n"+ b" for r in rows:\n"+ b" obj = await session.get(Repo, r.id)\n"+ )+ assert facts.batch.call == "select(Repo).where(inspect(Repo).primary_key[0].in_(keys))"+ assert facts.batch.equivalent is True+++def test_batch_session_get_on_the_whole_element_is_not_equivalent():+ """The element itself may be a composite key tuple, which one column's IN does not match."""+ facts = _loop(+ b"from sqlalchemy import select\n"+ b"async def f(session, keys):\n"+ b" for key in keys:\n"+ b" obj = await session.get(Repo, key)\n"+ )+ assert facts.batch is not None and facts.batch.equivalent is False+++def test_batch_session_get_second_sink_in_body_not_equivalent():+ hits = _hits(+ b"from sqlalchemy import select\n"+ b"async def f(session, others, ids):\n"+ b" for rid in ids:\n"+ b" session.execute(select(Other))\n"+ b" obj = await session.get(Repo, rid)\n"+ )+ get_hit = next(h for h in hits if h.loop is not None and h.loop.batch is not None)+ assert get_hit.loop.batch.equivalent is False+++def test_batch_session_get_none_when_key_computed_from_element():+ facts = _loop(+ b"from sqlalchemy import select\n"+ b"async def f(session, ids):\n"+ b" for rid in ids:\n"+ b" obj = await session.get(Repo, f'x:{rid}')\n"+ )+ assert facts is None or facts.batch is None++++def test_a_result_built_in_key_order_is_not_equivalent():+ """One IN query returns rows in its own order, not grouped by the loop's keys."""+ facts = _loop(+ b"from sqlalchemy import select\n"+ b"def f(session, events):\n"+ b" out = []\n"+ b" for e in events:\n"+ b" rows = session.query(A).filter(A.event_id == e.id).all()\n"+ b" out.extend(rows)\n"+ b" return out\n"+ )+ assert facts.batch is not None and facts.batch.equivalent is False+++def test_session_get_of_a_key_already_used_in_the_iteration_is_not_a_sink():+ """The earlier call may have loaded the row, so the second ``get`` is served from memory."""+ hits = _hits(+ b"from sqlalchemy import select\n"+ b"async def f(session, recs):\n"+ b" for rec in recs:\n"+ b" await upsert(session, rec.id)\n"+ b" meta = await session.get(Meta, rec.id)\n"+ )+ assert all(h.line != 5 for h in hits)+++def test_a_read_filtered_to_one_chunk_of_keys_does_not_grow():+ facts = _loop(+ b"from sqlalchemy import select\n"+ b"async def f(session, paths):\n"+ b" for i in range(0, len(paths), BATCH):\n"+ b" result = await session.execute(select(G).where(G.path.in_(paths[i : i + BATCH])))\n"+ b" for g in result.scalars().all():\n"+ b" await session.execute(select(H).where(H.id == g.id))\n"+ )+ assert facts.magnitude != "grows_with_data"+++def test_a_read_filtered_to_a_chunk_loops_element_does_not_grow():+ facts = _loop(+ b"from sqlalchemy import select\n"+ b"async def f(session, paths):\n"+ b" for chunk in chunked(paths):\n"+ b" result = await session.execute(select(G).where(G.path.in_(chunk)))\n"+ b" for g in result.all():\n"+ b" await session.execute(select(H).where(H.id == g.id))\n"+ )+ assert facts.magnitude != "grows_with_data"+++def test_a_limit_on_a_statement_built_before_the_loop_caps_the_read():+ facts = _loop(+ b"from sqlalchemy import select\n"+ b"async def f(session, limit):\n"+ b" stmt = select(G).limit(limit)\n"+ b" rows = (await session.execute(stmt)).scalars().all()\n"+ b" for g in rows:\n"+ b" await session.execute(select(H).where(H.id == g.id))\n"+ )+ assert facts.magnitude != "grows_with_data"+++def test_a_dict_lookup_of_the_key_does_not_hide_the_session_get():+ hits = _hits(+ b"from sqlalchemy import select\n"+ b"async def f(session, rows, quotes):\n"+ b" for row in rows:\n"+ b" quote = quotes.get(row.old_id)\n"+ b" rec = await session.get(Rec, row.old_id)\n"+ )+ assert any(h.line == 5 for h in hits)+++def test_a_result_read_from_one_chunk_of_keys_does_not_grow():+ facts = _loop(+ b"from sqlalchemy import select\n"+ b"async def f(session, paths):\n"+ b" for chunk in chunked(paths):\n"+ b" result = await session.execute(select(G).where(G.path.in_(chunk)))\n"+ b" for g in result.scalars().all():\n"+ b" await session.execute(select(H).where(H.id == g.id))\n"+ )+ assert facts.magnitude != "grows_with_data"diff --git a/tests/unit/health/test_perf_loop_facts_ts.py b/tests/unit/health/test_perf_loop_facts_ts.pyindex b817ec90f..40790b3ed 100644--- a/tests/unit/health/test_perf_loop_facts_ts.py+++ b/tests/unit/health/test_perf_loop_facts_ts.py@@ -256,3 +256,183 @@ function f(ids, n) {hits = _io_hits(src)assert hitsassert hits[0].loop is None or hits[0].loop.chunked is False+++# ---------------------------------------------------------------------------+# runs_in_place / concurrency_bound: an awaited limiter closure is the loop body+# ---------------------------------------------------------------------------+++def test_stored_closure_is_not_in_place():+ src = """+async function f(xs) {+ for (const x of xs) {+ const cb = () => fetch(x);+ await limit(cb);+ }+}+"""+ assert _io_hits(src) == []+++def test_returned_closure_is_not_in_place():+ src = """+function make(xs) {+ for (const x of xs) {+ return () => fetch(x);+ }+}+"""+ assert _io_hits(src) == []+++def test_unawaited_limiter_call_is_not_in_place():+ # The fan-out already happens here: `limit(...)` is queued, not awaited.+ src = """+async function f(xs) {+ for (const x of xs) {+ tasks.push(limit(() => fetch(x)));+ }+}+"""+ assert _io_hits(src) == []+++def test_limiter_call_inside_a_non_limiter_callback_is_not_in_place():+ # The outer closure is the argument of `other(...)`, not a limiter — its+ # boundary resets loop_depth regardless of the limiter call nested inside.+ src = """+async function f(xs) {+ for (const x of xs) {+ await other(() => { limit(() => fetch(x)); });+ }+}+"""+ assert _io_hits(src) == []+++def test_closure_passed_to_settimeout_is_not_in_place():+ src = """+async function f(xs) {+ for (const x of xs) {+ setTimeout(() => fetch(x), 0);+ }+}+"""+ assert _io_hits(src) == []+++def test_limiter_built_inside_the_loop_body_bounds_nothing():+ # `pLimit(2)` is itself a call, so the enclosing call's callee is a+ # `call_expression`, not an identifier/member naming a limiter — the+ # closure does not run in place, and even if it did there would be no+ # stable limiter identity to report.+ src = """+async function f(xs) {+ for (const x of xs) {+ await pLimit(2)(() => fetch(x));+ }+}+"""+ assert _io_hits(src) == []+++def test_break_in_body_gives_a_hit_with_no_bound():+ src = """+async function f(xs) {+ for (const x of xs) {+ if (!x) break;+ await limit(() => fetch(x));+ }+}+"""+ hits = _io_hits(src)+ assert hits+ assert hits[0].loop is None or hits[0].loop.concurrency_bound is None+++def test_limit_closure_runs_in_place_and_bounds_the_sink():+ src = """+async function f(xs) {+ for (const x of xs) {+ await limit(() => fetch(x));+ }+}+"""+ for kind in ("io_in_loop", "serial_await_in_loop"):+ hits = _hits(src, kind)+ assert hits and hits[0].loop is not None+ assert hits[0].loop.concurrency_bound == "limit"+++def test_mutex_run_exclusive_closure_runs_in_place_and_bounds_the_sink():+ src = """+async function f(xs) {+ for (const x of xs) {+ await mutex.runExclusive(async () => { await fetch(x); });+ }+}+"""+ hits = _io_hits(src)+ assert hits+ assert hits[0].loop is not None+ assert hits[0].loop.concurrency_bound == "mutex.runExclusive"+ # The sink is awaited inside the closure too, so it is additionally a+ # missed-concurrency co-signal — carrying the same bound.+ serial = _hits(src, "serial_await_in_loop")+ assert serial+ assert serial[0].loop is not None+ assert serial[0].loop.concurrency_bound == "mutex.runExclusive"+++def test_prisma_find_unique_pushed_in_key_order_not_equivalent():+ src = """+async function f(users) {+ const out = [];+ for (const u of users) {+ out.push(await prisma.user.findUnique({ where: { id: u.id } }));+ }+ return out;+}+"""+ loop = _io_hits(src)[0].loop+ assert loop is not None and loop.batch is not None and loop.batch.equivalent is False+++def test_a_float_step_of_one_is_not_chunked():+ src = """+async function f(ids) {+ for (let i = 0; i < ids.length; i += 1.0) {+ await fetch(ids[i]);+ }+}+"""+ loop = _io_hits(src)[0].loop+ assert loop is None or not loop.chunked+++def test_a_push_inside_a_limiter_closure_still_breaks_equivalence():+ src = """+async function f(users) {+ const out = [];+ for (const u of users) {+ await mutex.runExclusive(async () => {+ out.push(await prisma.user.findUnique({ where: { id: u.id } }));+ });+ }+ return out;+}+"""+ loop = _io_hits(src)[0].loop+ assert loop is not None and loop.batch is not None and loop.batch.equivalent is False+++def test_an_awaited_non_limiter_callback_is_not_in_place():+ src = """+async function f(xs) {+ for (const x of xs) {+ await retry(() => fetch(x));+ }+}+"""+ assert _io_hits(src) == []