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(strings): registry borrows its own QN copy; semantic tokens interned

Martin Vogelff616241235e
diff --git a/src/pipeline/pass_semantic_edges.c b/src/pipeline/pass_semantic_edges.c
index 7524c41d5..41c86f51a 100644
--- a/src/pipeline/pass_semantic_edges.c
+++ b/src/pipeline/pass_semantic_edges.c
@@ -17,6 +17,7 @@
#include "semantic/semantic.h"
#include "semantic/ast_profile.h"
#include "simhash/minhash.h"
+#include "foundation/hash_table.h"
#include "foundation/log.h"
#include "foundation/compat.h"
#define XXH_INLINE_ALL
@@ -600,10 +601,14 @@ typedef struct {
int *token_counts; /* output: token count per function */
int func_count;
_Atomic int next_idx;
+ /* Per-worker token intern pools (key==value==the one owned strdup):
+ * identical tokens ("xfs", "error", ...) recur across hundreds of
+ * thousands of functions; per-func strdups made all_tokens hold every
+ * instance. Interned, it holds at most workers x unique tokens. */
+ CBMHashTable **pools;
} tokenize_ctx_t;
static void tokenize_worker(int worker_id, void *ctx_ptr) {
- (void)worker_id;
tokenize_ctx_t *tc = ctx_ptr;
while (true) {
int f = atomic_fetch_add_explicit(&tc->next_idx, SKIP_ONE, memory_order_relaxed);
@@ -619,6 +624,18 @@ static void tokenize_worker(int worker_id, void *ctx_ptr) {
char **dst = &tc->all_tokens[(ptrdiff_t)f * CBM_SEM_MAX_TOKENS];
int count = tokenize_node(n, tc->gbuf, dst, CBM_SEM_MAX_TOKENS);
count = inject_pattern_tokens(n, tc->gbuf, dst, count, CBM_SEM_MAX_TOKENS);
+ if (tc->pools && tc->pools[worker_id]) {
+ CBMHashTable *pool = tc->pools[worker_id];
+ for (int t = 0; t < count; t++) {
+ char *canon = cbm_ht_get(pool, dst[t]);
+ if (canon) {
+ free(dst[t]);
+ dst[t] = canon;
+ } else {
+ cbm_ht_set(pool, dst[t], dst[t]); /* key borrows the value */
+ }
+ }
+ }
tc->token_counts[f] = count;
}
}
@@ -1144,13 +1161,15 @@ static void phase1b_decode_and_build(cbm_sem_func_t *funcs, const cbm_gbuf_node_
/* Phase 2: tokenize each function's metadata in parallel, filling
* all_tokens[] and token_counts[]. Caller allocates the arrays. */
static void phase2_tokenize(const cbm_gbuf_node_t **node_ptrs, cbm_gbuf_t *gbuf, char **all_tokens,
- int *token_counts, int func_count, int worker_count) {
+ int *token_counts, int func_count, int worker_count,
+ CBMHashTable **pools) {
tokenize_ctx_t tc = {
.node_ptrs = node_ptrs,
.gbuf = gbuf,
.all_tokens = all_tokens,
.token_counts = token_counts,
.func_count = func_count,
+ .pools = pools,
};
atomic_init(&tc.next_idx, 0);
cbm_parallel_for_opts_t opts = {.max_workers = worker_count, .force_pthreads = false};
@@ -1307,18 +1326,33 @@ static int run_scoring_phase(cbm_gbuf_t *gbuf, cbm_sem_func_t *funcs, uint64_t *
}
/* Free the per-function arrays malloc'd during phases 1b and 4a. */
+static void free_token_pool_entry(const char *key, void *value, void *ud) {
+ (void)key; /* key == value: one owned string per unique token */
+ (void)ud;
+ free(value);
+}
+
+/* all_tokens slots BORROW their strings from the per-worker intern pools;
+ * the pools own exactly one copy per unique token per worker. */
static void free_funcs_and_tokens(cbm_sem_func_t *funcs, int func_count, char **all_tokens,
- const int *token_counts) {
+ const int *token_counts, CBMHashTable **pools,
+ int worker_count) {
+ (void)token_counts;
for (int f = 0; f < func_count; f++) {
free(funcs[f].tfidf_indices);
free(funcs[f].tfidf_weights);
- int tc = token_counts[f];
- for (int t = 0; t < tc; t++) {
- free(all_tokens[((ptrdiff_t)f * CBM_SEM_MAX_TOKENS) + t]);
- }
}
free(all_tokens);
free(funcs);
+ if (pools) {
+ for (int w = 0; w < worker_count; w++) {
+ if (pools[w]) {
+ cbm_ht_foreach(pools[w], free_token_pool_entry, NULL);
+ cbm_ht_free(pools[w]);
+ }
+ }
+ free(pools);
+ }
}
/* ── Pass entry point ────────────────────────────────────────────── */
@@ -1356,7 +1390,14 @@ int cbm_pipeline_pass_semantic_edges(cbm_pipeline_ctx_t *ctx) {
int *token_counts = calloc((size_t)func_count, sizeof(int));
CBM_PROF_START(t_phase2);
- phase2_tokenize(node_ptrs, gbuf, all_tokens, token_counts, func_count, worker_count);
+ CBMHashTable **token_pools = calloc((size_t)worker_count, sizeof(CBMHashTable *));
+ if (token_pools) {
+ for (int w = 0; w < worker_count; w++) {
+ token_pools[w] = cbm_ht_create(CBM_SZ_1K);
+ }
+ }
+ phase2_tokenize(node_ptrs, gbuf, all_tokens, token_counts, func_count, worker_count,
+ token_pools);
CBM_PROF_END_N("semantic_edges", "2_tokenize_parallel", t_phase2, func_count);
free(node_ptrs);
@@ -1391,7 +1432,8 @@ int cbm_pipeline_pass_semantic_edges(cbm_pipeline_ctx_t *ctx) {
free_lsh_buckets(band_buckets);
free(signatures);
cbm_log_info("pass.done", "pass", "semantic_edges", "edges", itoa_log(total_edges));
- free_funcs_and_tokens(funcs, func_count, all_tokens, token_counts);
+ free_funcs_and_tokens(funcs, func_count, all_tokens, token_counts, token_pools,
+ worker_count);
free(token_counts);
cbm_sem_corpus_free(corpus);
CBM_PROF_END("semantic_edges", "7_cleanup", t_phase7);
diff --git a/src/pipeline/registry.c b/src/pipeline/registry.c
index 50fb881cb..4b70f67d4 100644
--- a/src/pipeline/registry.c
+++ b/src/pipeline/registry.c
@@ -75,6 +75,11 @@ const char *cbm_confidence_band(double score) {
typedef CBM_DYN_ARRAY(char *) qn_array_t;
struct cbm_registry {
+ /* Interned label strings (<=~30 distinct labels; owned here, freed in
+ * _free). The exact map's VALUES point into this pool instead of one
+ * strdup per registered definition (~8.5M strdups on the kernel). */
+ char *label_pool[64];
+ int label_pool_n;
/* exact: qualifiedName → label string (heap-owned copies) */
CBMHashTable *exact;
@@ -464,17 +469,15 @@ cbm_registry_t *cbm_registry_new(void) {
static void free_label(const char *key, void *value, void *ud) {
(void)ud;
+ (void)value; /* interned in the registry's label_pool */
free((void *)key);
- free(value);
}
static void free_qn_array(const char *key, void *value, void *ud) {
(void)ud;
qn_array_t *arr = value;
if (arr) {
- for (int i = 0; i < arr->count; i++) {
- free(arr->items[i]);
- }
+ /* items borrow the exact map's keys — freed there, not here */
cbm_da_free(arr);
free(arr);
}
@@ -485,10 +488,14 @@ void cbm_registry_free(cbm_registry_t *r) {
if (!r) {
return;
}
- cbm_ht_foreach(r->exact, free_label, NULL);
- cbm_ht_free(r->exact);
+ /* by_name first: its items borrow exact's keys. */
cbm_ht_foreach(r->by_name, free_qn_array, NULL);
cbm_ht_free(r->by_name);
+ cbm_ht_foreach(r->exact, free_label, NULL);
+ cbm_ht_free(r->exact);
+ for (int i = 0; i < r->label_pool_n; i++) {
+ free(r->label_pool[i]);
+ }
free(r);
}
@@ -506,8 +513,28 @@ void cbm_registry_add(cbm_registry_t *r, const char *name, const char *qualified
return;
}
- /* Store in exact map: QN → label */
- cbm_ht_set(r->exact, strdup(qualified_name), strdup(label));
+ /* Intern the label (bounded set; linear scan is fine at this size). */
+ const char *interned = NULL;
+ for (int i = 0; i < r->label_pool_n; i++) {
+ if (strcmp(r->label_pool[i], label) == 0) {
+ interned = r->label_pool[i];
+ break;
+ }
+ }
+ if (!interned && r->label_pool_n < (int)(sizeof(r->label_pool) / sizeof(r->label_pool[0]))) {
+ r->label_pool[r->label_pool_n] = strdup(label);
+ interned = r->label_pool[r->label_pool_n];
+ r->label_pool_n++;
+ }
+ if (!interned) {
+ return; /* pool exhausted (cannot happen with sane label sets) */
+ }
+
+ /* Store in exact map: QN → interned label. The key is the registry's ONE
+ * owned copy of the QN; by_name below borrows it (same lifetime) instead
+ * of a second strdup — this pair of copies was ~280 MB on the kernel. */
+ cbm_ht_set(r->exact, strdup(qualified_name), (void *)interned);
+ const char *owned_qn = cbm_ht_get_key(r->exact, qualified_name);
/* Index by simple name.
* No array dedup needed: exact-map check above guarantees uniqueness. */
@@ -517,7 +544,7 @@ void cbm_registry_add(cbm_registry_t *r, const char *name, const char *qualified
arr = calloc(CBM_ALLOC_ONE, sizeof(qn_array_t));
cbm_ht_set(r->by_name, strdup(simple), arr);
}
- cbm_da_push(arr, strdup(qualified_name));
+ cbm_da_push(arr, (char *)owned_qn);
}
/* ── Lookup ──────────────────────────────────────────────────────── */