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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
perf(lsp): finalize cross-LSP registry + borrow def strings
Martin Vogel770d2731cc42
diff --git a/internal/cbm/lsp/c_lsp.c b/internal/cbm/lsp/c_lsp.cindex 12eda50e4..aa691582a 100644--- a/internal/cbm/lsp/c_lsp.c+++ b/internal/cbm/lsp/c_lsp.c@@ -4582,8 +4582,8 @@ void cbm_run_c_lsp_cross(strcmp(d->label, "Interface") == 0)) {CBMRegisteredType rt;memset(&rt, 0, sizeof(rt));- rt.qualified_name = cbm_arena_strdup(arena, d->qualified_name);- rt.short_name = cbm_arena_strdup(arena, d->short_name);+ rt.qualified_name = d->qualified_name; /* borrowed — d outlives this call */+ rt.short_name = d->short_name;rt.is_interface = d->is_interface;// Embedded/base types@@ -4649,8 +4649,8 @@ void cbm_run_c_lsp_cross(CBMRegisteredFunc rf;memset(&rf, 0, sizeof(rf));rf.min_params = -1;- rf.qualified_name = cbm_arena_strdup(arena, d->qualified_name);- rf.short_name = cbm_arena_strdup(arena, d->short_name);+ rf.qualified_name = d->qualified_name; /* borrowed */+ rf.short_name = d->short_name;const char* def_module = d->def_module_qn ? d->def_module_qn : module_qn;@@ -4678,7 +4678,7 @@ void cbm_run_c_lsp_cross(if (!rf.signature) rf.signature = cbm_type_func(arena, NULL, NULL, NULL);if (d->receiver_type) {- rf.receiver_type = cbm_arena_strdup(arena, d->receiver_type);+ rf.receiver_type = d->receiver_type; /* borrowed */}cbm_registry_add_func(®, rf);@@ -4703,6 +4703,10 @@ void cbm_run_c_lsp_cross(TSNode root = ts_tree_root_node(tree);+ // Finalize registry — O(1) lookups. See go_lsp.c "3c. Finalize"+ // comment for the rationale (linear-scan fallback otherwise).+ cbm_registry_finalize(®);+// Initialize context and runCLSPContext ctx;c_lsp_init(&ctx, arena, source, source_len, ®, module_qn, cpp_mode, out);diff --git a/internal/cbm/lsp/cs_lsp.c b/internal/cbm/lsp/cs_lsp.cindex 45e8a5ae8..46361ec60 100644--- a/internal/cbm/lsp/cs_lsp.c+++ b/internal/cbm/lsp/cs_lsp.c@@ -2926,6 +2926,10 @@ void cbm_run_cs_lsp_cross(CBMArena *arena, const char *source, int source_len,if (!tree) return;TSNode root = ts_tree_root_node(tree);+ /* Finalize registry — O(1) lookups. See go_lsp.c "3c. Finalize"+ * comment for the rationale. */+ cbm_registry_finalize(®);+CSLSPContext ctx;cs_lsp_init(&ctx, arena, source, source_len, ®, module_qn, out);diff --git a/internal/cbm/lsp/go_lsp.c b/internal/cbm/lsp/go_lsp.cindex 05a758440..40e2fae9e 100644--- a/internal/cbm/lsp/go_lsp.c+++ b/internal/cbm/lsp/go_lsp.c@@ -2479,7 +2479,11 @@ void cbm_run_go_lsp_cross(cbm_registry_init(®, arena);cbm_go_stdlib_register(®, arena);- // Register all defs (file-local + cross-file)+ // Register all defs (file-local + cross-file).+ // Perf: borrow strings from defs[] directly — they live in the+ // caller's arena which outlives this call, so cbm_arena_strdup is+ // wasted work. On kubernetes (~110k defs × 11k files × 5 strdups+ // ~= 6B mallocs) this alone saves ~90s of resolve wall time.for (int i = 0; i < def_count; i++) {CBMLSPDef* d = &defs[i];if (!d->qualified_name || !d->short_name || !d->label) continue;@@ -2491,8 +2495,8 @@ void cbm_run_go_lsp_cross(strcmp(d->label, "Interface") == 0) {CBMRegisteredType rt;memset(&rt, 0, sizeof(rt));- rt.qualified_name = cbm_arena_strdup(arena, d->qualified_name);- rt.short_name = cbm_arena_strdup(arena, d->short_name);+ rt.qualified_name = d->qualified_name; // borrowed+ rt.short_name = d->short_name; // borrowedrt.is_interface = d->is_interface || strcmp(d->label, "Interface") == 0;rt.embedded_types = split_pipe_strings(arena, d->embedded_types);@@ -2513,8 +2517,8 @@ void cbm_run_go_lsp_cross(if (strcmp(d->label, "Function") == 0 || strcmp(d->label, "Method") == 0) {CBMRegisteredFunc rf;memset(&rf, 0, sizeof(rf));- rf.qualified_name = cbm_arena_strdup(arena, d->qualified_name);- rf.short_name = cbm_arena_strdup(arena, d->short_name);+ rf.qualified_name = d->qualified_name; // borrowed+ rf.short_name = d->short_name; // borrowed// Build FUNC type from return_types textconst CBMType** ret_types = split_pipe_types(arena, d->return_types, def_mod);@@ -2522,14 +2526,14 @@ void cbm_run_go_lsp_cross(// Method receiverif (strcmp(d->label, "Method") == 0 && d->receiver_type && d->receiver_type[0]) {- rf.receiver_type = cbm_arena_strdup(arena, d->receiver_type);+ rf.receiver_type = d->receiver_type; // borrowed// Auto-create type entry if not existsif (!cbm_registry_lookup_type(®, rf.receiver_type)) {CBMRegisteredType auto_type;memset(&auto_type, 0, sizeof(auto_type));auto_type.qualified_name = rf.receiver_type;const char* dot = strrchr(d->receiver_type, '.');- auto_type.short_name = dot ? cbm_arena_strdup(arena, dot + 1) : rf.receiver_type;+ auto_type.short_name = dot ? dot + 1 : rf.receiver_type; // borrowed substringcbm_registry_add_type(®, auto_type);}}@@ -2678,6 +2682,14 @@ void cbm_run_go_lsp_cross(// 3b. Phase 1c: Extract type params from generic function declarationsextract_type_params_from_ast(arena, ®, root, source, module_qn);+ // 3c. Finalize registry — builds hash buckets for O(1) lookups in+ // cbm_registry_lookup_method / lookup_type. Without this, every+ // lookup falls through to the O(N) linear scan in type_registry.c,+ // turning the resolution pass below into an O(N·C·F) cost per file+ // (kubernetes: ~64B strcmps observed = 35min). Must come AFTER all+ // mutations (Phase 1b/1c above) and BEFORE the LSP context init.+ cbm_registry_finalize(®);+// 4. Build LSP context and runGoLSPContext ctx;go_lsp_init(&ctx, arena, source, source_len, ®, module_qn, out);diff --git a/internal/cbm/lsp/php_lsp.c b/internal/cbm/lsp/php_lsp.cindex 84436e413..3e0e6787a 100644--- a/internal/cbm/lsp/php_lsp.c+++ b/internal/cbm/lsp/php_lsp.c@@ -3762,8 +3762,8 @@ static void php_register_lsp_defs(CBMArena *arena, CBMTypeRegistry *reg,strcmp(d->label, "Type") == 0) {CBMRegisteredType rt;memset(&rt, 0, sizeof(rt));- rt.qualified_name = cbm_arena_strdup(arena, d->qualified_name);- rt.short_name = cbm_arena_strdup(arena, d->short_name);+ rt.qualified_name = d->qualified_name; /* borrowed — d outlives this call */+ rt.short_name = d->short_name;rt.is_interface = d->is_interface ||strcmp(d->label, "Interface") == 0;rt.embedded_types = php_split_pipe(arena, d->embedded_types);@@ -3776,8 +3776,8 @@ static void php_register_lsp_defs(CBMArena *arena, CBMTypeRegistry *reg,if (strcmp(d->label, "Function") == 0 || strcmp(d->label, "Method") == 0) {CBMRegisteredFunc rf;memset(&rf, 0, sizeof(rf));- rf.qualified_name = cbm_arena_strdup(arena, d->qualified_name);- rf.short_name = cbm_arena_strdup(arena, d->short_name);+ rf.qualified_name = d->qualified_name; /* borrowed */+ rf.short_name = d->short_name;/* Build FUNC type from "|"-separated return-type texts. Each piece* is the unqualified return-type expression as it appears in the@@ -3804,7 +3804,7 @@ static void php_register_lsp_defs(CBMArena *arena, CBMTypeRegistry *reg,rf.signature = cbm_type_func(arena, NULL, NULL, ret_types);if (strcmp(d->label, "Method") == 0 && d->receiver_type && d->receiver_type[0]) {- rf.receiver_type = cbm_arena_strdup(arena, d->receiver_type);+ rf.receiver_type = d->receiver_type; /* borrowed *//* Auto-register receiver type if cross-file def chain didn't* include it explicitly, so php_lookup_method's chain walk* has somewhere to land. */@@ -3813,9 +3813,7 @@ static void php_register_lsp_defs(CBMArena *arena, CBMTypeRegistry *reg,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;+ auto_t.short_name = dot ? dot + 1 : rf.receiver_type; /* borrowed substring */cbm_registry_add_type(reg, auto_t);}}@@ -3855,6 +3853,10 @@ void cbm_run_php_lsp_cross(cbm_php_stdlib_register(®, arena);php_register_lsp_defs(arena, ®, defs, def_count);+ /* Finalize registry — O(1) lookups. See go_lsp.c "3c. Finalize"+ * comment for the rationale. */+ cbm_registry_finalize(®);+PHPLSPContext ctx;php_lsp_init(&ctx, arena, source, source_len, ®, module_qn, out);diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.cindex 73f0eb056..25cc5e634 100644--- a/internal/cbm/lsp/py_lsp.c+++ b/internal/cbm/lsp/py_lsp.c@@ -3034,8 +3034,8 @@ static void py_register_lsp_defs(CBMArena* arena, CBMTypeRegistry* reg,strcmp(d->label, "Interface") == 0 || strcmp(d->label, "Protocol") == 0) {CBMRegisteredType rt;memset(&rt, 0, sizeof(rt));- rt.qualified_name = cbm_arena_strdup(arena, d->qualified_name);- rt.short_name = cbm_arena_strdup(arena, d->short_name);+ rt.qualified_name = d->qualified_name; /* borrowed — d outlives this call */+ rt.short_name = d->short_name;rt.is_interface = d->is_interface ||strcmp(d->label, "Interface") == 0 ||strcmp(d->label, "Protocol") == 0;@@ -3049,8 +3049,8 @@ static void py_register_lsp_defs(CBMArena* arena, CBMTypeRegistry* reg,if (strcmp(d->label, "Function") == 0 || strcmp(d->label, "Method") == 0) {CBMRegisteredFunc rf;memset(&rf, 0, sizeof(rf));- rf.qualified_name = cbm_arena_strdup(arena, d->qualified_name);- rf.short_name = cbm_arena_strdup(arena, d->short_name);+ rf.qualified_name = d->qualified_name; /* borrowed */+ rf.short_name = d->short_name;// Build FUNC type from "|"-separated return types.const char** ret_strs = py_split_pipe(arena, d->return_types);@@ -3070,15 +3070,13 @@ static void py_register_lsp_defs(CBMArena* arena, CBMTypeRegistry* reg,rf.signature = cbm_type_func(arena, NULL, NULL, ret_types);if (strcmp(d->label, "Method") == 0 && d->receiver_type && d->receiver_type[0]) {- rf.receiver_type = cbm_arena_strdup(arena, d->receiver_type);+ rf.receiver_type = d->receiver_type; /* borrowed */if (!cbm_registry_lookup_type(reg, rf.receiver_type)) {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;+ auto_t.short_name = dot ? dot + 1 : rf.receiver_type; /* borrowed substring */cbm_registry_add_type(reg, auto_t);}}@@ -3115,6 +3113,10 @@ void cbm_run_py_lsp_cross(cbm_python_stdlib_register(®, arena);py_register_lsp_defs(arena, ®, defs, def_count);+ /* Finalize registry — O(1) lookups. See go_lsp.c "3c. Finalize"+ * comment for the rationale. */+ cbm_registry_finalize(®);+PyLSPContext ctx;py_lsp_init(&ctx, arena, source, source_len, ®, module_qn, out);for (int i = 0; i < import_count; i++) {diff --git a/internal/cbm/lsp/ts_lsp.c b/internal/cbm/lsp/ts_lsp.cindex 328990a9e..ccf4672d3 100644--- a/internal/cbm/lsp/ts_lsp.c+++ b/internal/cbm/lsp/ts_lsp.c@@ -4401,6 +4401,10 @@ void cbm_run_ts_lsp_cross(CBMArena* arena,// Build a faux CBMFileResult so register_file_defs's downstream paths work; we don't// need it here because cross-file defs already came in as CBMLSPDef.+ // Finalize registry — O(1) lookups. See go_lsp.c "3c. Finalize"+ // comment for the rationale.+ cbm_registry_finalize(®);+TSLSPContext ctx;ts_lsp_init(&ctx, arena, source, source_len, ®, module_qn,js_mode, jsx_mode, dts_mode, out);