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(registry): per-file memoization cache for is_import_reachable
Martin Vogel109c9f07c1e8
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.cindex 3dd02ff25..f8ed3a256 100644--- a/src/pipeline/pass_parallel.c+++ b/src/pipeline/pass_parallel.c@@ -1930,6 +1930,14 @@ static void resolve_worker(int worker_id, void *ctx_ptr) {atomic_fetch_add_explicit(&rc->time_ns_import_map,extract_now_ns() - _imp_t0, memory_order_relaxed);+ /* Per-file is_import_reachable memoization. Spans all 5 resolve+ * sub-passes (calls/usages/throws/rw/semantic) which all flow+ * through cbm_registry_resolve. Same callee_name appears in+ * many call sites — first eval pays the strstr cost, repeats+ * are O(1) hash. Imports are constant within a file so the+ * cache is sound; invalidated at file exit. */+ cbm_registry_reach_cache_begin(result->calls.count + result->usages.count + 64);+char *module_qn = cbm_pipeline_fqn_module(rc->project_name, rel);/* ── Cross-file LSP (FUSED) ─────────────────────────────@@ -2075,6 +2083,8 @@ static void resolve_worker(int worker_id, void *ctx_ptr) {resolve_file_semantic(rc, ws, result, module_qn, imp_keys, imp_vals, imp_count);atomic_fetch_add_explicit(&rc->time_ns_semantic, extract_now_ns() - _ph_t0, memory_order_relaxed);+ cbm_registry_reach_cache_end();+free(module_qn);free_import_map(imp_keys, imp_vals, imp_count);diff --git a/src/pipeline/pipeline.h b/src/pipeline/pipeline.hindex 3d12e9864..5e293bad0 100644--- a/src/pipeline/pipeline.h+++ b/src/pipeline/pipeline.h@@ -131,6 +131,14 @@ cbm_resolution_t cbm_registry_resolve(const cbm_registry_t *r, const char *calleconst char *module_qn, const char **import_map_keys,const char **import_map_vals, int import_map_count);+/* Per-file memoization cache for is_import_reachable. Thread-local —+ * each resolve worker owns its own cache. Call _begin at the start+ * of resolve_file_calls (or any per-file resolve loop) and _end at+ * the end. The cache MUST be invalidated between files because+ * is_import_reachable's truth depends on the file's import_vals. */+void cbm_registry_reach_cache_begin(int estimated_capacity);+void cbm_registry_reach_cache_end(void);+/* Check if a qualified name exists in the registry. */bool cbm_registry_exists(const cbm_registry_t *r, const char *qn);diff --git a/src/pipeline/registry.c b/src/pipeline/registry.cindex 0b1ecfdc1..37ea24d52 100644--- a/src/pipeline/registry.c+++ b/src/pipeline/registry.c@@ -17,6 +17,7 @@ enum { REG_INIT_CAP = 16, REG_MIN_CANDIDATES = 3, REG_RESOLVED = 1, REG_SUFFIX_A#define DEFAULT_CONFIDENCE 0.5#include "pipeline/pipeline.h"+#include "foundation/compat.h" /* CBM_TLS */#include "foundation/hash_table.h"#include "foundation/dyn_array.h"#include "foundation/platform.h"@@ -149,10 +150,62 @@ static const char *best_by_import_distance(const char **candidates, int count,return best;}+/* ── Per-file is_import_reachable memoization cache ───────────────+ *+ * The hot path on kubernetes spends ~50% of resolve_calls CPU in+ * is_import_reachable's O(candidates × imports × strstr) scan. The+ * SAME candidate_qn is re-evaluated dozens of times per file because+ * the same callee_name appears in multiple call sites and each lookup+ * re-checks all candidates for that name.+ *+ * Cache life-cycle is per-FILE (because import_vals changes between+ * files). resolve_file_calls calls _begin at file entry and _end at+ * file exit. Thread-local so each worker has its own cache without+ * contention. */+static CBM_TLS CBMHashTable *_reach_cache = NULL;++/* Sentinels stored as values in the cache. NULL means "not cached".+ * We need two distinct non-NULL pointers to encode true/false. */+#define REACH_CACHE_TRUE ((void *)(uintptr_t)1)+#define REACH_CACHE_FALSE ((void *)(uintptr_t)2)++static void reach_cache_free_key(const char *key, void *val, void *ud) {+ (void)val;+ (void)ud;+ free((char *)key);+}++void cbm_registry_reach_cache_begin(int estimated_capacity) {+ if (_reach_cache) {+ /* Defensive: caller forgot to call _end. Clear and reuse. */+ cbm_ht_foreach(_reach_cache, reach_cache_free_key, NULL);+ cbm_ht_clear(_reach_cache);+ return;+ }+ if (estimated_capacity < 16) estimated_capacity = 16;+ _reach_cache = cbm_ht_create((uint32_t)estimated_capacity);+}++void cbm_registry_reach_cache_end(void) {+ if (!_reach_cache) return;+ cbm_ht_foreach(_reach_cache, reach_cache_free_key, NULL);+ cbm_ht_free(_reach_cache);+ _reach_cache = NULL;+}+/* Check if candidate's module prefix appears in import map values.- * Uses stack buffer to avoid malloc/free per call in hot resolution loop. */+ * Uses stack buffer to avoid malloc/free per call in hot resolution loop.+ * Per-file memoization via TLS cache: repeated lookups of the same+ * candidate_qn (same name appears in many call sites) become O(1)+ * after the first computation. */static bool is_import_reachable(const char *candidate_qn, const char **import_vals,int import_count) {+ if (_reach_cache) {+ void *cached = cbm_ht_get(_reach_cache, candidate_qn);+ if (cached == REACH_CACHE_TRUE) return true;+ if (cached == REACH_CACHE_FALSE) return false;+ }+char cand_mod[CBM_SZ_512];const char *last = strrchr(candidate_qn, '.');if (last) {@@ -165,12 +218,22 @@ static bool is_import_reachable(const char *candidate_qn, const char **import_va} else {snprintf(cand_mod, sizeof(cand_mod), "%s", candidate_qn);}+ bool reachable = false;for (int i = 0; i < import_count; i++) {if (strstr(cand_mod, import_vals[i]) || strstr(import_vals[i], cand_mod)) {- return true;+ reachable = true;+ break;+ }+ }++ if (_reach_cache) {+ char *kdup = strdup(candidate_qn);+ if (kdup) {+ cbm_ht_set(_reach_cache, kdup,+ reachable ? REACH_CACHE_TRUE : REACH_CACHE_FALSE);}}- return false;+ return reachable;}/* Scale confidence inversely with candidate count. */