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
refactor(health): dispatch the perf walk to per-node handlers (#4067)
Raghav Chamadiyae788f0afdcaa
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.pyindex c02322715..a77d466bb 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@@ -22,7 +22,7 @@ from __future__ import annotationsimport refrom collections.abc import Iteratorfrom dataclasses import replace-from typing import TYPE_CHECKING+from typing import TYPE_CHECKING, NamedTuplefrom ..perf.dialects import PERF_DIALECTSfrom ..perf.dialects.base import BasePerfDialect as BasePerfDialectClass@@ -517,26 +517,50 @@ def _collect_perf_hits() -> tuple[list[PerfHit], dict[str, str], list[PerfFnFacts]]:"""Whole-tree perf pass → ``(hits, io_boundary_names, fn_facts)``.- Iterative DFS carrying ``(node, loop_depth, in_async, func_name,- func_start, lock_depth)`` — the proven Phase-0 shape, extended with the- enclosing function's start line (so per-function facts key to a graph symbol- node) and a block-scoped ``lock_depth`` (so an I/O sink under a held lock is- flagged). Loop-body scoping and constant-loop skipping are applied so only- genuinely per-iteration calls are flagged. Returns nothing for languages- that opt out of the perf pass (empty ``call_kinds``).+ An iterative DFS visits each node in a :class:`_Frame` (loop depth, async,+ enclosing function, lock depth). Per-function facts key to the function's+ start line, and a block-scoped lock depth flags an I/O sink under a held+ lock. Loop-body scoping and constant-loop skipping keep only per-iteration+ calls. Returns nothing for languages that opt out (empty ``call_kinds``).*io_names* is ``perf.io_boundaries.collect_io_names`` for *root*, which thewalker's shared file descent collects whenever ``perf_pass_runs``.Alongside the same-function ``hits``, it accumulates ``fn_facts`` (one:class:`PerfFnFacts` per enclosing function that has any loop-nested call- or a bare sink) — the input to PR4's cross-function reachability.+ or a bare sink), the input to cross-function reachability."""if not perf_pass_runs(language, lmap):return [], {}, []return _PerfWalk(root, language, lmap, io_names).run()+class _Frame(NamedTuple):+ """The DFS context a node is visited in."""++ loop_depth: int+ in_async: bool+ func: str | None+ start: int+ lock_depth: int+ # Whether the outermost enclosing loop iterates a collection (not a+ # ``while`` / cursor): the precision gate for ``nested_loop_with_io``.+ outer_iter: bool+ # The innermost data-dependent loop's header line, 0 outside one.+ loop_line: int+++_NO_LOOP: tuple[bool, None] = (False, None)+++class _Call(NamedTuple):+ node: Node+ method: str+ root_name: str+ awaited: bool+ line: int++class _PerfWalk:"""One file's perf pass: the dialect's switches, a per-loop memo and theper-function accumulators the DFS in :meth:`run` fills."""@@ -586,6 +610,12 @@ class _PerfWalk:self.do_bound = _overrides(dialect, "concurrency_bound")self.do_loop_facts = self.do_chunked or self.do_magnitude or self.do_batch or self.do_boundself.do_in_place = _overrides(dialect, "runs_in_place")+ # Node kinds a loop, scope or call handler can apply to: one set test per node.+ self.loop_probe = self.loop_kinds | (self.call_kinds if self.do_block_loop else frozenset())+ self.scope_probe = self.scope_kinds | self.async_fn_kinds+ self.call_probe = self.call_kinds | (+ self.bare_call_wrapper_kinds if self.do_bare_stmt_call else frozenset()+ )# An awaited call on a line whose lint suppression says awaiting in this loop# is intended (a retry or a stream read) is not a per-iteration finding.self.awaits_allowed = (@@ -688,465 +718,394 @@ class _PerfWalk:def run(self) -> tuple[list[PerfHit], dict[str, str], list[PerfFnFacts]]:"""The DFS over the file, then the per-line dedupe and per-function facts."""- root, lmap, io_names, dialect = self.root, self.lmap, self.io_names, self.dialect- call_kinds, loop_kinds, fn_kinds = self.call_kinds, self.loop_kinds, self.fn_kinds- lambda_kinds, scope_kinds = self.lambda_kinds, self.scope_kinds- async_fn_kinds, access_kinds = self.async_fn_kinds, self.access_kinds- bare_call_wrapper_kinds = self.bare_call_wrapper_kinds- has_db_import, list_names = self.has_db_import, self.list_names- awaits_allowed = self.awaits_allowed- do_string_concat, do_blocking = self.do_string_concat, self.do_blocking- do_loop_call_marker = self.do_loop_call_marker- do_loop_stmt_marker = self.do_loop_stmt_marker- do_bare_call_marker = self.do_bare_call_marker- do_loop_iterable_call_marker = self.do_loop_iterable_call_marker- do_serial_await, do_nested_io = self.do_serial_await, self.do_nested_io- do_nested_quadratic, do_hot_path = self.do_nested_quadratic, self.do_hot_path- do_lock_io, do_block_loop = self.do_lock_io, self.do_block_loop- do_bare_stmt_call, do_in_place = self.do_bare_stmt_call, self.do_in_place- probe, loop_facts, _acc = self.probe, self.loop_facts, self._acc- hits, chain_of, fn_acc = self.hits, self.chain_of, self.fn_acc- call_facts, keyed_lines = self.call_facts, self.keyed_lines- call_loop_lines, lambda_spans = self.call_loop_lines, self.lambda_spans- # (node, loop_depth, in_async, func_name, func_start, lock_depth, outer_iter,- # loop_line). ``outer_iter`` is whether the OUTERMOST enclosing loop iterates- # a collection (vs a ``while``/cursor) — the precision gate for- # ``nested_loop_with_io``. ``loop_line`` is the innermost data-dependent- # loop's header line (0 outside one), so a reader can show the iteration.- stack: list[tuple[Node, int, bool, str | None, int, int, bool, int]] = [- (root, 0, False, None, 0, 0, True, 0)- ]+ loop_probe, scope_probe = self.loop_probe, self.scope_probe+ do_lock_io = self.do_lock_io+ stack: list[tuple[Node, _Frame]] = [(self.root, _Frame(0, False, None, 0, 0, True, 0))]while stack:- (- node, loop_depth, in_async, func_name, func_start, lock_depth, outer_iter, loop_line- ) = stack.pop()+ node, cur = stack.pop()t = node.type+ is_loop, block_loop_body = self._loop_kind(node, t) if t in loop_probe else _NO_LOOP+ nxt = self._scope(node, t, cur) if t in scope_probe else cur+ if is_loop:+ self._on_loop(node, cur, nxt)+ self._visit(node, t, cur, nxt)+ if is_loop:+ self._push_loop_children(stack, node, cur, nxt, block_loop_body)+ elif do_lock_io and self._is_lock_entry(node):+ _push_lock_children(stack, node, nxt)+ else:+ stack.extend([(c, nxt) for c in node.children])+ return _dedupe(self.hits, self.lambda_spans), self.io_names, self._fn_facts()- # ``node.is_named`` guards grammars (Ruby) whose keyword tokens share- # the node-type name of their parent (a ``while`` node contains an- # unnamed ``while`` token) — only the named node is the loop.- is_loop = t in loop_kinds and node.is_named- # Block-iteration loop (Ruby ``items.each do … end``): the dialect- # recognises the call and returns the per-iteration body node; the- # receiver / arguments still run once (native loop-BODY scoping).- block_loop_body: Node | None = None- if not is_loop and do_block_loop and t in call_kinds:- block_loop_body = dialect.block_loop_body(node)- if block_loop_body is not None:- is_loop = True- if is_loop and dialect.is_constant_loop(node):- is_loop = False- block_loop_body = None-- entering_fn = t in fn_kinds or t in lambda_kinds- is_async_fn = t in async_fn_kinds or (entering_fn and dialect.is_async_fn(node))- next_async = True if is_async_fn else (False if entering_fn else in_async)- # A nested function/lambda opens a new execution scope: its body is not- # run per outer-loop-iteration nor while an outer lock is held (it is- # merely DEFINED here — it runs whenever/wherever it is later invoked).- # Reset both depths at the boundary, mirroring ``next_async``; the- # closure's OWN loops/locks still count from its own body.- # Ceiling: a closure INVOKED inline in the loop (``(lambda: io())()`` /- # Go's ``func(){…}()`` IIFE) does run per-iteration, but we do not detect- # 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 { … }``),- # 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))+ def _visit(self, node: Node, t: str, cur: _Frame, nxt: _Frame) -> None:+ """Hits and facts of *node* itself, stamped with the loop the frame names.++ The iterable marker ``_on_loop`` adds sits on the loop's own header and is not stamped.+ """+ hits = self.hits+ stamp_from = len(hits)+ if t in self.call_probe and (call_node := self._call_node(node, t)) is not None:+ self._on_call(call_node, cur, nxt)+ elif cur.loop_depth or (self.do_blocking and cur.in_async):+ self._on_statement(node, cur, nxt)+ if cur.loop_line and len(hits) > stamp_from:+ stamped = hits[stamp_from:]+ hits[stamp_from:] = [replace(h, loop_line=cur.loop_line) for h in stamped]++ def _loop_kind(self, node: Node, t: str) -> tuple[bool, Node | None]:+ """``(is_loop, block_loop_body)``: a data-dependent loop, and the+ per-iteration body when it is a block-iteration call (Ruby ``items.each do``)."""+ # ``is_named`` guards grammars (Ruby) whose keyword tokens share the+ # node-type name of their parent loop.+ is_loop = t in self.loop_kinds and node.is_named+ block_loop_body: Node | None = None+ if not is_loop and self.do_block_loop and t in self.call_kinds:+ block_loop_body = self.dialect.block_loop_body(node)+ if block_loop_body is not None:+ is_loop = True+ if is_loop and self.dialect.is_constant_loop(node):+ return False, None+ return is_loop, block_loop_body++ def _scope(self, node: Node, t: str, cur: _Frame) -> _Frame:+ """The frame *node*'s own visit and children see when it opens a function or lambda.++ A nested function is only defined here, so loop and lock depth reset at+ its boundary. Not reset: a lambda a block-iteration combinator runs per+ element (``ids.forEach { … }``) and a closure the dialect proves runs in+ place. An inline-invoked closure (an IIFE) is reset too: a recall ceiling.+ """+ dialect = self.dialect+ entering_fn = t in self.scope_kinds+ is_async_fn = t in self.async_fn_kinds or (entering_fn and dialect.is_async_fn(node))+ next_async = True if is_async_fn else (False if entering_fn else cur.in_async)+ body_scope = entering_fn and (+ (self.do_block_loop and _is_block_loop_body_scope(node, dialect, self.call_kinds))+ or (self.do_in_place and dialect.runs_in_place(node))+ )+ func, start = self._scope_owner(node, t, cur)+ if body_scope or not entering_fn:+ return cur._replace(in_async=next_async, func=func, start=start)+ return _Frame(0, next_async, func, start, 0, cur.outer_iter, 0)++ def _scope_owner(self, node: Node, t: str, cur: _Frame) -> tuple[str | None, int]:+ """``(func, start)`` the hits under *node* key to; a top-level lambda's span is recorded."""+ if t in self.fn_kinds and is_function_node(node, self.lmap):+ return _perf_func_name(node) or cur.func, node.start_point[0] + 1+ if t in self.lambda_kinds and cur.func is None:+ self.lambda_spans.append(+ (+ node.start_point[0] + 1,+ node.end_point[0] + 1,+ _find_function_entry_name(node, self.lmap),+ ))- next_loop_depth = loop_depth if (body_scope or not entering_fn) else 0- next_lock_depth = lock_depth if (body_scope or not entering_fn) else 0- next_loop_line = loop_line if (body_scope or not entering_fn) else 0- next_func = func_name- next_start = func_start- if t in fn_kinds and is_function_node(node, lmap):- next_func = _perf_func_name(node) or func_name- next_start = node.start_point[0] + 1- elif t in lambda_kinds and func_name is None:- lambda_spans.append(- (- node.start_point[0] + 1,- node.end_point[0] + 1,- _find_function_entry_name(node, lmap),- )+ return cur.func, cur.start++ def _on_loop(self, node: Node, cur: _Frame, nxt: _Frame) -> None:+ """Facts and markers a loop node carries on its own header line."""+ dialect = self.dialect+ # A data-dependent loop inside another, iterating the same named+ # collection (all-pairs O(n^2)), is a fact the engine turns into+ # ``nested_loop_quadratic`` only for a hot function.+ if cur.loop_depth >= 1 and self.do_nested_quadratic:+ nm = dialect.loop_iterable_name(node)+ if nm and nm in _enclosing_loop_iterables(+ node, dialect, self.loop_kinds, self.fn_kinds, self.do_block_loop+ ):+ misc = self._acc(nxt.start, nxt.func)[3]+ if misc[0] == 0:+ misc[0] = node.start_point[0] + 1+ # A loop whose iterable is itself a slow call (``df.iterrows()``) sits in+ # the header, which the body markers never see.+ if self.do_loop_iterable_call_marker:+ icm = dialect.loop_iterable_call_marker(node)+ if icm is not None:+ self.hits.append(+ PerfHit(icm, node.start_point[0] + 1, nxt.func, "", func_start=nxt.start))- # A data-dependent loop nested inside another (``loop_depth`` only counts- # non-constant loops) is an O(n^2) shape. Record the site as a fact; the- # engine emits ``nested_loop_quadratic`` only when the function is hot- # (centrality gate), so the noisy-everywhere shape never ships ungated.- if is_loop and loop_depth >= 1 and do_nested_quadratic:- # Shape gate (Phase-7d): raw nesting depth was ~3-20% precision in- # every language. Fire only on the SAME-COLLECTION shape — the inner- # loop iterates the same named collection as an enclosing loop- # (all-pairs O(n^2)) — which all four Phase-7c labelers converged on.- nm = dialect.loop_iterable_name(node)- if nm and nm in _enclosing_loop_iterables(- node, dialect, loop_kinds, fn_kinds, do_block_loop- ):- misc = _acc(next_start, next_func)[3]- if misc[0] == 0:- misc[0] = node.start_point[0] + 1-- # A loop whose ITERABLE is itself a slow call (``for _, r in- # df.iterrows()``): the call sits in the loop header (runs once), so the- # body call markers never see it. Fire on the loop node itself, at any- # nesting depth — ``iterrows`` is O(n)-boxing slow on its own.- if is_loop and do_loop_iterable_call_marker:- icm = dialect.loop_iterable_call_marker(node)- if icm is not None:- hits.append(- PerfHit(icm, node.start_point[0] + 1, next_func, "", func_start=next_start)+ def _call_node(self, node: Node, t: str) -> Node | None:+ if t in self.call_kinds:+ return node+ if self.do_bare_stmt_call and t in self.bare_call_wrapper_kinds:+ # A parenless call (Pascal ``Q.Open;``) has no call node; the wrapper+ # also holds non-calls (``Exit;``), which the dialect answers None for.+ return self.dialect.bare_statement_call(node)+ return None++ def _on_call(self, call_node: Node, cur: _Frame, nxt: _Frame) -> None:+ dialect = self.dialect+ call = _Call(+ call_node,+ dialect.callee_method_name(call_node) or "",+ dialect.callee_root_name(call_node) or "",+ dialect.is_awaited(call_node),+ call_node.start_point[0] + 1,+ )+ if self.do_bare_call_marker:+ # A call that is its own iteration construct (``.reduce`` with an+ # accumulator spread): a smell at any loop depth.+ bare = dialect.bare_call_marker(call.root_name, call.method, call_node)+ if bare is not None:+ self.hits.append(PerfHit(bare, call.line, nxt.func, "", func_start=nxt.start))+ kind = dialect.call_sink_kind(+ call_node,+ awaited=call.awaited,+ io_names=self.io_names,+ has_db_import=self.has_db_import,+ )+ if kind is None:+ self._on_helper_call(call, cur, nxt)+ else:+ chain = self._sink_chain(call)+ # A link is neither a hit nor a helper: its chain's sink speaks for it.+ if chain is not None:+ self._on_sink(call, kind, chain, cur, nxt)+ if self.do_blocking and cur.in_async and not call.awaited:+ api = dialect.blocking_sync_api(call.root_name, call.method)+ if api is not None and not dialect.task_already_complete(call_node):+ self.hits.append(+ PerfHit(+ "blocking_sync_in_async", call.line, nxt.func, api, func_start=nxt.start)- # Hits from here on sit in the loop the stack names; the iterable marker- # above is on the loop's own header line.- stamp_from = len(hits)-- call_node: Node | None = None- if t in call_kinds:- call_node = node- elif do_bare_stmt_call and t in bare_call_wrapper_kinds:- # A parenless call (Pascal ``Q.Open;``): no ``call_kinds`` node- # exists at all, so the dialect is asked whether this particular- # statement wrapper IS one (it also wraps non-call statements —- # Pascal's bare ``Exit;`` / ``inherited;`` — that must stay None).- call_node = dialect.bare_statement_call(node)-- if call_node is not None:- method = dialect.callee_method_name(call_node) or ""- root_name = dialect.callee_root_name(call_node) or ""- awaited = dialect.is_awaited(call_node)- line = call_node.start_point[0] + 1- if do_bare_call_marker:- # A call that is its own iteration construct (``.reduce`` with an- # accumulator spread) — a perf smell at any loop depth.- bare = dialect.bare_call_marker(root_name, method, call_node)- if bare is not None:- hits.append(PerfHit(bare, line, next_func, "", func_start=next_start))- kind = dialect.call_sink_kind(- call_node, awaited=awaited, io_names=io_names, has_db_import=has_db_import)- chain_top = call_node- chain_awaited = awaited- is_link = False- if kind is not None:- inner = None- for link in _receiver_chain_calls(call_node, call_kinds, access_kinds):- if probe(link) is not None:- inner = link- if inner is not None:- # A link: the innermost sink below it is the hit. The- # outermost link is met first and stays the chain's top.- top, link_awaited = chain_of.get(inner.id, (call_node, False))- chain_of[inner.id] = (top, link_awaited or awaited)- is_link = True- else:- chain_top, link_awaited = chain_of.pop(call_node.id, (call_node, False))- chain_awaited = awaited or link_awaited- if is_link:- # Neither a hit nor a helper the loop calls: its chain's sink- # speaks for it.- pass- elif kind is not None:- if loop_depth >= 1 and chain_awaited and line in awaits_allowed:- pass- elif loop_depth >= 1:- # The loop's key may feed any link, so the chain's facts are- # read off its outermost call, which holds them all.- facts = loop_facts(chain_top, sink=True, per_call=True)- call = _sink_call_text(- chain_top, dialect.callee_method_name(chain_top) or "", dialect- )- if chain_top is not call_node and not _quotable_chain(call):- call = _sink_call_text(call_node, method, dialect)- hits.append(- PerfHit(- "io_in_loop", line, next_func, kind,- func_start=next_start, loop=facts, sink_call=call,- )- )- if do_serial_await and chain_awaited:- # An *awaited* sink in a loop body is additionally a- # missed-concurrency candidate (a serial round-trip that- # a ``gather`` / ``Promise.all`` could fan out). Advisory- # co-signal alongside ``io_in_loop``; ``detail`` carries- # the boundary kind for the finding.- hits.append(- PerfHit(- "serial_await_in_loop", line, next_func, kind,- func_start=next_start, loop=facts, sink_call=call,- )- )- if do_nested_io and loop_depth >= 2 and outer_iter:- # A sink in the inner body of a NESTED loop -> O(n·m)- # round-trips. Nesting raises confidence, so it ships- # un-gated alongside ``io_in_loop`` — but only when the- # OUTER loop iterates a collection (``outer_iter``): a- # ``while`` pagination cursor wrapping an inner ``for ...- # of chunk`` is ``io_in_loop``, not a nested explosion- # (Phase-7c TS while-cursor FP).- hits.append(- PerfHit(- "nested_loop_with_io", line, next_func, kind,- func_start=next_start, loop=facts, sink_call=call,- )- )- else:- # A sink at loop_depth 0 makes this function a reachability- # target: a loop elsewhere calling into it runs the sink N- # times (cross-function N+1).- _lt, _kt, sink_slot, misc = _acc(next_start, next_func)- if sink_slot[0] is None:- sink_slot[0] = kind- if (- do_hot_path- and not awaited- and misc[1] is None- and kind in _HOT_PATH_SINK_KINDS- and method not in dialect.hot_path_excluded_methods- and not _in_deferred_body(call_node, dialect, scope_kinds)- ):- # An inherently-blocking (non-awaited subprocess / fs /- # sync-network) sink outside any loop. Noisy everywhere,- # so record it as a fact; the engine emits- # ``hot_path_sync_io`` only for a hot, central function- # (centrality gate). ``db`` is excluded — see- # ``_HOT_PATH_SINK_KINDS``; point-sized reads/writes are- # excluded per-dialect — see ``hot_path_excluded_methods``- # (outside a loop their cost is bounded, so they belong to- # the loop markers only).- misc[1] = kind- misc[2] = line- if do_lock_io and lock_depth >= 1:- # A sink reached while a block-scoped lock is held: the- # round-trip runs under the lock, serializing every thread.- hits.append(- PerfHit(- "blocking_io_under_lock", line, next_func, kind, func_start=next_start- )- )- else:- if loop_depth >= 1:- marker = (- dialect.loop_call_marker(root_name, method, call_node, list_names)- if do_loop_call_marker- else None- )- if marker == "lock_in_loop" and _is_lock_acquire_spin(- call_node, next_func, dialect, loop_kinds, fn_kinds- ):- marker = None- if marker is not None:- hits.append(- PerfHit(- marker, line, next_func, "", func_start=next_start,- loop=loop_facts(call_node, sink=False),- # ``arr.includes(x)``: the list is the receiver.- subject=root_name if marker == _MEMBERSHIP else "",- )- )- elif method and not (awaited and line in awaits_allowed):- # A loop-nested call to a non-sink helper: a candidate- # entry for cross-function reachability (PR4). Keep the- # first line we see the helper called at, for the finding.- targets = _acc(next_start, next_func)[0]- if method not in targets:- targets[method] = line- if loop_line:- call_loop_lines.setdefault(next_start, {})[line] = loop_line- facts = loop_facts(call_node, sink=False, per_call=True)- # Same innermost loop ``loop_facts`` just read. A- # keyed call clears ``key_unused`` for the whole- # line, even when its own facts are all default.- loops = _enclosing_loops(- call_node, dialect, loop_kinds, fn_kinds, do_block_loop- )- this_keyed = bool(loops) and _loop_key_feeds(call_node, loops[0])- if this_keyed:- keyed_lines.setdefault(next_start, set()).add(line)- merged = _call_line_facts(- call_facts.get(next_start, {}).get(line),- facts,- this_keyed=this_keyed,- line_keyed=line in keyed_lines.get(next_start, ()),- )- bucket = call_facts.get(next_start)- if merged is None:- if bucket is not None:- bucket.pop(line, None)- elif bucket is None:- call_facts[next_start] = {line: merged}- else:- bucket[line] = merged- if 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.- lock_targets = _acc(next_start, next_func)[1]- if method not in lock_targets:- lock_targets[method] = line- if do_blocking and in_async and not awaited:- api = dialect.blocking_sync_api(root_name, method)- if api is not None and not dialect.task_already_complete(call_node):- hits.append(- PerfHit(- "blocking_sync_in_async", line, next_func, api, func_start=next_start- )- )- else:- if do_blocking and in_async:- # A non-call member read that blocks in async (C# ``task.Result``).- mem = dialect.async_blocking_member(node)- if mem is not None and not dialect.task_already_complete(node):- hits.append(- PerfHit(- "blocking_sync_in_async",- node.start_point[0] + 1,- next_func,- mem,- func_start=next_start,- )- )- if loop_depth >= 1:- if do_string_concat and dialect.is_string_concat(node):- hits.append(- PerfHit(- "string_concat_in_loop",- node.start_point[0] + 1,- next_func,- "",- func_start=next_start,- loop=loop_facts(node, sink=False),- subject=_concat_target(node),- )- )- elif do_loop_stmt_marker:- sm = dialect.loop_stmt_marker(node, list_names)- if sm == "lock_in_loop" and _is_lock_acquire_spin(- node, next_func, dialect, loop_kinds, fn_kinds- ):- sm = None- if sm is not None:- hits.append(- PerfHit(- sm,- node.start_point[0] + 1,- next_func,- "",- func_start=next_start,- loop=loop_facts(node, sink=False),- # ``x in big``: the list is the last operand.- subject=(- _subject_text(next(iter(reversed(node.named_children)), None))- if sm == _MEMBERSHIP- else ""- ),- )- )-- if loop_line and len(hits) > stamp_from:- hits[stamp_from:] = [replace(h, loop_line=loop_line) for h in hits[stamp_from:]]-- # A block-scoped lock (``lock``/``synchronized``) opens a held region.- # Only its BLOCK body runs with the lock held — a sink in the lock-object- # expression (``synchronized(repo.find(id)){…}``) runs before the lock is- # taken — so ``lock_depth`` is raised per-child, for the body block only.- entering_lock = (- do_lock_io and dialect.is_lock_scope(node) and not _is_memoizing_lock(node)++ def _sink_chain(self, call: _Call) -> tuple[Node, bool] | None:+ """``(chain_top, chain_awaited)`` for the sink a chain ends at; ``None`` for a link.++ The innermost sink of a chain is the hit. Each outer link is met first+ and leaves the chain's outermost call and whether any link is awaited.+ """+ call_node = call.node+ inner = None+ for link in _receiver_chain_calls(call_node, self.call_kinds, self.access_kinds):+ if self.probe(link) is not None:+ inner = link+ if inner is not None:+ top, link_awaited = self.chain_of.get(inner.id, (call_node, False))+ self.chain_of[inner.id] = (top, link_awaited or call.awaited)+ return None+ chain_top, link_awaited = self.chain_of.pop(call_node.id, (call_node, False))+ return chain_top, call.awaited or link_awaited++ def _on_sink(+ self, call: _Call, kind: str, chain: tuple[Node, bool], cur: _Frame, nxt: _Frame+ ) -> None:+ """*chain* is ``(chain_top, chain_awaited)`` from :meth:`_sink_chain`."""+ if cur.loop_depth >= 1:+ if not (chain[1] and call.line in self.awaits_allowed):+ self._loop_sink_hits(call, kind, chain, cur, nxt)+ else:+ self._bare_sink_facts(call, kind, nxt)+ if self.do_lock_io and cur.lock_depth >= 1:+ # The round-trip runs under a held lock, serializing every thread.+ self.hits.append(+ PerfHit("blocking_io_under_lock", call.line, nxt.func, kind, func_start=nxt.start))- if is_loop:- # The outermost loop in a nest fixes ``outer_iter``; deeper loops- # inherit it. Set when entering the first loop (loop_depth 0 -> 1).- next_outer_iter = outer_iter if loop_depth >= 1 else dialect.is_iteration_loop(node)- # Only the loop BODY runs per-iteration; the ``for x in <iterable>``- # header / ``while <cond>`` condition runs once. For a block-- # iteration loop the body is the dialect-returned block node.- body = block_loop_body- if body is None:- body = dialect.loop_body(node)- header = node.start_point[0] + 1- if body is not None:- # NB: tree-sitter Node wrappers are not singletons, so compare- # with ``==`` (identity by tree + byte range), never ``is``.- for c in node.children:- inside = c == body- stack.append(- (- c,- next_loop_depth + 1 if inside else next_loop_depth,- next_async,- next_func,- next_start,- next_lock_depth,- next_outer_iter,- header if inside else next_loop_line,- )- )- else:- for c in node.children:- stack.append(- (- c,- next_loop_depth + 1,- next_async,- next_func,- next_start,- next_lock_depth,- next_outer_iter,- header,- )- )- elif entering_lock:- # Raise lock_depth for the body block only (not the lock-object expr).- for c in node.children:- cl = next_lock_depth + 1 if c.type in _LOCK_BODY_KINDS else next_lock_depth- stack.append(- (- c,- next_loop_depth,- next_async,- next_func,- next_start,- cl,- outer_iter,- next_loop_line,- )+ def _loop_sink_hits(+ self, call: _Call, kind: str, chain: tuple[Node, bool], cur: _Frame, nxt: _Frame+ ) -> None:+ """``io_in_loop`` and its co-signals for a sink in a loop body."""+ dialect = self.dialect+ chain_top, chain_awaited = chain+ # The loop's key may feed any link, so the chain's facts are read off+ # its outermost call, which holds them all.+ facts = self.loop_facts(chain_top, sink=True, per_call=True)+ sink_call = _sink_call_text(chain_top, dialect.callee_method_name(chain_top) or "", dialect)+ if chain_top is not call.node and not _quotable_chain(sink_call):+ sink_call = _sink_call_text(call.node, call.method, dialect)+ kinds = ["io_in_loop"]+ if self.do_serial_await and chain_awaited:+ # A serial round-trip a ``gather`` / ``Promise.all`` could fan out.+ kinds.append("serial_await_in_loop")+ if self.do_nested_io and cur.loop_depth >= 2 and cur.outer_iter:+ # O(n·m) round-trips, only when the outer loop iterates a collection:+ # a ``while`` pagination cursor around a chunk loop is not one.+ kinds.append("nested_loop_with_io")+ self.hits.extend(+ PerfHit(+ k, call.line, nxt.func, kind,+ func_start=nxt.start, loop=facts, sink_call=sink_call,+ )+ for k in kinds+ )++ def _bare_sink_facts(self, call: _Call, kind: str, nxt: _Frame) -> None:+ """A sink outside any loop makes its function a cross-function reachability target."""+ _lt, _kt, sink_slot, misc = self._acc(nxt.start, nxt.func)+ if sink_slot[0] is None:+ sink_slot[0] = kind+ dialect = self.dialect+ if (+ self.do_hot_path+ and not call.awaited+ and misc[1] is None+ and kind in _HOT_PATH_SINK_KINDS+ and call.method not in dialect.hot_path_excluded_methods+ and not _in_deferred_body(call.node, dialect, self.scope_kinds)+ ):+ # Inherently blocking and noisy everywhere, so a fact: the engine emits+ # ``hot_path_sync_io`` only for a hot, central function. ``db`` and+ # point-sized calls are out (``_HOT_PATH_SINK_KINDS``, ``hot_path_excluded_methods``).+ misc[1] = kind+ misc[2] = call.line++ def _on_helper_call(self, call: _Call, cur: _Frame, nxt: _Frame) -> None:+ """A non-sink call: a loop marker, or a reachability entry in a loop or under a lock."""+ if cur.loop_depth >= 1:+ marker = (+ self.dialect.loop_call_marker(+ call.root_name, call.method, call.node, self.list_names+ )+ if self.do_loop_call_marker+ else None+ )+ if marker == "lock_in_loop" and _is_lock_acquire_spin(+ call.node, nxt.func, self.dialect, self.loop_kinds, self.fn_kinds+ ):+ marker = None+ if marker is not None:+ self.hits.append(+ PerfHit(+ marker, call.line, nxt.func, "", func_start=nxt.start,+ loop=self.loop_facts(call.node, sink=False),+ # ``arr.includes(x)``: the list is the receiver.+ subject=call.root_name if marker == _MEMBERSHIP else "",)- else:- for c in node.children:- stack.append(- (- c,- next_loop_depth,- next_async,- next_func,- next_start,- next_lock_depth,- outer_iter,- next_loop_line,- )+ )+ elif call.method and not (call.awaited and call.line in self.awaits_allowed):+ self._record_loop_call(call, cur, nxt)+ if self.do_lock_io and cur.lock_depth >= 1 and call.method:+ lock_targets = self._acc(nxt.start, nxt.func)[1]+ if call.method not in lock_targets:+ lock_targets[call.method] = call.line++ def _record_loop_call(self, call: _Call, cur: _Frame, nxt: _Frame) -> None:+ """A loop-nested helper call, kept at the first line it is seen."""+ targets = self._acc(nxt.start, nxt.func)[0]+ if call.method in targets:+ return+ line, start = call.line, nxt.start+ targets[call.method] = line+ if cur.loop_line:+ self.call_loop_lines.setdefault(start, {})[line] = cur.loop_line+ facts = self.loop_facts(call.node, sink=False, per_call=True)+ # Same innermost loop ``loop_facts`` just read. A keyed call clears+ # ``key_unused`` for the whole line, even when its own facts are default.+ loops = _enclosing_loops(+ call.node, self.dialect, self.loop_kinds, self.fn_kinds, self.do_block_loop+ )+ this_keyed = bool(loops) and _loop_key_feeds(call.node, loops[0])+ if this_keyed:+ self.keyed_lines.setdefault(start, set()).add(line)+ merged = _call_line_facts(+ self.call_facts.get(start, {}).get(line),+ facts,+ this_keyed=this_keyed,+ line_keyed=line in self.keyed_lines.get(start, ()),+ )+ bucket = self.call_facts.get(start)+ if merged is None:+ if bucket is not None:+ bucket.pop(line, None)+ elif bucket is None:+ self.call_facts[start] = {line: merged}+ else:+ bucket[line] = merged++ def _on_statement(self, node: Node, cur: _Frame, nxt: _Frame) -> None:+ """Markers on a non-call node: a blocking member read, string concat, statement markers."""+ dialect = self.dialect+ if self.do_blocking and cur.in_async:+ # A non-call member read that blocks in async (C# ``task.Result``).+ mem = dialect.async_blocking_member(node)+ if mem is not None and not dialect.task_already_complete(node):+ self.hits.append(+ PerfHit(+ "blocking_sync_in_async",+ node.start_point[0] + 1,+ nxt.func,+ mem,+ func_start=nxt.start,)+ )+ if cur.loop_depth < 1:+ return+ if self.do_string_concat and dialect.is_string_concat(node):+ self.hits.append(+ PerfHit(+ "string_concat_in_loop",+ node.start_point[0] + 1,+ nxt.func,+ "",+ func_start=nxt.start,+ loop=self.loop_facts(node, sink=False),+ subject=_concat_target(node),+ )+ )+ elif self.do_loop_stmt_marker:+ self._on_loop_statement(node, nxt)++ def _on_loop_statement(self, node: Node, nxt: _Frame) -> None:+ sm = self.dialect.loop_stmt_marker(node, self.list_names)+ if sm == "lock_in_loop" and _is_lock_acquire_spin(+ node, nxt.func, self.dialect, self.loop_kinds, self.fn_kinds+ ):+ sm = None+ if sm is None:+ return+ self.hits.append(+ PerfHit(+ sm,+ node.start_point[0] + 1,+ nxt.func,+ "",+ func_start=nxt.start,+ loop=self.loop_facts(node, sink=False),+ # ``x in big``: the list is the last operand.+ subject=(+ _subject_text(next(iter(reversed(node.named_children)), None))+ if sm == _MEMBERSHIP+ else ""+ ),+ )+ )- # One finding per (kind, line, function), keeping a call the loop's key- # reaches when one of them does. A chain of sinks is one hit already- # (``chain_of`` above); what is left to collapse here is two hits of one- # kind that share a line without being one chain, such as two membership- # tests in one condition.- 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)- 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))+ def _is_lock_entry(self, node: Node) -> bool:+ """A block-scoped lock (``lock`` / ``synchronized``) whose body runs with it held.++ Only the body: the lock-object expression runs before the lock is taken.+ """+ return self.dialect.is_lock_scope(node) and not _is_memoizing_lock(node)++ def _push_loop_children(+ self,+ stack: list[tuple[Node, _Frame]],+ node: Node,+ cur: _Frame,+ nxt: _Frame,+ block_loop_body: Node | None,+ ) -> None:+ """Only the loop BODY runs per iteration; the header runs once."""+ # The outermost loop of a nest fixes ``outer_iter``; deeper loops inherit it.+ outer_iter = cur.outer_iter if cur.loop_depth >= 1 else self.dialect.is_iteration_loop(node)+ body = block_loop_body+ if body is None:+ body = self.dialect.loop_body(node)+ inside = nxt._replace(+ loop_depth=nxt.loop_depth + 1, outer_iter=outer_iter, loop_line=node.start_point[0] + 1+ )+ if body is None:+ stack.extend((c, inside) for c in node.children)+ return+ header = nxt._replace(outer_iter=outer_iter)+ # Node wrappers are not singletons: compare with ``==``, never ``is``.+ stack.extend((c, inside if c == body else header) for c in node.children)+ def _fn_facts(self) -> list[PerfFnFacts]:+ """One :class:`PerfFnFacts` per function with a call target, a sink or a fact."""fn_facts = [PerfFnFacts(function=name,@@ -1157,11 +1116,38 @@ class _PerfWalk:nested_loop_line=misc[0],blocking_sink_kind=misc[1],blocking_sink_line=misc[2],- loop_call_facts=tuple(sorted(call_facts.get(start, {}).items())),- loop_call_lines=tuple(sorted(call_loop_lines.get(start, {}).items())),+ loop_call_facts=tuple(sorted(self.call_facts.get(start, {}).items())),+ loop_call_lines=tuple(sorted(self.call_loop_lines.get(start, {}).items())),)- for start, (name, loop_targets, lock_targets, sink, misc) in fn_acc.items()+ for start, (name, loop_targets, lock_targets, sink, misc) in self.fn_acc.items()if (loop_targets or lock_targets or sink[0] is not None or misc[0] or misc[1] is not None)]fn_facts.sort(key=lambda f: f.func_start)- return deduped, io_names, fn_facts+ return fn_facts+++def _push_lock_children(stack: list[tuple[Node, _Frame]], node: Node, nxt: _Frame) -> None:+ """Raise the lock depth for the lock's body block only."""+ held = nxt._replace(lock_depth=nxt.lock_depth + 1)+ stack.extend([(c, held if c.type in _LOCK_BODY_KINDS else nxt) for c in node.children])+++def _dedupe(hits: list[PerfHit], lambda_spans: list[tuple[int, int, str]]) -> list[PerfHit]:+ """One finding per (kind, line, function), keeping a call the loop's key reaches.++ A chain of sinks is one hit already; what is left are two hits of one kind+ sharing a line without being one chain, such as two membership tests in one+ condition.+ """+ 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)+ 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))+ return deduped