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(parallel): O(1) per-file lsp resolution index + ASan fix

Martin Vogeldf6ddf058b4d
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index e51657059..e53184389 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -48,6 +48,7 @@ enum { PP_CSHARP_M_PREFIX_LEN = 2 };
#include "graph_buffer/graph_buffer.h"
#include "service_patterns.h"
#include "foundation/platform.h"
+#include "foundation/hash_table.h"
#include "foundation/log.h"
#include "foundation/slab_alloc.h"
#include "foundation/mem.h"
@@ -1518,10 +1519,55 @@ static void try_field_type_hint(resolve_ctx_t *rc, cbm_resolution_t *res, const
}
}
+/* Free a strdup'd key stored in the per-file lsp_idx hash table. */
+static void lsp_idx_free_key(const char *key, void *value, void *ud) {
+ (void)value;
+ (void)ud;
+ free((char *)key);
+}
+
/* Resolve calls for one file and emit CALLS/HTTP_CALLS/ASYNC_CALLS edges. */
static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CBMFileResult *result,
const char *rel, const char *module_qn, const char **imp_keys,
const char **imp_vals, int imp_count) {
+ /* Build a per-file hash index of resolved_calls keyed by
+ * "caller_qn|callee_short" for O(1) lookup. cbm_pipeline_find_lsp_
+ * resolution would otherwise do an O(N) linear scan over
+ * resolved_calls for EACH of result->calls.count calls — the
+ * dominant cost in parallel_resolve on kubernetes (~50s of pure
+ * scanning). On insert, keep the highest-confidence entry per key
+ * (matches the original "best" tie-break). Skip the build entirely
+ * when there are no calls (nothing to look up) or no resolved
+ * entries (lookups would all miss). */
+ CBMHashTable *lsp_idx = NULL;
+ if (result->calls.count > 0 && result->resolved_calls.count > 0) {
+ lsp_idx = cbm_ht_create((uint32_t)result->resolved_calls.count * 2u + 16u);
+ if (lsp_idx) {
+ for (int i = 0; i < result->resolved_calls.count; i++) {
+ CBMResolvedCall *rc_e = &result->resolved_calls.items[i];
+ if (!rc_e->caller_qn || !rc_e->callee_qn ||
+ rc_e->confidence < CBM_LSP_CONFIDENCE_FLOOR) {
+ continue;
+ }
+ const char *short_name = strrchr(rc_e->callee_qn, '.');
+ short_name = short_name ? short_name + 1 : rc_e->callee_qn;
+ char key[1024];
+ int kn = snprintf(key, sizeof(key), "%s|%s", rc_e->caller_qn, short_name);
+ if (kn <= 0 || kn >= (int)sizeof(key)) continue;
+ CBMResolvedCall *existing = (CBMResolvedCall *)cbm_ht_get(lsp_idx, key);
+ if (!existing) {
+ /* New entry — strdup so the key outlives the loop body. */
+ char *kdup = strdup(key);
+ if (kdup) cbm_ht_set(lsp_idx, kdup, rc_e);
+ } else if (rc_e->confidence > existing->confidence) {
+ /* Update value; reuse stored key pointer to avoid leak. */
+ const char *skey = cbm_ht_get_key(lsp_idx, key);
+ if (skey) cbm_ht_set(lsp_idx, skey, rc_e);
+ }
+ }
+ }
+ }
+
for (int c = 0; c < result->calls.count; c++) {
CBMCall *call = &result->calls.items[c];
if (!call->callee_name) {
@@ -1539,8 +1585,21 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
* LSP overrides so a project doesn't get different attributions
* depending on whether parallel mode kicked in. */
cbm_resolution_t res = {0};
- const CBMResolvedCall *lsp =
- cbm_pipeline_find_lsp_resolution(&result->resolved_calls, call);
+ const CBMResolvedCall *lsp = NULL;
+ if (lsp_idx && call->enclosing_func_qn) {
+ char key[1024];
+ int kn = snprintf(key, sizeof(key), "%s|%s",
+ call->enclosing_func_qn, call->callee_name);
+ if (kn > 0 && kn < (int)sizeof(key)) {
+ lsp = (const CBMResolvedCall *)cbm_ht_get(lsp_idx, key);
+ }
+ }
+ if (!lsp) {
+ /* Fallback to the linear scan for edge cases the index may
+ * miss (e.g. callee_name that wasn't the registered short
+ * name). Keeps semantics identical. */
+ lsp = cbm_pipeline_find_lsp_resolution(&result->resolved_calls, call);
+ }
if (lsp) {
/* Canonicalise to the gbuf node's QN so res.qualified_name matches
* the gbuf even when the cross-file fallback had to prefix the
@@ -1583,6 +1642,10 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
rc->registry, rc->main_gbuf, imp_keys, imp_vals, imp_count);
ws->calls_resolved++;
}
+ if (lsp_idx) {
+ cbm_ht_foreach(lsp_idx, lsp_idx_free_key, NULL);
+ cbm_ht_free(lsp_idx);
+ }
}
/* Resolve usages for one file. */