Files
codebase-memory-mcp/internal/cbm/lsp/lsp_node_iter.h
Martin Vogel 1b9d9c136f perf(lsp): make per-file and cross-file LSP walkers O(n); fix two crashes
The advanced-mode LSP passes walked the AST with the same O(i) ts_node_child(node, i) idiom, so a wide root made each per-file pass quadratic — the TS fixture reallyLargeFile.ts took ~16000s. Add a shared cbm_lsp_collect_children() helper (lsp_node_iter.h) and convert the entry and recursive walkers across the TypeScript, C, C#, PHP and Go LSP back-ends to a single O(n) cursor pass. The file now indexes in ~28s; full advanced-mode runs: TS 100s, roslyn 46s, kubernetes 51s, postgres 8s, WordPress 7s.

Also fix two latent crashes exposed once the resolve phase actually ran: process_node indexed the NULL-terminated param_types[] by the call's argument count, reading past the terminator when a call passed more args than the callee declares; and return_type_of() built a tuple via cbm_type_tuple(NULL, ...), passing a NULL arena — it now threads ctx->arena.

This also includes the Tier 2 cross-LSP registry plumbing (shared read-only base registry with per-file overlay/fallback) the walkers build on.

All LSP unit suites pass: TS, C 737/737, C# 60/60, PHP 280/280, Go 55/55.
2026-05-28 13:56:08 +02:00

43 lines
1.7 KiB
C

/*
* lsp_node_iter.h — O(n) child enumeration for the per-language LSP walkers.
*
* tree-sitter's ts_node_child(node, i) is O(i): it walks the child iterator
* from the first child every call. So the common idiom
*
* uint32_t nc = ts_node_child_count(node);
* for (uint32_t i = 0; i < nc; i++) { TSNode c = ts_node_child(node, i); ... }
*
* is O(n²) over a node's children — catastrophic on a wide root (e.g. a program
* node holding hundreds of thousands of top-level statements, as in TS's
* reallyLargeFile.ts fixture: 583K comment lines made the per-file LSP passes
* run for ~133 minutes). cbm_lsp_collect_children() enumerates the children in
* a single O(n) cursor pass into an arena array; callers then index the array.
*/
#ifndef CBM_LSP_NODE_ITER_H
#define CBM_LSP_NODE_ITER_H
#include "../arena.h"
#include "tree_sitter/api.h"
/* Collect `node`'s children into an arena array (source order) via one O(n)
* cursor pass. Returns NULL and sets *out_n=0 for a childless node or on OOM. */
static inline TSNode* cbm_lsp_collect_children(CBMArena* arena, TSNode node, uint32_t* out_n) {
uint32_t nc = ts_node_child_count(node);
*out_n = 0;
if (nc == 0) return NULL;
TSNode* kids = (TSNode*)cbm_arena_alloc(arena, (size_t)nc * sizeof(TSNode));
if (!kids) return NULL;
uint32_t kn = 0;
TSTreeCursor cur = ts_tree_cursor_new(node);
if (ts_tree_cursor_goto_first_child(&cur)) {
do {
kids[kn++] = ts_tree_cursor_current_node(&cur);
} while (kn < nc && ts_tree_cursor_goto_next_sibling(&cur));
}
ts_tree_cursor_delete(&cur);
*out_n = kn;
return kids;
}
#endif /* CBM_LSP_NODE_ITER_H */