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(rust-lsp): O(1) type map in the registry build + macro-expansion dedup

Martin Vogel10b3b7963061
diff --git a/internal/cbm/lsp/lsp_neg_memo.h b/internal/cbm/lsp/lsp_neg_memo.h
index ae20559a4..022db6dfc 100644
--- a/internal/cbm/lsp/lsp_neg_memo.h
+++ b/internal/cbm/lsp/lsp_neg_memo.h
@@ -139,4 +139,92 @@ static inline void cbm_negmemo_insert(CBMNegMemo *m, CBMArena *arena, uint64_t k
}
}
+/* ── CBMIdxMemo: exact-match string-key → int index map ─────────────────────
+ * Companion for build-time registration loops that need "have I registered
+ * this QN, and at which index?" in O(1) — the registry's own buckets don't
+ * exist before finalize, and probing it linearly from inside the registration
+ * loop is the classic quadratic (kernel shared rust registry: ~63 s).
+ * Exact match: each slot stores the borrowed key pointer and verifies with
+ * strcmp, so hash collisions can't map to a wrong index. First-put wins. */
+typedef struct {
+ struct cbm_idxmemo_slot {
+ uint64_t h; /* 0 = empty */
+ const char *key;
+ int32_t val;
+ } *slots;
+ int cap; /* power of two */
+ int count;
+} CBMIdxMemo;
+
+static inline int32_t cbm_idxmemo_get(const CBMIdxMemo *m, const char *key) {
+ if (!m->slots || !key || m->count == 0) {
+ return -1;
+ }
+ uint64_t h = cbm_negmemo_key(0, key, NULL);
+ uint64_t mask = (uint64_t)(m->cap - 1);
+ for (uint64_t i = h & mask;; i = (i + 1) & mask) {
+ if (m->slots[i].h == 0) {
+ return -1;
+ }
+ if (m->slots[i].h == h && m->slots[i].key && strcmp(m->slots[i].key, key) == 0) {
+ return m->slots[i].val;
+ }
+ }
+}
+
+static inline void cbm_idxmemo_put_raw(struct cbm_idxmemo_slot *slots, int cap, uint64_t h,
+ const char *key, int32_t val) {
+ uint64_t mask = (uint64_t)(cap - 1);
+ for (uint64_t i = h & mask;; i = (i + 1) & mask) {
+ if (slots[i].h == 0) {
+ slots[i].h = h;
+ slots[i].key = key;
+ slots[i].val = val;
+ return;
+ }
+ if (slots[i].h == h && slots[i].key && strcmp(slots[i].key, key) == 0) {
+ return; /* first-put wins */
+ }
+ }
+}
+
+static inline void cbm_idxmemo_put_if_absent(CBMIdxMemo *m, CBMArena *arena, const char *key,
+ int32_t val) {
+ if (!arena || !key) {
+ return;
+ }
+ if (!m->slots) {
+ m->slots = cbm_arena_alloc(arena,
+ sizeof(struct cbm_idxmemo_slot) * CBM_NEGMEMO_INIT_CAP);
+ if (!m->slots) {
+ return;
+ }
+ memset(m->slots, 0, sizeof(struct cbm_idxmemo_slot) * CBM_NEGMEMO_INIT_CAP);
+ m->cap = CBM_NEGMEMO_INIT_CAP;
+ m->count = 0;
+ }
+ if ((int64_t)(m->count + 1) * CBM_NEGMEMO_LOAD_DEN >=
+ (int64_t)m->cap * CBM_NEGMEMO_LOAD_NUM) {
+ int new_cap = m->cap * CBM_NEGMEMO_GROW;
+ struct cbm_idxmemo_slot *ns =
+ cbm_arena_alloc(arena, sizeof(struct cbm_idxmemo_slot) * (size_t)new_cap);
+ if (!ns) {
+ return;
+ }
+ memset(ns, 0, sizeof(struct cbm_idxmemo_slot) * (size_t)new_cap);
+ for (int i = 0; i < m->cap; i++) {
+ if (m->slots[i].h) {
+ cbm_idxmemo_put_raw(ns, new_cap, m->slots[i].h, m->slots[i].key, m->slots[i].val);
+ }
+ }
+ m->slots = ns;
+ m->cap = new_cap;
+ }
+ uint64_t h = cbm_negmemo_key(0, key, NULL);
+ if (cbm_idxmemo_get(m, key) < 0) {
+ cbm_idxmemo_put_raw(m->slots, m->cap, h, key, val);
+ m->count++;
+ }
+}
+
#endif /* CBM_LSP_NEG_MEMO_H */
diff --git a/internal/cbm/lsp/rust_lsp.c b/internal/cbm/lsp/rust_lsp.c
index 183b152c1..5ca867cd2 100644
--- a/internal/cbm/lsp/rust_lsp.c
+++ b/internal/cbm/lsp/rust_lsp.c
@@ -29,6 +29,7 @@
#include "rust_cargo.h"
#include "../helpers.h"
#include <ctype.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -3159,6 +3160,20 @@ static void rust_expand_user_macro(RustLSPContext *ctx, const char *mname, TSNod
if (!substituted)
return;
+ /* Expansion memo (see RustLSPContext.macro_memo): reset per top-level
+ * invocation, dedup identical (macro, substituted body) within the
+ * recursion chain — kills the exponential breadth of self-recursive
+ * macro_rules without losing any distinctly-attributed source site. */
+ if (ctx->macro_expand_depth == 0 && ctx->macro_memo.slots) {
+ memset(ctx->macro_memo.slots, 0, sizeof(uint64_t) * (size_t)ctx->macro_memo.cap);
+ ctx->macro_memo.count = 0;
+ }
+ uint64_t mm_key = cbm_negmemo_key(3, mname, substituted);
+ if (cbm_negmemo_contains(&ctx->macro_memo, mm_key)) {
+ return;
+ }
+ cbm_negmemo_insert(&ctx->macro_memo, ctx->arena, mm_key);
+
/* Wrap and parse. */
char *wrapped = cbm_arena_sprintf(ctx->arena, "fn __cbm_macro_expand() { %s; }\n", substituted);
if (!wrapped)
@@ -3239,6 +3254,20 @@ static void rust_resolve_macro_arg_exprs(RustLSPContext *ctx, TSNode invocation)
if (!arg_text)
return;
+ /* Same expansion memo as rust_expand_user_macro (site tag 4), but ONLY
+ * inside a recursion chain (depth > 0): there the identical argument text
+ * is re-parsed on every re-expansion of a self-recursive macro and has no
+ * distinct source site. Top-level invocations (depth 0) always parse —
+ * identical args in different enclosing functions attribute differently
+ * and must all be walked. */
+ if (ctx->macro_expand_depth > 0) {
+ uint64_t am_key = cbm_negmemo_key(4, arg_text, NULL);
+ if (cbm_negmemo_contains(&ctx->macro_memo, am_key)) {
+ return;
+ }
+ cbm_negmemo_insert(&ctx->macro_memo, ctx->arena, am_key);
+ }
+
/* Wrap the comma-separated arguments in a tuple expression so the whole
* thing parses as one valid expression (a trailing format-spec arg like
* `width = w` would otherwise break statement parsing). Calls inside any
@@ -5342,6 +5371,24 @@ static void rust_populate_cross_registry(CBMTypeRegistry *reg, CBMArena *arena,
cbm_registry_init(reg, arena);
cbm_rust_stdlib_register(reg, arena);
+ /* qn → (type index + 1), FIRST occurrence wins (mirrors the linear scans
+ * this map replaces). Both in-loop registry probes below — the receiver
+ * auto-registration check and the trait-linkage lookup — used to scan the
+ * UNFINALIZED registry linearly (no buckets exist before finalize): the
+ * checklist's lookup-in-registration-loop pattern. Invisible on small
+ * per-file builds; on the shared all_defs build (~1.4M entries) those
+ * scans were a constant ~63 s of the kernel run — and the sibling
+ * null-filter files waited on the build once-guard for exactly that long,
+ * which is why no resolution-side fix ever moved their wall time. Index,
+ * not pointer, because reg->types reallocs as it grows. */
+ CBMIdxMemo type_idx = {0};
+ for (int ti = 0; ti < reg->type_count; ti++) {
+ const char *qn = reg->types[ti].qualified_name;
+ if (qn) {
+ cbm_idxmemo_put_if_absent(&type_idx, arena, qn, ti);
+ }
+ }
+
for (int i = 0; i < def_count; i++) {
CBMRustLSPDef *d = &defs[i];
if (!d->qualified_name || !d->short_name || !d->label)
@@ -5358,6 +5405,7 @@ static void rust_populate_cross_registry(CBMTypeRegistry *reg, CBMArena *arena,
rt.is_interface = d->is_interface || strcmp(d->label, "Trait") == 0 ||
strcmp(d->label, "Interface") == 0;
cbm_registry_add_type(reg, rt);
+ cbm_idxmemo_put_if_absent(&type_idx, arena, rt.qualified_name, reg->type_count - 1);
}
if (strcmp(d->label, "Function") == 0 || strcmp(d->label, "Method") == 0) {
@@ -5398,13 +5446,15 @@ static void rust_populate_cross_registry(CBMTypeRegistry *reg, CBMArena *arena,
if (strcmp(d->label, "Method") == 0 && d->receiver_type && d->receiver_type[0]) {
rf.receiver_type = cbm_arena_strdup(arena, d->receiver_type);
- if (!cbm_registry_lookup_type(reg, rf.receiver_type)) {
+ if (cbm_idxmemo_get(&type_idx, rf.receiver_type) < 0) {
CBMRegisteredType auto_t;
memset(&auto_t, 0, sizeof(auto_t));
auto_t.qualified_name = rf.receiver_type;
const char *dot = strrchr(d->receiver_type, '.');
auto_t.short_name = dot ? cbm_arena_strdup(arena, dot + 1) : rf.receiver_type;
cbm_registry_add_type(reg, auto_t);
+ cbm_idxmemo_put_if_absent(&type_idx, arena, auto_t.qualified_name,
+ reg->type_count - 1);
}
}
@@ -5413,12 +5463,9 @@ static void rust_populate_cross_registry(CBMTypeRegistry *reg, CBMArena *arena,
/* If trait_qn set: encode embedded_type linkage on receiver. */
if (rf.receiver_type && d->trait_qn && d->trait_qn[0]) {
CBMRegisteredType *rt = NULL;
- for (int ti = 0; ti < reg->type_count; ti++) {
- if (reg->types[ti].qualified_name &&
- strcmp(reg->types[ti].qualified_name, rf.receiver_type) == 0) {
- rt = &reg->types[ti];
- break;
- }
+ int32_t tix = cbm_idxmemo_get(&type_idx, rf.receiver_type);
+ if (tix >= 0) {
+ rt = &reg->types[tix];
}
if (rt) {
int existing = 0;
@@ -5437,7 +5484,8 @@ static void rust_populate_cross_registry(CBMTypeRegistry *reg, CBMArena *arena,
}
}
- /* Finalise the cross-file registry now that all defs are added. */
+ /* Finalise the cross-file registry now that all defs are added.
+ * (type_idx is arena-owned — freed with the registry's arena.) */
cbm_registry_finalize(reg);
}
diff --git a/internal/cbm/lsp/rust_lsp.h b/internal/cbm/lsp/rust_lsp.h
index 19fbf3707..f6ddd223b 100644
--- a/internal/cbm/lsp/rust_lsp.h
+++ b/internal/cbm/lsp/rust_lsp.h
@@ -174,6 +174,18 @@ typedef struct {
/* CBM_LSP_DEBUG=1 in env enables verbose stderr trace. */
bool debug;
+
+ /* Per-ROOT-invocation macro-expansion memo. Kernel macro_rules are often
+ * self-recursive (define_sizes! re-invokes itself with @internal/@impls
+ * args) and the expansion fallback ("no pattern matched — still expand the
+ * first rule") makes that recursion non-convergent: with an unbounded
+ * BREADTH under the depth-8 guard, 2-44 source invocations exploded into
+ * ~200k expansions (each a full tree-sitter parse) ≈ 63 s per file.
+ * Within one expansion chain an identical (macro, substituted body) is
+ * walked once — identical text implies an identical walk, and recursive
+ * re-invocations have no distinct source site to attribute. Reset at each
+ * top-level invocation so distinct source sites keep their attribution. */
+ CBMNegMemo macro_memo;
} RustLSPContext;
/* Initialise an empty context for processing one file. */