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): say whether Pascal perf loops grow with the data (#3517)
Laentir Valetovc894527fb826
diff --git a/docs/layers/PERFORMANCE.md b/docs/layers/PERFORMANCE.mdindex 3d4c277fc..e0eab2e1f 100644--- a/docs/layers/PERFORMANCE.md+++ b/docs/layers/PERFORMANCE.md@@ -76,7 +76,7 @@ graph and the import graph. An opportunity with a supported shared fix links toEach loop also says what it walks: rows a query returns, a collection its callerpasses in, the rows of one key, a registry or configuration table, a fixed set, adirectory listing or a stream, and plan steps name it. Python, TypeScript/JavaScript,-Go, Java, C#, Ruby, Rust, Kotlin and C++ loops say whether they grow with the data or are bounded; a+Go, Java, C#, Ruby, Rust, Kotlin, C++ and Pascal loops say whether they grow with the data or are bounded; aloop whose size the code does not show stays unknown and is never treated asbounded. A loop that stops at the first call that answers, or feeds each call'sresult into the next, gets no batch advice (reason `loop_not_batchable`).diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/pascal.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/pascal.pyindex 5083bca58..515a2bbec 100644--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/pascal.py+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/pascal.py@@ -42,17 +42,30 @@ a ``+=`` compound assignment (``{$COPERATORS ON}`` is off by default), whichthe generic ``is_string_concat`` predicate cannot express without riskingfalse positives on ordinary reassignment -- ``string_concat_in_loop`` istherefore also left out rather than guessed.++What a loop walks comes from the shared reader in ``iterables.py``. Two shapes+are Pascal's own. A counted loop has no ``i < n`` condition: its inclusive+bound is the ``end`` value (``High(xs)``, ``xs.Count - 1``), or the start+value under ``downto``. A data set is opened in place rather than bound to a+query's result, so ``while not Q.Eof`` walks what the ``Q.Open`` before the+loop read, and the statement text assigned to ``Q.SQL`` before it is what+tells one row from all of them. Identifiers are case-insensitive, so names and+method vocabularies are compared lower-cased."""from __future__ import annotations+import refrom typing import TYPE_CHECKING-from .base import BasePerfDialect+from .iterables import IterableSources, text_ofif TYPE_CHECKING:from tree_sitter import Node+ from ..loop_facts import SinkProbe+ from .iterables import Source+# Filesystem round-trips. Bare RTL functions (SysUtils / System.IOUtils /# WinAPI) with no receiver, so they fire regardless of ``is_attribute``._FS_BARE: frozenset[str] = frozenset(@@ -125,8 +138,28 @@ _DB_METHODS: frozenset[str] = frozenset({"Open", "ExecSQL", "Post"})# ``uses``-clause network evidence for the same reason._NETWORK_METHODS: frozenset[str] = frozenset({"Get", "Post", "Put", "Patch", "Delete", "Head"})+# Keys a data set's ``Open`` and its statement text are filed under in the+# scope's bindings. No identifier holds a NUL, so they never meet a real name.+_OPEN_KEY = b"\x00open"+_SQL_KEY = b"\x00sql"+# ``Inc(n)`` / ``SetLength(xs, n)``: procedures that step or resize their first argument.+_STEPPERS: frozenset[str] = frozenset({"inc", "dec", "setlength", "include", "exclude"})+# ``TColor``: a type, by the convention every Delphi and FPC unit follows.+_TYPE_NAME = re.compile(r"T[A-Z]\w*\Z")+++def _lower(node: Node | None) -> bytes:+ return (node.text or b"").lower() if node is not None else b""+++class _Names(set[bytes]):+ """Names matched without case."""-class PascalPerfDialect(BasePerfDialect):+ def __contains__(self, name: object) -> bool:+ return isinstance(name, bytes) and super().__contains__(name.lower())+++class PascalPerfDialect(IterableSources):language = "pascal"markers = frozenset({"io_in_loop", "hot_path_sync_io"})@@ -223,5 +256,210 @@ class PascalPerfDialect(BasePerfDialect):return "network"return None+ # -- what a loop walks (perf/dialects/iterables.py) ---------------------------++ function_kinds = frozenset({"defProc"})+ change_kinds = frozenset({"assignment"})+ grow_methods = frozenset(+ {"add", "addobject", "addrange", "addstrings", "insert", "append", "push", "enqueue"}+ )+ conditional_kinds = frozenset(+ {+ "if", "ifElse", "case", "caseCase", "for", "foreach", "while", "repeat",+ "try", "exceptionHandler",+ }+ ) # fmt: skip++ literal_kinds = frozenset({"exprBrackets"})+ int_kinds = frozenset({"literalNumber"})+ wrapper_kinds = frozenset({"exprParens"})+ member_kinds = frozenset({"exprDot"})+ pass_through = frozenset({"keys", "values", "toarray"})+ size_names = frozenset({"count", "length", "high"})+ listings = frozenset({"getfiles", "getdirectories", "getfilesystementries"})+ recursive_marks = (b"soAllDirectories",)++ def loop_header(self, loop: Node) -> tuple[str, Node | None]:+ if loop.type == "foreach":+ return "each", loop.child_by_field_name("iterable")+ if loop.type == "for":+ if any(child.type == "kDownto" for child in loop.children):+ start = loop.child_by_field_name("start")+ return "counted", start.child_by_field_name("rhs") if start is not None else None+ return "counted", loop.child_by_field_name("end")+ dataset = self._eof_dataset(loop)+ if dataset is None:+ return "each", None+ # Unopened here, the data set is whatever its receiver is (a parameter).+ return "each", self.reaching_value(loop, _lower(dataset) + _OPEN_KEY) or dataset++ def _eof_dataset(self, loop: Node) -> Node | None:+ """``Q`` of ``while not Q.Eof`` / ``repeat ... until Q.Eof``."""+ cond = loop.child_by_field_name("condition")+ if cond is None:+ return None+ cond = self._unwrap(cond)+ if loop.type == "while":+ operator = cond.child_by_field_name("operator")+ operand = cond.child_by_field_name("operand")+ if cond.type != "exprUnary" or operator is None or operator.type != "kNot":+ return None+ if operand is None:+ return None+ cond = self._unwrap(operand)+ if cond.type == "exprDot" and _lower(cond.child_by_field_name("rhs")) == b"eof":+ return cond.child_by_field_name("lhs")+ return None++ def call_parts(self, node: Node) -> tuple[str, Node | None, list[Node]] | None:+ if node.type == "exprCall":+ args = node.child_by_field_name("args")+ arg_nodes = (+ [a for a in args.named_children if a.type != "comment"] if args is not None else []+ )+ elif (+ node.parent is not None+ and node.parent.type == "statement"+ and self.bare_statement_call(node.parent) == node+ ):+ arg_nodes = []+ else:+ return None+ entity = self._entity_of(node)+ if entity is None:+ return None+ if entity.type == "exprDot":+ receiver = entity.child_by_field_name("lhs")+ name = entity.child_by_field_name("rhs")+ return (+ (_lower(name).decode("utf-8", "replace"), receiver, arg_nodes)+ if name is not None+ else None+ )+ if entity.type != "identifier":+ return None+ return _lower(entity).decode("utf-8", "replace"), None, arg_nodes++ def member_parts(self, node: Node) -> tuple[str, Node] | None:+ if node.type != "exprDot":+ return None+ obj, name = node.child_by_field_name("lhs"), node.child_by_field_name("rhs")+ if obj is None or name is None:+ return None+ return _lower(name).decode("utf-8", "replace"), obj++ @staticmethod+ def _sql_owner(target: Node | None) -> Node | None:+ """``Q`` of ``Q.SQL.Text`` / ``Q.CommandText``: the data set a statement text is for."""+ if target is None or target.type != "exprDot":+ return None+ owner, name = target.child_by_field_name("lhs"), _lower(target.child_by_field_name("rhs"))+ if name == b"commandtext":+ return owner+ if name != b"text" or owner is None or owner.type != "exprDot":+ return None+ if _lower(owner.child_by_field_name("rhs")) != b"sql":+ return None+ return owner.child_by_field_name("lhs")++ def bindings_of(self, node: Node) -> list[tuple[bytes, Node]]:+ if node.type == "assignment":+ lhs, rhs = node.child_by_field_name("lhs"), node.child_by_field_name("rhs")+ if lhs is None or rhs is None:+ return []+ if lhs.type == "identifier":+ return [(_lower(lhs), rhs)]+ dataset = self._sql_owner(lhs)+ return [(_lower(dataset) + _SQL_KEY, rhs)] if dataset is not None else []+ parts = self.call_parts(node)+ if parts is None:+ return []+ method, receiver, args = parts+ if receiver is None:+ return []+ if method == "open":+ return [(_lower(receiver) + _OPEN_KEY, node)]+ # ``Q.SQL.Add('...')``: one line of the text; several leave it unread.+ if method != "add" or len(args) != 1 or receiver.type != "exprDot":+ return []+ if _lower(receiver.child_by_field_name("rhs")) != b"sql":+ return []+ return [(_lower(receiver.child_by_field_name("lhs")) + _SQL_KEY, args[0])]++ def mutated_name(self, node: Node) -> bytes | None:+ if node.type in self.change_kinds:+ return _lower(node.child_by_field_name("lhs")).split(b"[", 1)[0] or None+ parts = self.call_parts(node)+ if parts is None:+ return None+ method, receiver, args = parts+ if receiver is not None:+ return _lower(receiver) if method in self.grow_methods else None+ return _lower(args[0]) if method in _STEPPERS and args else None++ def bindings_before(self, anchor: Node, name: bytes) -> list[tuple[Node, Node]]:+ return super().bindings_before(anchor, name.lower())++ def _changed_before(self, value: Node, loop: Node, name: bytes) -> bool:+ return super()._changed_before(value, loop, name.lower())++ def param_names(self, loop: Node) -> set[bytes]:+ scope = self._scope(loop)+ header = scope.child_by_field_name("header") if scope is not None else None+ args = header.child_by_field_name("args") if header is not None else None+ names = _Names()+ for arg in args.named_children if args is not None else ():+ if arg.type == "declArg":+ names.update(+ _lower(n) for n in arg.children_by_field_name("name") if n.type == "identifier"+ )+ return names++ def special_call(+ self,+ call: Node,+ parts: tuple[str, Node | None, list[Node]],+ loop: Node,+ probe: SinkProbe,+ ) -> Source | None:+ method, receiver, args = parts+ if method != "open" or receiver is None or probe(call) != "db":+ return None+ sql = args[0] if args else self.reaching_value(loop, _lower(receiver) + _SQL_KEY)+ if sql is not None and sql.type == "identifier":+ sql = self.reaching_value(loop, _lower(sql))+ # A text set in a form file or built up line by line is not in view,+ # and a read whose text is unseen may be one row as easily as all of them.+ if sql is None or not any(n.type == "literalString" for n in self._walk(sql)):+ return "query", None+ return self._query(sql, loop)++ def _name(self, name: Node, loop: Node, probe: SinkProbe, *, hop: bool) -> Source:+ found = super()._name(name, loop, probe, hop=hop)+ # ``for E in TColor`` / ``High(TColor)``: an ordinal type has a fixed set of values.+ if found == ("unknown", None) and _TYPE_NAME.match(text_of(name)):+ return "constant", "bounded"+ return found++ def _counted(self, stop: Node, loop: Node, probe: SinkProbe, *, hop: bool = True) -> Source:+ return super()._counted(self._size_of_last_index(stop), loop, probe, hop=hop)++ def _size_of_last_index(self, node: Node) -> Node:+ """``Length(xs)`` of ``Length(xs) - 1`` / ``Pred(Length(xs))``: an inclusive+ bound is written one short of the size it walks."""+ node = self._unwrap(node)+ if node.type == "exprBinary":+ operator = node.child_by_field_name("operator")+ lhs, rhs = node.child_by_field_name("lhs"), node.child_by_field_name("rhs")+ if operator is None or operator.type != "kSub" or lhs is None or rhs is None:+ return node+ if self._unwrap(rhs).type in self.int_kinds:+ return self._size_of_last_index(lhs)+ return node+ parts = self.call_parts(node)+ if parts is not None and parts[0] == "pred" and parts[1] is None and len(parts[2]) == 1:+ return self._size_of_last_index(parts[2][0])+ return node+DIALECT = PascalPerfDialect()diff --git a/tests/unit/health/test_perf_loop_sources_dialects.py b/tests/unit/health/test_perf_loop_sources_dialects.pyindex 18482bf98..f3a48ce14 100644--- a/tests/unit/health/test_perf_loop_sources_dialects.py+++ b/tests/unit/health/test_perf_loop_sources_dialects.py@@ -1,4 +1,4 @@-"""What a perf loop walks, and the magnitude that proves, in Go, Java, C# and Ruby.+"""What a perf loop walks, and the magnitude that proves, in Go, Java, C#, Pascal and Ruby.Through the real walker. Each shape that grows or is bounded is paired with onethe syntax does not settle, which must stay ``unknown``: no reader takes@@ -136,6 +136,164 @@ def test_csharp_loop_sources(line, expected):assert _loops("A.cs", "csharp", _CS)[line] == expected+_PASCAL = """unit Orders;+interface+uses FireDAC.Comp.Client;+procedure Sync(Q: TFDQuery; const Ids: array of Integer; Flag: Boolean);+implementation+procedure Sync(Q: TFDQuery; const Ids: array of Integer; Flag: Boolean);+var+ I, N: Integer;+begin+ Q.SQL.Text := 'SELECT id FROM orders';+ Q.Open;+ while not Q.Eof do+ begin+ Q.ExecSQL;+ Q.Next;+ end;+ for I := 0 to High(Ids) do+ Q.ExecSQL;+ N := 3;+ for I := 1 to N do+ Q.ExecSQL;+ if Flag then+ N := Length(Ids);+ for I := 1 to N do+ Q.ExecSQL;+end;+end.+"""+++ ("line", "expected"),+ [+ (14, ("grows_with_data", "query")), # a data set opened on an uncapped SELECT+ (18, ("unknown", "parameter")),+ (21, ("bounded", "constant")),+ (25, ("unknown", "unknown")), # N rebound under a branch before the loop+ ],+)+def test_pascal_loop_sources(line, expected):+ assert _loops("Orders.pas", "pascal", _PASCAL)[line] == expected+++_PASCAL_SHAPES = """unit Shapes;+interface+uses FireDAC.Comp.Client, System.IOUtils;+implementation+procedure Run(D: TFDQuery; L: TStringList; RepoRoot, Dir: string; Flag: Boolean);+var+ One, Top, Form, Lines, Branch, Arg, Rep: TFDQuery;+ S: string; I, N, M, K: Integer; E: TColor; Names: TArray<string>;+begin+ One.SQL.Text := 'SELECT * FROM t WHERE id = :id';+ One.Open;+ while not One.Eof do begin D.ExecSQL; One.Next; end;+ Top.SQL.Text := 'SELECT * FROM t LIMIT 10';+ Top.Open;+ while not Top.Eof do begin D.ExecSQL; Top.Next; end;+ while not D.Eof do begin D.ExecSQL; D.Next; end;+ Form.Open;+ while not Form.Eof do begin D.ExecSQL; Form.Next; end;+ Lines.SQL.Add('SELECT id');+ Lines.SQL.Add('FROM orders');+ Lines.Open;+ while not Lines.Eof do begin D.ExecSQL; Lines.Next; end;+ if Flag then Branch.Open;+ while not Branch.Eof do begin D.ExecSQL; Branch.Next; end;+ Arg.Open('SELECT id FROM orders');+ while not (Arg.Eof) do begin D.ExecSQL; Arg.Next; end;+ Rep.CommandText := 'SELECT id FROM orders';+ Rep.Open();+ repeat D.ExecSQL; Rep.Next; until Rep.Eof;+ for S in L do D.ExecSQL;+ for I in [1, 2, 3] do D.ExecSQL;+ for E in TColor do D.ExecSQL;+ for E := Low(TColor) to High(TColor) do D.ExecSQL;+ for I := 0 to L.Count - 1 do D.ExecSQL;+ for I := L.Count - 1 downto 0 do D.ExecSQL;+ n := 3;+ for I := 1 to N do D.ExecSQL;+ M := 3;+ Inc(M);+ for I := 1 to M do D.ExecSQL;+ for I := 1 to K do D.ExecSQL;+ Names := ['a', 'b'];+ for S in Names do D.ExecSQL;+ for S in TDirectory.GetFiles(RepoRoot, '*.pas', TSearchOption.soAllDirectories) do D.ExecSQL;+ for S in TDirectory.GetFiles(Dir) do D.ExecSQL;+end;+end.+"""+++ ("line", "expected"),+ [+ (12, ("bounded", "caller_key")),+ (15, ("unknown", "query")), # a capped read is as large as its cap+ (16, ("unknown", "parameter")), # a data set the caller opened+ # The statement text is in a form file, or added line by line: unread.+ (18, ("unknown", "query")),+ (22, ("unknown", "query")),+ (24, ("unknown", "unknown")), # an Open under a branch proves nothing+ (26, ("grows_with_data", "query")), # Open('SELECT ...')+ (29, ("grows_with_data", "query")), # repeat ... until Rep.Eof+ (30, ("unknown", "parameter")),+ (31, ("bounded", "constant")),+ (32, ("bounded", "constant")), # the values of an enumerated type+ (33, ("bounded", "constant")),+ (34, ("unknown", "parameter")), # Count - 1: the inclusive bound of L+ (35, ("unknown", "parameter")), # downto: the size is the start value+ (37, ("bounded", "constant")), # n and N are one name+ (40, ("unknown", "unknown")), # stepped after its literal+ (41, ("unknown", "unknown")), # a local never bound+ (43, ("bounded", "constant")),+ (44, ("grows_with_data", "listing")),+ (45, ("unknown", "listing")),+ ],+)+def test_pascal_data_sets_and_counted_loops(line, expected):+ assert _loops("Shapes.pas", "pascal", _PASCAL_SHAPES)[line] == expected+++_PASCAL_CASE = """unit Names;+interface+uses FireDAC.Comp.Client;+implementation+procedure F(Q: TFDQuery; n: Integer; items: TStringList);+var I: Integer; E: TColor; S: string; total, tmp: Integer;+begin+ for I := 1 to N do Q.ExecSQL;+ for I := 0 to Items.Count - 1 do Q.ExecSQL;+ for S in ITEMS do Q.ExecSQL;+ for E in tcolor do Q.ExecSQL;+ for I := 1 to High(tmp) do Q.ExecSQL;+ for I := 1 to total do Q.ExecSQL;+end;+end.+"""+++ ("line", "expected"),+ [+ (8, ("unknown", "parameter")), # N is the parameter n+ (9, ("unknown", "parameter")),+ (10, ("unknown", "parameter")),+ # A type is told from a variable by its spelling alone, so a lower-cased+ # one reads like ``tmp`` and ``total`` and bounds nothing.+ (11, ("unknown", "unknown")),+ (12, ("unknown", "unknown")),+ (13, ("unknown", "unknown")),+ ],+)+def test_pascal_names_match_without_case_but_a_type_needs_its_spelling(line, expected):+ assert _loops("Names.pas", "pascal", _PASCAL_CASE)[line] == expected++_RUBY = """require "active_record"def f(ids)users = User.where(active: true)