Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

perf(extract): eliminate O(n^2) AST traversal on wide-flat files

Martin Vogel01bdba2a3f12
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.c
index 8f69b68ea..942e306ff 100644
--- a/internal/cbm/extract_calls.c
+++ b/internal/cbm/extract_calls.c
@@ -428,11 +428,7 @@ static void walk_calls(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec)
}
}
- uint32_t count = ts_node_child_count(node);
- enum { LAST_IDX_OFFSET = 1 };
- for (int i = (int)(count - LAST_IDX_OFFSET); i >= 0; i--) {
- ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
- }
+ ts_nstack_push_children(&stack, ctx->arena, node);
}
}
diff --git a/internal/cbm/extract_channels.c b/internal/cbm/extract_channels.c
index 2c20838b8..5b9f421de 100644
--- a/internal/cbm/extract_channels.c
+++ b/internal/cbm/extract_channels.c
@@ -128,10 +128,7 @@ static void scan_string_consts_js(CBMExtractCtx *ctx, chan_const_table_t *tbl) {
}
}
- uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
- ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
- }
+ ts_nstack_push_children(&stack, ctx->arena, node);
}
}
@@ -384,10 +381,7 @@ static void extract_channels_js(CBMExtractCtx *ctx) {
if (strcmp(ts_node_type(node), "call_expression") == 0) {
js_process_call(ctx, node, &consts);
}
- uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
- ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
- }
+ ts_nstack_push_children(&stack, ctx->arena, node);
}
}
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index 38da65ef1..a70502d7f 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -3218,7 +3218,9 @@ static void walk_variables_iter(CBMExtractCtx *ctx, TSNode root, const CBMLangSp
continue;
}
if (cbm_kind_in_set(child, spec->variable_node_types)) {
- if (cbm_is_module_level(child, ctx->language)) {
+ /* parent of `child` is `node` (we're iterating its children) —
+ * pass it directly to avoid the O(n) ts_node_parent rescan. */
+ if (cbm_is_module_level_p(node, ctx->language)) {
extract_var_names(ctx, child, spec);
}
}
@@ -3247,40 +3249,49 @@ static void extract_variables(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec
return;
}
- uint32_t count = ts_node_child_count(root);
- for (uint32_t i = 0; i < count; i++) {
- TSNode child = ts_node_child(root, i);
- if (ts_node_is_null(child)) {
- continue;
- }
-
- if (!cbm_is_module_level(child, ctx->language)) {
- continue;
- }
+ // `root` is the file's root node (the sole caller passes ctx->root), so it
+ // is the parent of every top-level child and the module-level check is
+ // invariant across the loop — hoist it out (and it's now O(1) via _p).
+ // NB: this short-circuit relies on `root` always being the file root; if a
+ // future caller passes a non-root node, restore the per-child check.
+ if (!cbm_is_module_level_p(root, ctx->language)) {
+ return;
+ }
- if (cbm_kind_in_set(child, spec->variable_node_types)) {
- extract_var_names(ctx, child, spec);
- continue;
- }
+ // Iterate top-level children via a cursor (O(n) total) instead of
+ // ts_node_child(root, i) per index, which is O(i) each → O(n²) on files
+ // with tens of thousands of top-level statements (e.g. generated/fixture
+ // files like TS's reallyLargeFile.ts, 583K lines).
+ TSTreeCursor cursor = ts_tree_cursor_new(root);
+ if (ts_tree_cursor_goto_first_child(&cursor)) {
+ do {
+ TSNode child = ts_tree_cursor_current_node(&cursor);
- // Unwrap wrapper nodes: expression_statement, export_statement, statement
- const char *ck = ts_node_type(child);
- if (strcmp(ck, "expression_statement") == 0 || strcmp(ck, "export_statement") == 0 ||
- strcmp(ck, "statement") == 0) {
- // Check inner named children for variable types
- uint32_t nc = ts_node_named_child_count(child);
- for (uint32_t j = 0; j < nc; j++) {
- TSNode inner = ts_node_named_child(child, j);
- if (cbm_kind_in_set(inner, spec->variable_node_types)) {
- extract_var_names(ctx, inner, spec);
- }
- }
- // Also check if the wrapper itself is a variable type (e.g., PHP expression_statement)
if (cbm_kind_in_set(child, spec->variable_node_types)) {
extract_var_names(ctx, child, spec);
+ continue;
}
- }
+
+ // Unwrap wrapper nodes: expression_statement, export_statement, statement
+ const char *ck = ts_node_type(child);
+ if (strcmp(ck, "expression_statement") == 0 || strcmp(ck, "export_statement") == 0 ||
+ strcmp(ck, "statement") == 0) {
+ // Check inner named children for variable types
+ uint32_t nc = ts_node_named_child_count(child);
+ for (uint32_t j = 0; j < nc; j++) {
+ TSNode inner = ts_node_named_child(child, j);
+ if (cbm_kind_in_set(inner, spec->variable_node_types)) {
+ extract_var_names(ctx, inner, spec);
+ }
+ }
+ // Also check if the wrapper itself is a variable type (e.g., PHP expression_statement)
+ if (cbm_kind_in_set(child, spec->variable_node_types)) {
+ extract_var_names(ctx, child, spec);
+ }
+ }
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
}
+ ts_tree_cursor_delete(&cursor);
}
// Extract typed struct/class fields for cross-file LSP resolution (C/C++/CUDA/Go/Java/Rust etc.)
diff --git a/internal/cbm/extract_env_accesses.c b/internal/cbm/extract_env_accesses.c
index 9a12411a5..ab0fc8f96 100644
--- a/internal/cbm/extract_env_accesses.c
+++ b/internal/cbm/extract_env_accesses.c
@@ -163,11 +163,7 @@ static void walk_env_accesses(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec
continue; // don't push children (avoid double-counting)
}
- uint32_t count = ts_node_child_count(node);
- enum { LAST_IDX = 1 };
- for (int i = (int)count - LAST_IDX; i >= 0; i--) {
- ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
- }
+ ts_nstack_push_children(&stack, ctx->arena, node);
}
}
diff --git a/internal/cbm/extract_imports.c b/internal/cbm/extract_imports.c
index c4e3112d8..ea286e4ab 100644
--- a/internal/cbm/extract_imports.c
+++ b/internal/cbm/extract_imports.c
@@ -436,10 +436,7 @@ static void walk_es_imports(CBMExtractCtx *ctx, TSNode root) {
}
if (push_children) {
- uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
- ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
- }
+ ts_nstack_push_children(&stack, ctx->arena, node);
}
}
}
@@ -836,10 +833,7 @@ static void walk_wolfram_imports(CBMExtractCtx *ctx, TSNode root) {
process_wolfram_needs(ctx, node);
}
- uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
- ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
- }
+ ts_nstack_push_children(&stack, ctx->arena, node);
}
}
diff --git a/internal/cbm/extract_node_stack.h b/internal/cbm/extract_node_stack.h
index bb7b183b4..fd70cb95c 100644
--- a/internal/cbm/extract_node_stack.h
+++ b/internal/cbm/extract_node_stack.h
@@ -50,4 +50,36 @@ static inline TSNode ts_nstack_pop(TSNodeStack *s) {
return s->items[--s->count];
}
+/*
+ * Push all children of `node` so they POP in forward (source) order — a drop-in
+ * replacement for the common idiom:
+ * for (int i = (int)count - 1; i >= 0; i--) ts_nstack_push(s, a, ts_node_child(node, i));
+ *
+ * That idiom calls ts_node_child(node, i) once per index, and ts_node_child is
+ * O(i) in tree-sitter (it walks the child iterator from the first child each
+ * time). Over a node with N children that is O(N^2) — catastrophic on a program
+ * root holding hundreds of thousands of top-level nodes (e.g. fixture/generated
+ * files). This helper enumerates children in a single O(N) cursor pass, then
+ * reverses the just-pushed segment so pop order is identical to the old idiom.
+ */
+static inline void ts_nstack_push_children(TSNodeStack *s, CBMArena *arena, TSNode node) {
+ int base = s->count;
+ TSTreeCursor cursor = ts_tree_cursor_new(node);
+ if (ts_tree_cursor_goto_first_child(&cursor)) {
+ do {
+ ts_nstack_push(s, arena, ts_tree_cursor_current_node(&cursor));
+ } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ }
+ ts_tree_cursor_delete(&cursor);
+ /* Reverse [base, count) so the first child pops first (forward order). */
+ int lo = base, hi = s->count - 1;
+ while (lo < hi) {
+ TSNode tmp = s->items[lo];
+ s->items[lo] = s->items[hi];
+ s->items[hi] = tmp;
+ lo++;
+ hi--;
+ }
+}
+
#endif /* CBM_EXTRACT_NODE_STACK_H */
diff --git a/internal/cbm/extract_type_assigns.c b/internal/cbm/extract_type_assigns.c
index 74f306507..7830a8b18 100644
--- a/internal/cbm/extract_type_assigns.c
+++ b/internal/cbm/extract_type_assigns.c
@@ -191,11 +191,7 @@ static void walk_type_assigns(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec
while (stack.count > 0) {
TSNode node = ts_nstack_pop(&stack);
process_type_assign_node(ctx, node, spec, cbm_enclosing_func_qn_cached(ctx, node));
- enum { LAST_IDX = 1 };
- uint32_t count = ts_node_child_count(node);
- for (int i = (int)count - LAST_IDX; i >= 0; i--) {
- ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i));
- }
+ ts_nstack_push_children(&stack, ctx->arena, node);
}
}
diff --git a/internal/cbm/helpers.c b/internal/cbm/helpers.c
index 0a7e0ebc4..a36b57cdf 100644
--- a/internal/cbm/helpers.c
+++ b/internal/cbm/helpers.c
@@ -659,8 +659,15 @@ static const char **get_module_parents(CBMLanguage lang) {
}
}
-bool cbm_is_module_level(TSNode node, CBMLanguage lang) {
- TSNode parent = ts_node_parent(node);
+/* Variant that takes the node's parent DIRECTLY. The callers in
+ * extract_defs.c iterate a known parent's children, so they already
+ * have the parent — passing it here avoids ts_node_parent(node), which
+ * is O(n) per call (tree-sitter nodes carry no parent pointer; the
+ * parent is found by rescanning from the root). On a pathologically
+ * large file (e.g. a 583k-line generated/fixture file with tens of
+ * thousands of top-level statements) the old per-child ts_node_parent
+ * made extraction O(n²) and effectively hung. */
+bool cbm_is_module_level_p(TSNode parent, CBMLanguage lang) {
if (ts_node_is_null(parent)) {
return false;
}
@@ -693,6 +700,13 @@ bool cbm_is_module_level(TSNode node, CBMLanguage lang) {
return false;
}
+/* Back-compat wrapper: computes the parent via ts_node_parent (O(n)).
+ * Prefer cbm_is_module_level_p at call sites that already know the
+ * parent (the common case — iterating a parent's children). */
+bool cbm_is_module_level(TSNode node, CBMLanguage lang) {
+ return cbm_is_module_level_p(ts_node_parent(node), lang);
+}
+
// --- FQN computation ---
// Mirrors Go's fqn.Compute(): project + path_parts_dotted + name
diff --git a/internal/cbm/helpers.h b/internal/cbm/helpers.h
index f30ad59e4..73f04b372 100644
--- a/internal/cbm/helpers.h
+++ b/internal/cbm/helpers.h
@@ -45,6 +45,12 @@ int cbm_count_branching(TSNode node, const char **branching_types);
// Is this a module-level node? (not nested inside function/class body)
bool cbm_is_module_level(TSNode node, CBMLanguage lang);
+// Same check, but the node's PARENT is supplied directly — avoids the
+// O(n) ts_node_parent rescan. Use at call sites iterating a known
+// parent's children (the common case). `parent` is the parent of the
+// node being classified.
+bool cbm_is_module_level_p(TSNode parent, CBMLanguage lang);
+
// --- FQN computation ---
// Compute qualified name: project.rel_path_parts.name