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(lsp): generic negative-lookup memo; wire the rust trait cascades

Martin Vogel2f11a4d580a8
diff --git a/internal/cbm/lsp/lsp_neg_memo.h b/internal/cbm/lsp/lsp_neg_memo.h
new file mode 100644
index 000000000..ae20559a4
--- /dev/null
+++ b/internal/cbm/lsp/lsp_neg_memo.h
@@ -0,0 +1,142 @@
+/*
+ * lsp_neg_memo.h — generic negative-lookup memo for LSP resolve cascades.
+ *
+ * Problem (all hybrid-LSP languages): a resolve cascade probes a ladder of
+ * hypotheses (direct hit, module-prefixed retry, alias walk, trait/base
+ * dispatch, short-name fallback) and most probes MISS — macro-expanded or
+ * generated code asks the SAME failing question thousands of times per file,
+ * re-paying the whole ladder each time (linux kernel: 4 trait-heavy rust
+ * files at ~63 s each; the C resolver had the same disease before its memo).
+ *
+ * A miss is a pure fact of (registry, query) — but ONLY while the registry
+ * cannot change. Callers must therefore gate the memo on a SEALED registry
+ * (reg->read_only, set at finalize; cbm_registry_add_* hard-return then) and
+ * must key only queries whose cascade reads nothing but the registry and the
+ * query strings (no per-function scope state in the memoized rungs).
+ *
+ * Shape: open-addressing set of 64-bit keys, arena-backed (dies with the
+ * per-file arena — no explicit free, mirroring the py field overlay). A memo
+ * HIT means "this exact query already ran the full cascade and returned
+ * nothing" → the caller returns its miss result immediately. To make a hash
+ * collision harmless, callers keep their cheap DIRECT lookup before the memo
+ * check (the C-memo pattern): a colliding real hit is still found; only the
+ * expensive registry-pure miss ladder is skipped.
+ *
+ * Per-language wiring is a few lines: add a CBMNegMemo to the language ctx,
+ * key each cascade entry with cbm_negmemo_key (site tag + query strings),
+ * check at entry, insert on the miss return. Wired: rust. Candidates per the
+ * 2026-07 resolve audit: php, c# (extension methods), java, kotlin; the C
+ * resolver's bespoke memo in c_lsp.c predates this header and can migrate.
+ */
+#ifndef CBM_LSP_NEG_MEMO_H
+#define CBM_LSP_NEG_MEMO_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "arena.h"
+
+typedef struct {
+ uint64_t *slots; /* arena-owned; 0 = empty (keys are never 0) */
+ int cap; /* power of two */
+ int count;
+} CBMNegMemo;
+
+enum {
+ CBM_NEGMEMO_INIT_CAP = 1024,
+ CBM_NEGMEMO_GROW = 2,
+ CBM_NEGMEMO_LOAD_NUM = 7, /* grow at 70% load */
+ CBM_NEGMEMO_LOAD_DEN = 10,
+};
+
+/* FNV-1a over (site tag, a, 0xff, b). The site tag keeps two cascades with
+ * the same argument strings from sharing keys. Never returns 0. */
+static inline uint64_t cbm_negmemo_key(uint8_t site, const char *a, const char *b) {
+ uint64_t h = 0xcbf29ce484222325ULL;
+ h ^= site;
+ h *= 0x100000001b3ULL;
+ if (a) {
+ while (*a) {
+ h ^= (unsigned char)*a++;
+ h *= 0x100000001b3ULL;
+ }
+ }
+ h ^= 0xff;
+ h *= 0x100000001b3ULL;
+ if (b) {
+ while (*b) {
+ h ^= (unsigned char)*b++;
+ h *= 0x100000001b3ULL;
+ }
+ }
+ return h ? h : 1;
+}
+
+static inline bool cbm_negmemo_contains(const CBMNegMemo *m, uint64_t key) {
+ if (!m->slots || m->count == 0) {
+ return false;
+ }
+ uint64_t mask = (uint64_t)(m->cap - 1);
+ for (uint64_t i = key & mask;; i = (i + 1) & mask) {
+ uint64_t s = m->slots[i];
+ if (s == 0) {
+ return false;
+ }
+ if (s == key) {
+ return true;
+ }
+ }
+}
+
+static inline void cbm_negmemo_insert_raw(uint64_t *slots, int cap, uint64_t key) {
+ uint64_t mask = (uint64_t)(cap - 1);
+ for (uint64_t i = key & mask;; i = (i + 1) & mask) {
+ if (slots[i] == key) {
+ return;
+ }
+ if (slots[i] == 0) {
+ slots[i] = key;
+ return;
+ }
+ }
+}
+
+/* Arena-backed insert: lazy first allocation, grow-by-rehash at 70% load.
+ * The abandoned table stays in the arena (bounded, freed with the file). */
+static inline void cbm_negmemo_insert(CBMNegMemo *m, CBMArena *arena, uint64_t key) {
+ if (!arena) {
+ return; /* no arena — memo silently disabled */
+ }
+ if (!m->slots) {
+ m->slots = cbm_arena_alloc(arena, sizeof(uint64_t) * CBM_NEGMEMO_INIT_CAP);
+ if (!m->slots) {
+ return;
+ }
+ memset(m->slots, 0, sizeof(uint64_t) * 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;
+ uint64_t *ns = cbm_arena_alloc(arena, sizeof(uint64_t) * (size_t)new_cap);
+ if (!ns) {
+ return; /* keep the old (full-ish) table; inserts degrade, reads stay correct */
+ }
+ memset(ns, 0, sizeof(uint64_t) * (size_t)new_cap);
+ for (int i = 0; i < m->cap; i++) {
+ if (m->slots[i]) {
+ cbm_negmemo_insert_raw(ns, new_cap, m->slots[i]);
+ }
+ }
+ m->slots = ns;
+ m->cap = new_cap;
+ }
+ if (!cbm_negmemo_contains(m, key)) {
+ cbm_negmemo_insert_raw(m->slots, m->cap, key);
+ 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 16e3aedce..183b152c1 100644
--- a/internal/cbm/lsp/rust_lsp.c
+++ b/internal/cbm/lsp/rust_lsp.c
@@ -2324,7 +2324,8 @@ static const CBMRegisteredFunc *rust_resolve_trait_method(RustLSPContext *ctx,
/* First try inherent method on the receiver itself, following any
* type aliases (so `std.sync.Arc.clone` resolves through to
- * `alloc.sync.Arc.clone` in the registry). */
+ * `alloc.sync.Arc.clone` in the registry). Stays BEFORE the memo check —
+ * a colliding real hit is always found (collision guard). */
const CBMRegisteredFunc *inh =
cbm_registry_lookup_method_aliased(reg, receiver_type_qn, method_name);
if (inh) {
@@ -2333,6 +2334,23 @@ static const CBMRegisteredFunc *rust_resolve_trait_method(RustLSPContext *ctx,
return inh;
}
+ /* Negative memo (sealed registry only): this whole cascade — embedded-
+ * impl walk + trait-default tail — reads nothing but the registry and the
+ * two query strings, so under read_only a miss is a pure fact. Macro-
+ * expanded kernel rust asks the same failing (receiver, method) question
+ * thousands of times per file; without the memo each repeat re-paid the
+ * full walk (~63 s per trait-heavy file). Only the full miss (0 impls,
+ * no trait default) is memoized, preserving out_impl_count fidelity for
+ * the ambiguous (>=2 impls) case. */
+ bool nm_active = reg && reg->read_only;
+ uint64_t nm_key = 0;
+ if (nm_active) {
+ nm_key = cbm_negmemo_key(1, receiver_type_qn, method_name);
+ if (cbm_negmemo_contains(&ctx->neg_memo, nm_key)) {
+ return NULL;
+ }
+ }
+
/* Look at every type whose embedded_types include the receiver_type_qn
* (treated as a trait): pick the single-impl case. Prefilter to the types whose
* embedded_types carry a matching BARE name via the registry index; the exact
@@ -2364,7 +2382,12 @@ static const CBMRegisteredFunc *rust_resolve_trait_method(RustLSPContext *ctx,
*out_impl_count = impls;
if (impls == 1)
return unique;
- return rust_lookup_method_in_trait(ctx, receiver_type_qn, method_name);
+ const CBMRegisteredFunc *tm =
+ rust_lookup_method_in_trait(ctx, receiver_type_qn, method_name);
+ if (nm_active && !tm && impls == 0) {
+ cbm_negmemo_insert(&ctx->neg_memo, ctx->arena, nm_key);
+ }
+ return tm;
}
// True if `type_qn` implements a trait that declares `method_name` — i.e. a
@@ -2401,6 +2424,16 @@ static const CBMRegisteredFunc *rust_find_sole_trait_impl(RustLSPContext *ctx, c
if (!ctx || !trait_qn || !method_name)
return NULL;
const CBMTypeRegistry *reg = ctx->registry;
+ /* Negative memo (sealed registry only) — registry-pure cascade; only the
+ * zero-implementer miss is memoized (out_n fidelity for the 2+ case). */
+ bool nm_active = reg && reg->read_only;
+ uint64_t nm_key = 0;
+ if (nm_active) {
+ nm_key = cbm_negmemo_key(2, trait_qn, method_name);
+ if (cbm_negmemo_contains(&ctx->neg_memo, nm_key)) {
+ return NULL;
+ }
+ }
const char *tdot = strrchr(trait_qn, '.');
const char *tbare = tdot ? tdot + 1 : trait_qn;
const CBMRegisteredFunc *first = NULL;
@@ -2441,6 +2474,9 @@ static const CBMRegisteredFunc *rust_find_sole_trait_impl(RustLSPContext *ctx, c
}
if (out_n)
*out_n = n;
+ if (nm_active && n == 0) {
+ cbm_negmemo_insert(&ctx->neg_memo, ctx->arena, nm_key);
+ }
return n == 1 ? first : NULL;
}
diff --git a/internal/cbm/lsp/rust_lsp.h b/internal/cbm/lsp/rust_lsp.h
index 759e5b1d0..19fbf3707 100644
--- a/internal/cbm/lsp/rust_lsp.h
+++ b/internal/cbm/lsp/rust_lsp.h
@@ -27,6 +27,7 @@
#include "type_rep.h"
#include "scope.h"
#include "type_registry.h"
+#include "lsp_neg_memo.h"
#include "../cbm.h"
#include "go_lsp.h" // for CBMLSPDef (pipeline def), used by the Tier-2 builder
@@ -58,6 +59,12 @@ typedef struct {
const CBMTypeRegistry *registry;
CBMScope *current_scope;
+ /* Negative-lookup memo for the registry-pure resolve cascades (trait
+ * method / sole-trait-impl / free-func fallback). Active ONLY when the
+ * registry is sealed (read_only) — see lsp_neg_memo.h. Arena-backed,
+ * dies with the file. Zeroed by rust_lsp_init's memset (lazy alloc). */
+ CBMNegMemo neg_memo;
+
/* `use` map: parallel arrays mapping a local-name (the last segment, or
* the `as` alias) to its full module path (`std::collections::HashMap`,
* `crate::foo::Bar`). Glob imports go in `glob_module_qns` instead. */