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: cut the allocation churn and repeated work the sanitizer found

Martin Vogel959f4de50cca
diff --git a/internal/cbm/cbm.c b/internal/cbm/cbm.c
index 52614d74c..e88354680 100644
--- a/internal/cbm/cbm.c
+++ b/internal/cbm/cbm.c
@@ -3,6 +3,7 @@
* guard, so whichever is included first is the one this file sees. */
#include "foundation/arena.h" // cbm_arena_init_sized
#include "foundation/mem_core.h" // class accounting for the bound allocators
+#include "foundation/mem_events.h" // waste sanitizer: the bound allocators bypass every observer
#include "foundation/log.h" // cbm_log_warn -- extract.lsp.skipped
#include "cbm.h"
#include "arena.h" // CBMArena, cbm_arena_init/alloc/strdup/destroy
@@ -347,28 +348,55 @@ static mi_heap_t *sqlite_heap(void) {
return tl_sqlite_heap;
}
+#if defined(CBM_MEMWASTE) && CBM_MEMWASTE
+#define BOUND_ALLOC(block, n, flags) \
+ do { \
+ if ((block) && cbm_memev_enabled()) { \
+ cbm_memev_alloc_ex((block), (n), mi_usable_size(block), \
+ __builtin_return_address(0), (unsigned)(flags)); \
+ } \
+ } while (0)
+#define BOUND_REALLOC(old_block, grown, n) \
+ do { \
+ if ((grown) && cbm_memev_enabled()) { \
+ cbm_memev_realloc((old_block), (grown), (n), mi_usable_size(grown), \
+ __builtin_return_address(0)); \
+ } \
+ } while (0)
+#define BOUND_FREE(block) cbm_memev_free(block)
+#else
+#define BOUND_ALLOC(block, n, flags) ((void)0)
+#define BOUND_REALLOC(old_block, grown, n) ((void)0)
+#define BOUND_FREE(block) ((void)0)
+#endif
+
static void *cbm_sqlite_malloc(int n) {
mi_heap_t *heap = sqlite_heap();
void *block = heap ? mi_heap_malloc(heap, (size_t)n) : mi_malloc((size_t)n);
if (block) {
cbm_mem_class_add_external(CBM_MEM_CLASS_STORE, mi_usable_size(block));
}
+ BOUND_ALLOC(block, (size_t)n, 0);
return block;
}
static void cbm_sqlite_free(void *p) {
if (p) {
cbm_mem_class_remove_external(CBM_MEM_CLASS_STORE, mi_usable_size(p));
+ BOUND_FREE(p);
}
mi_free(p);
}
static void *cbm_sqlite_realloc(void *p, int n) {
size_t old_size = p ? mi_usable_size(p) : 0;
mi_heap_t *heap = sqlite_heap();
+ CBM_MEMEV_BACKING(1);
void *grown = heap ? mi_heap_realloc(heap, p, (size_t)n) : mi_realloc(p, (size_t)n);
+ CBM_MEMEV_BACKING(-1);
if (grown) {
cbm_mem_class_remove_external(CBM_MEM_CLASS_STORE, old_size);
cbm_mem_class_add_external(CBM_MEM_CLASS_STORE, mi_usable_size(grown));
}
+ BOUND_REALLOC(p, grown, (size_t)n);
return grown;
}
static int cbm_sqlite_size(void *p) {
@@ -384,6 +412,7 @@ static void *cbm_ts_malloc(size_t n) {
if (block) {
cbm_mem_class_add_external(CBM_MEM_CLASS_TS_TREE, mi_usable_size(block));
}
+ BOUND_ALLOC(block, n, 0);
return block;
}
static void *cbm_ts_calloc(size_t count, size_t size) {
@@ -391,20 +420,25 @@ static void *cbm_ts_calloc(size_t count, size_t size) {
if (block) {
cbm_mem_class_add_external(CBM_MEM_CLASS_TS_TREE, mi_usable_size(block));
}
+ BOUND_ALLOC(block, count * size, CBM_MEMEV_ZEROED);
return block;
}
static void *cbm_ts_realloc(void *p, size_t n) {
size_t old_size = p ? mi_usable_size(p) : 0;
+ CBM_MEMEV_BACKING(1);
void *grown = mi_realloc(p, n);
+ CBM_MEMEV_BACKING(-1);
if (grown) {
cbm_mem_class_remove_external(CBM_MEM_CLASS_TS_TREE, old_size);
cbm_mem_class_add_external(CBM_MEM_CLASS_TS_TREE, mi_usable_size(grown));
}
+ BOUND_REALLOC(p, grown, n);
return grown;
}
static void cbm_ts_free(void *p) {
if (p) {
cbm_mem_class_remove_external(CBM_MEM_CLASS_TS_TREE, mi_usable_size(p));
+ BOUND_FREE(p);
}
mi_free(p);
}
@@ -477,6 +511,87 @@ void cbm_reset_thread_parser(void) {
}
}
+enum { FIELD_CACHE_SLOTS = 512, FIELD_NAME_CACHED_MAX = 31 };
+typedef struct {
+ const TSLanguage *lang;
+ uint32_t len;
+ TSFieldId id;
+ char name[FIELD_NAME_CACHED_MAX + 1];
+} field_id_slot_t;
+static CBM_TLS field_id_slot_t tl_field_ids[FIELD_CACHE_SLOTS];
+
+TSNode cbm_ts_child_by_field_name(TSNode node, const char *name, uint32_t name_length) {
+ if (!name || name_length == 0 || name_length > FIELD_NAME_CACHED_MAX || ts_node_is_null(node)) {
+ return (ts_node_child_by_field_name)(node, name, name_length); /* the real one */
+ }
+ const TSLanguage *lang = ts_node_language(node);
+ uint64_t h = 0xcbf29ce484222325ULL ^ (uint64_t)(uintptr_t)lang;
+ for (uint32_t i = 0; i < name_length; i++) {
+ h ^= (uint8_t)name[i];
+ h *= 0x100000001b3ULL;
+ }
+ field_id_slot_t *slot = &tl_field_ids[h & (FIELD_CACHE_SLOTS - 1)];
+ if (slot->lang != lang || slot->len != name_length ||
+ memcmp(slot->name, name, name_length) != 0) {
+ /* The content is compared, never just the pointer: callers also pass
+ * names built in reused buffers. */
+ slot->id = ts_language_field_id_for_name(lang, name, name_length);
+ slot->lang = lang;
+ slot->len = name_length;
+ memcpy(slot->name, name, name_length);
+ }
+ return ts_node_child_by_field_id(node, slot->id);
+}
+
+static CBM_TLS TSTreeCursor tl_cursor;
+static CBM_TLS bool tl_cursor_live = false;
+
+TSTreeCursor *cbm_thread_cursor(TSNode node) {
+ if (tl_cursor_live) {
+ ts_tree_cursor_reset(&tl_cursor, node);
+ } else {
+ tl_cursor = ts_tree_cursor_new(node);
+ tl_cursor_live = true;
+ }
+ return &tl_cursor;
+}
+
+/* Per-depth cursors for recursive walks (cbm_cursor_acquire). A walk at depth d
+ * holds slot d while it recurses into depth d + 1; the LSP call walkers used to
+ * create and delete a cursor at EVERY node -- 49.8 M cursor stacks on the Go
+ * corpus (waste sanitizer, 2026-09-17). Deeper than the pool: private cursors. */
+enum { CURSOR_POOL_DEPTH = 128 };
+static CBM_TLS TSTreeCursor tl_cursor_pool[CURSOR_POOL_DEPTH];
+static CBM_TLS uint8_t tl_cursor_pool_state[CURSOR_POOL_DEPTH]; /* bit0 live, bit1 busy */
+enum { CURSOR_LIVE = 1, CURSOR_BUSY = 2 };
+
+TSTreeCursor *cbm_cursor_acquire(cbm_cursor_lease_t *lease, int depth, TSNode node) {
+ if (depth >= 0 && depth < CURSOR_POOL_DEPTH && !(tl_cursor_pool_state[depth] & CURSOR_BUSY)) {
+ if (tl_cursor_pool_state[depth] & CURSOR_LIVE) {
+ ts_tree_cursor_reset(&tl_cursor_pool[depth], node);
+ } else {
+ tl_cursor_pool[depth] = ts_tree_cursor_new(node);
+ }
+ tl_cursor_pool_state[depth] = CURSOR_LIVE | CURSOR_BUSY;
+ lease->slot = depth;
+ lease->cursor = &tl_cursor_pool[depth];
+ return lease->cursor;
+ }
+ lease->private_cursor = ts_tree_cursor_new(node);
+ lease->slot = -1;
+ lease->cursor = &lease->private_cursor;
+ return lease->cursor;
+}
+
+void cbm_cursor_release(cbm_cursor_lease_t *lease) {
+ if (lease->slot < 0) {
+ ts_tree_cursor_delete(&lease->private_cursor);
+ } else {
+ tl_cursor_pool_state[lease->slot] &= (uint8_t)~CURSOR_BUSY;
+ }
+ lease->cursor = NULL;
+}
+
void cbm_destroy_thread_parser(void) {
// Full cleanup: delete the parser. Call on worker thread exit.
if (tl_parser) {
@@ -484,6 +599,17 @@ void cbm_destroy_thread_parser(void) {
tl_parser = NULL;
tl_parser_lang = CBM_LANG_COUNT;
}
+ if (tl_cursor_live) {
+ ts_tree_cursor_delete(&tl_cursor);
+ tl_cursor_live = false;
+ }
+ for (int d = 0; d < CURSOR_POOL_DEPTH; d++) {
+ /* a busy slot belongs to a walk still on this stack: leave it */
+ if (tl_cursor_pool_state[d] == CURSOR_LIVE) {
+ ts_tree_cursor_delete(&tl_cursor_pool[d]);
+ tl_cursor_pool_state[d] = 0;
+ }
+ }
}
void cbm_shutdown(void) {
@@ -1635,6 +1761,7 @@ CBMFileResult *cbm_extract_file(const char *source, int source_len, CBMLanguage
* that bound and so a singleton OS allocation. One file in twelve thousand
* pays it, which is why the cost is accepted. */
enum { CBM_EXTRACT_SCRATCH_BLOCK = CBM_SZ_512 * CBM_SZ_1K };
+enum { CBM_EXTRACT_SCRATCH_KEEP_BYTES = 4 * CBM_SZ_1K * CBM_SZ_1K };
static CBMFileResult *extract_file_ex_body(const char *source, int source_len, CBMLanguage language,
const char *project, const char *rel_path,
@@ -2274,6 +2401,17 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
static CBM_TLS CBMArena tl_work_arena;
static CBM_TLS bool tl_work_arena_live = false;
+/* The traversal scratch arena is kept per worker the same way. A thread keeps
+ * one only after it has given a working arena back (so only pipeline workers,
+ * which call cbm_work_arena_release when they end), and only while it holds no
+ * more than CBM_EXTRACT_SCRATCH_KEEP_BYTES (an outsized file's arena is not
+ * worth holding). Before this every file created and destroyed a
+ * 512 KB block -- 28,145 of them on the Go corpus, 14 GB allocated, 99.9 %
+ * never written and 7,136 never touched (waste sanitizer, 2026-09-17). */
+static CBM_TLS CBMArena tl_scratch_arena;
+static CBM_TLS bool tl_scratch_live = false;
+static CBM_TLS bool tl_scratch_keep = false;
+
void cbm_work_arena_take(CBMArena *into) {
if (tl_work_arena_live) {
*into = tl_work_arena;
@@ -2289,12 +2427,27 @@ void cbm_work_arena_release(void) {
cbm_arena_destroy(&tl_work_arena);
tl_work_arena_live = false;
}
+ if (tl_scratch_live) {
+ cbm_arena_destroy(&tl_scratch_arena);
+ tl_scratch_live = false;
+ }
+ cbm_result_compact_release_thread();
+ tl_scratch_keep = false;
+}
+
+bool cbm_work_arena_keeping(void) {
+ return tl_scratch_keep;
+}
+
+void cbm_work_arena_keep_begin(void) {
+ tl_scratch_keep = true;
}
void cbm_work_arena_give(CBMArena *from) {
if (!from || from->nblocks == 0) {
return;
}
+ tl_scratch_keep = true; /* a pipeline worker: its release call will come */
if (tl_work_arena_live || cbm_arena_capacity(from) > (size_t)CBM_WORK_ARENA_KEEP_BYTES) {
cbm_arena_destroy(from);
return;
@@ -2307,20 +2460,41 @@ void cbm_work_arena_give(CBMArena *from) {
/* Public entry. Owns the traversal scratch arena for the whole of one file's
* extraction: created here, handed to the body as ctx->scratch, destroyed on
* the way out. The body has seven early returns, so bracketing it in a wrapper
- * is what keeps that to one create and one destroy. If the arena cannot be
- * created, the body is handed NULL and the traversal stacks fall back to the
- * result arena, which is what shipped before #1997. */
+ * is what keeps that to one create and one destroy. The arena is LAZY: it takes
+ * its first block when a traversal stack first needs one, so a file that builds
+ * no stack (most non-code files) costs nothing -- opened eagerly, 3.3 GB of
+ * 512 KB blocks were never read or written on the Go corpus (waste sanitizer,
+ * 2026-09-17). If the block cannot be allocated, the stack's allocation fails
+ * and it stops growing, exactly as on any later out-of-memory. */
CBMFileResult *cbm_extract_file_ex(const char *source, int source_len, CBMLanguage language,
const char *project, const char *rel_path,
int64_t timeout_micros, const char **extra_defines,
const char **include_paths, const CBMMacroTable *macro_table,
const CBMReturnTypeTable *return_type_table) {
CBMArena scratch;
- cbm_arena_init_sized(&scratch, CBM_EXTRACT_SCRATCH_BLOCK);
- CBMFileResult *result = extract_file_ex_body(
- source, source_len, language, project, rel_path, timeout_micros, extra_defines,
- include_paths, macro_table, return_type_table, scratch.nblocks > 0 ? &scratch : NULL);
- cbm_arena_destroy(&scratch);
+ if (tl_scratch_live) {
+ scratch = tl_scratch_arena;
+ tl_scratch_live = false;
+ cbm_arena_rewind(&scratch);
+ } else {
+ cbm_arena_init_lazy(&scratch, CBM_EXTRACT_SCRATCH_BLOCK);
+ }
+ CBMFileResult *result =
+ extract_file_ex_body(source, source_len, language, project, rel_path, timeout_micros,
+ extra_defines, include_paths, macro_table, return_type_table, &scratch);
+ /* !tl_scratch_live: a nested extraction (an embedded language inside this
+ * file) may already have parked its own; never overwrite it. */
+ /* Kept up to CBM_EXTRACT_SCRATCH_KEEP_BYTES, grown blocks included: the
+ * definitions walk draws its frames from this arena too, so a file with
+ * many definitions grows it past the first block, and dropping every grown
+ * arena turned those files into fresh 512 KB blocks for the next file. */
+ if (tl_scratch_keep && !tl_scratch_live && scratch.nblocks > 0 &&
+ cbm_arena_capacity(&scratch) <= (size_t)CBM_EXTRACT_SCRATCH_KEEP_BYTES) {
+ tl_scratch_arena = scratch;
+ tl_scratch_live = true;
+ } else {
+ cbm_arena_destroy(&scratch);
+ }
return result;
}
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h
index d875459c1..a0e61a5e4 100644
--- a/internal/cbm/cbm.h
+++ b/internal/cbm/cbm.h
@@ -6,6 +6,17 @@
#include "arena.h"
#include "tree_sitter/api.h"
+/* Field lookups by NAME resolve the name with a linear strncmp scan over the
+ * grammar's field table on every call -- 951 M strncmp calls on the Go corpus
+ * (waste sanitizer, 2026-09-17) across ~1,200 call sites. Tree-sitter's own
+ * implementation is exactly "field id for name, then child by field id"; this
+ * caches the first half per thread (language, name) and keeps the second.
+ * Every extractor includes this header, so every call site gets it. */
+TSNode cbm_ts_child_by_field_name(TSNode node, const char *name, uint32_t name_length);
+/* Variadic: call sites spell the name and its length as ONE macro argument
+ * (TS_FIELD("body") expands to "body", 4), which must expand before the call. */
+#define ts_node_child_by_field_name(...) cbm_ts_child_by_field_name(__VA_ARGS__)
+
// Language enum mirrors lang.Language in Go.
// Order must match lang_specs.c tables.
typedef enum {
@@ -729,6 +740,16 @@ void cbm_work_arena_take(CBMArena *into);
void cbm_work_arena_give(CBMArena *from);
/* Drop this thread's kept working arena (end of an extraction pass). */
void cbm_work_arena_release(void);
+/* True on a thread that has given a working arena back, i.e. a pipeline
+ * worker whose cbm_work_arena_release is guaranteed to run: only such a thread
+ * may keep per-thread scratch between files. */
+bool cbm_work_arena_keeping(void);
+/* Let this thread keep its per-file extraction scratch between the files of a
+ * sequential loop; the caller must call cbm_work_arena_release on the same
+ * thread when the loop ends (every return path). */
+void cbm_work_arena_keep_begin(void);
+/* Free the compaction scratch this thread kept (cbm_work_arena_release calls it). */
+void cbm_result_compact_release_thread(void);
CBMFileResult *cbm_extract_file(const char *source, int source_len, CBMLanguage language,
const char *project, const char *rel_path, int64_t timeout_micros,
@@ -771,6 +792,25 @@ void cbm_reset_thread_parser(void);
// Destroy the thread-local parser. Call on worker thread exit.
void cbm_destroy_thread_parser(void);
+// This thread's reusable tree cursor, reset to `node`. Child walks run once per
+// visited node; a cursor per walk was a malloc + free of its stack each time
+// (49.8 M on the Go corpus, waste sanitizer 2026-09-17). Valid until the next
+// call on this thread; released with the thread parser.
+TSTreeCursor *cbm_thread_cursor(TSNode node);
+
+// Reusable cursors for RECURSIVE walks: one per recursion depth on this thread,
+// so a walk that recurses from inside its child loop never shares a cursor with
+// its own callers. A slot already in use (another walker nested on this thread)
+// hands out a private cursor instead; release deletes it. Released with the
+// thread parser.
+typedef struct {
+ TSTreeCursor *cursor;
+ TSTreeCursor private_cursor;
+ int slot; /* -1: private */
+} cbm_cursor_lease_t;
+TSTreeCursor *cbm_cursor_acquire(cbm_cursor_lease_t *lease, int depth, TSNode node);
+void cbm_cursor_release(cbm_cursor_lease_t *lease);
+
// Shutdown the library. Call once at exit.
void cbm_shutdown(void);
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index b49147dc6..93da084be 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -7053,6 +7053,13 @@ typedef struct {
int cap;
const char *path; // for the WARN when the ceiling is hit (may be NULL)
bool warned;
+ /* The per-file traversal scratch (ctx->scratch) when there is one: frames
+ * then come from memory the thread reuses file after file, where a malloc
+ * of 256 frames per file was 28 k allocations and 255 MB never written on
+ * the Go corpus (waste sanitizer, 2026-09-17). Growth copies into a
+ * doubled buffer and abandons the old one to the arena, like TSNodeStack.
+ * NULL: the heap, freed by the walk. */
+ CBMArena *arena;
} wd_stack_t;
// Generous safety ceiling (frames), env-overridable via CBM_WALK_DEFS_MAX.
@@ -7082,7 +7089,16 @@ static void wd_push(wd_stack_t *s, TSNode node, const char *enclosing_qn) {
}
return; // bounded: stop growing (warned, not silent)
}
- walk_defs_frame_t *nd = safe_realloc(s->data, (size_t)ncap * sizeof(walk_defs_frame_t));
+ walk_defs_frame_t *nd = NULL;
+ if (s->arena) {
+ nd = (walk_defs_frame_t *)cbm_arena_alloc(s->arena,
+ (size_t)ncap * sizeof(walk_defs_frame_t));
+ if (nd && s->top > 0) {
+ memcpy(nd, s->data, (size_t)s->top * sizeof(walk_defs_frame_t));
+ }
+ } else {
+ nd = safe_realloc(s->data, (size_t)ncap * sizeof(walk_defs_frame_t));
+ }
if (!nd) {
/* OOM — safe_realloc already freed the old buffer. Bail cleanly: drop
* pending frames so the walk_defs loop drains and exits without a NULL
@@ -7747,6 +7763,7 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
(void)depth_unused;
wd_stack_t s = {0};
s.path = ctx->rel_path;
+ s.arena = ctx->scratch;
wd_push(&s, root, ctx->enclosing_class_qn);
while (s.top > 0) {
@@ -7899,7 +7916,9 @@ static void walk_defs(CBMExtractCtx *ctx, TSNode root, const CBMLangSpec *spec,
* collection is mandatory (see wd_push_children_reverse). */
wd_push_children_reverse(&s, node, frame.enclosing_class_qn);
}
- free(s.data);
+ if (!s.arena) {
+ free(s.data);
+ }
}
void cbm_extract_definitions_without_module(CBMExtractCtx *ctx) {
diff --git a/internal/cbm/extract_node_stack.h b/internal/cbm/extract_node_stack.h
index 0013d53b1..30a27968b 100644
--- a/internal/cbm/extract_node_stack.h
+++ b/internal/cbm/extract_node_stack.h
@@ -12,9 +12,9 @@
*
* Growth abandons the old buffer in that scratch arena, which is free because
* the arena dies with the file. The initial capacities below are unchanged, but
- * they are no longer what a small file costs: cbm_extract_file_ex creates the
- * scratch arena's first block for every call, including calls that build no
- * stack at all, and reclaims it on the way out.
+ * they are no longer what a small file costs: cbm_extract_file_ex hands every
+ * call a lazy scratch arena, whose first block is taken by the first stack
+ * built, so a call that builds no stack allocates nothing.
*/
#ifndef CBM_EXTRACT_NODE_STACK_H
#define CBM_EXTRACT_NODE_STACK_H
@@ -84,13 +84,14 @@ static inline TSNode ts_nstack_pop(TSNodeStack *s) {
*/
static inline void ts_nstack_push_children(TSNodeStack *s, TSNode node) {
int base = s->count;
- TSTreeCursor cursor = ts_tree_cursor_new(node);
- if (ts_tree_cursor_goto_first_child(&cursor)) {
+ /* The thread's reusable cursor (cbm_thread_cursor): the walk completes
+ * before anything else on this thread can ask for it. */
+ TSTreeCursor *cursor = cbm_thread_cursor(node);
+ if (ts_tree_cursor_goto_first_child(cursor)) {
do {
- ts_nstack_push(s, ts_tree_cursor_current_node(&cursor));
- } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ ts_nstack_push(s, ts_tree_cursor_current_node(cursor));
+ } while (ts_tree_cursor_goto_next_sibling(cursor));
}
- ts_tree_cursor_delete(&cursor);
/* Reverse [base, count) so the first child pops first (forward order). */
int lo = base, hi = s->count - 1;
while (lo < hi) {
diff --git a/internal/cbm/lsp/go_lsp.c b/internal/cbm/lsp/go_lsp.c
index 7fec20a9f..4518bf243 100644
--- a/internal/cbm/lsp/go_lsp.c
+++ b/internal/cbm/lsp/go_lsp.c
@@ -1,9 +1,11 @@
#include "go_lsp.h"
#include "lsp_node_iter.h"
+#include "../../../src/foundation/hash_table.h"
#include "../helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdatomic.h>
// Forward declarations
static void resolve_calls_in_node_inner(GoLSPContext* ctx, TSNode node);
@@ -1836,14 +1838,17 @@ recurse:;
// Recurse into children via a cursor (O(n)); ts_node_child(node,i) is O(i)
// → O(n²) on a wide node.
+ // The cursor is this depth's pooled one (cbm_cursor_acquire): the recursion
+ // below runs one level deeper and never touches it.
{
- TSTreeCursor cursor = ts_tree_cursor_new(node);
- if (ts_tree_cursor_goto_first_child(&cursor)) {
+ cbm_cursor_lease_t lease;
+ TSTreeCursor* cursor = cbm_cursor_acquire(&lease, ctx->walk_depth, node);
+ if (ts_tree_cursor_goto_first_child(cursor)) {
do {
- resolve_calls_in_node(ctx, ts_tree_cursor_current_node(&cursor));
- } while (ts_tree_cursor_goto_next_sibling(&cursor));
+ resolve_calls_in_node(ctx, ts_tree_cursor_current_node(cursor));
+ } while (ts_tree_cursor_goto_next_sibling(cursor));
}
- ts_tree_cursor_delete(&cursor);
+ cbm_cursor_release(&lease);
}
if (push_scope) {
@@ -2059,6 +2064,60 @@ static const CBMType *cbm_go_lsp_parse_signature_param_text(CBMArena *arena, con
return cbm_parse_return_type_text(arena, text, module_qn);
}
+// --- Shared Go stdlib registry ---
+
+/* The Go stdlib (2,328 functions, 321 types) registered ONCE per process into
+ * a finalized, sealed registry that every per-file registry chains to as its
+ * fallback -- the overlay contract of type_registry.h: lookups and the chain
+ * iterators reach it, refinements copy into the head, nothing writes it.
+ * Registered into each file's registry instead, it was 13.4 k re-registrations
+ * on the Go corpus, 2.5 GB of copies, and every lookup of the unfinalized
+ * per-file registry scanned the stdlib linearly (waste sanitizer, 2026-09-17).
+ * Threads racing the first build each build a copy and the first to publish
+ * wins; the others free theirs -- no thread waits. NULL on allocation
+ * failure: the caller registers the stdlib into its own registry as before. */
+typedef struct {
+ CBMArena arena;
+ CBMTypeRegistry reg;
+} go_stdlib_holder_t;
+
+static _Atomic(go_stdlib_holder_t *) g_go_stdlib_shared;
+
+static const CBMTypeRegistry* go_stdlib_shared(void) {
+ go_stdlib_holder_t* ready = atomic_load_explicit(&g_go_stdlib_shared, memory_order_acquire);
+ if (ready) return &ready->reg;
+ go_stdlib_holder_t* built = (go_stdlib_holder_t*)calloc(1, sizeof(*built));
+ if (!built) return NULL;
+ cbm_arena_init(&built->arena);
+ if (built->arena.nblocks == 0) {
+ free(built);
+ return NULL;
+ }
+ cbm_registry_init(&built->reg, &built->arena);
+ cbm_go_stdlib_register(&built->reg, &built->arena);
+ cbm_registry_finalize(&built->reg);
+ built->reg.read_only = true;
+ go_stdlib_holder_t* expected = NULL;
+ if (atomic_compare_exchange_strong_explicit(&g_go_stdlib_shared, &expected, built,
+ memory_order_acq_rel, memory_order_acquire)) {
+ return &built->reg;
+ }
+ cbm_arena_destroy(&built->arena);
+ free(built);
+ return &expected->reg;
+}
+
+/* A per-file registry sees the stdlib: chained to the shared copy, or
+ * registered into it when the shared copy cannot be built. */
+static void go_registry_with_stdlib(CBMTypeRegistry* reg, CBMArena* arena) {
+ const CBMTypeRegistry* shared = go_stdlib_shared();
+ if (shared) {
+ reg->fallback = shared;
+ } else {
+ cbm_go_stdlib_register(reg, arena);
+ }
+}
+
// --- Entry point: build registry from file defs + run LSP ---
void cbm_run_go_lsp(CBMArena* arena, CBMFileResult* result,
@@ -2067,8 +2126,8 @@ void cbm_run_go_lsp(CBMArena* arena, CBMFileResult* result,
CBMTypeRegistry reg;
cbm_registry_init(&reg, arena);
- // Register Go stdlib types/functions
- cbm_go_stdlib_register(&reg, arena);
+ // Go stdlib types/functions: the shared sealed registry, chained
+ go_registry_with_stdlib(&reg, arena);
const char* module_qn = result->module_qn;
@@ -2472,9 +2531,10 @@ static const char** split_pipe_strings(CBMArena* a, const char* text) {
// Helper: parse "|"-separated "name:type" field definitions and populate a registered type.
// Format: "Binder:Binder|Name:string|Count:int"
// type_text is resolved relative to def_module_qn.
-static void parse_field_defs_into_type(CBMArena* arena, CBMTypeRegistry* reg,
- const char* type_qn, const char* field_defs, const char* def_module_qn) {
- if (!field_defs || !field_defs[0] || !type_qn) return;
+/* Parse "name:type|name:type" into NULL-terminated name/type arrays. False when
+ * nothing parsed (or on allocation failure): the type keeps no field info. */
+static bool parse_field_defs(CBMArena* arena, const char* field_defs, const char* def_module_qn,
+ const char*** names_out, const CBMType*** types_out) {
// Count fields
int count = 1;
@@ -2485,7 +2545,7 @@ static void parse_field_defs_into_type(CBMArena* arena, CBMTypeRegistry* reg,
const char** names = (const char**)cbm_arena_alloc(arena, (count + 1) * sizeof(const char*));
const CBMType** types = (const CBMType**)cbm_arena_alloc(arena, (count + 1) * sizeof(const CBMType*));
- if (!names || !types) return;
+ if (!names || !types) return false;
char* buf = cbm_arena_strdup(arena, field_defs);
int idx = 0;
@@ -2511,8 +2571,17 @@ static void parse_field_defs_into_type(CBMArena* arena, CBMTypeRegistry* reg,
}
names[idx] = NULL;
types[idx] = NULL;
+ *names_out = names;
+ *types_out = types;
+ return idx > 0;
+}
- if (idx > 0) {
+static void parse_field_defs_into_type(CBMArena* arena, CBMTypeRegistry* reg,
+ const char* type_qn, const char* field_defs, const char* def_module_qn) {
+ if (!field_defs || !field_defs[0] || !type_qn) return;
+ const char** names = NULL;
+ const CBMType** types = NULL;
+ if (parse_field_defs(arena, field_defs, def_module_qn, &names, &types)) {
// Find the registered type and update field info
for (int ti = 0; ti < reg->type_count; ti++) {
if (reg->types[ti].qualified_name &&
@@ -2964,10 +3033,10 @@ void cbm_run_go_lsp_cross(
}
TSNode root = ts_tree_root_node(tree);
- // 2. Build registry
+ // 2. Build registry (the stdlib is the shared sealed registry, chained)
CBMTypeRegistry reg;
cbm_registry_init(&reg, arena);
- cbm_go_stdlib_register(&reg, arena);
+ go_registry_with_stdlib(&reg, arena);
// Register all defs (file-local + cross-file).
// Perf: borrow strings from defs[] directly — they live in the
@@ -3227,6 +3296,27 @@ CBMTypeRegistry* cbm_go_build_cross_registry(
cbm_registry_init(reg, arena);
cbm_go_stdlib_register(reg, arena);
+ /* Qualified name -> FIRST type index while the registry is built: it is
+ * not finalized until the end, so its own lookup scans every type, and the
+ * receiver check below plus the field-definition lookup made the build
+ * O(types x (methods + structs)) -- 4.4 s single-threaded on the Go corpus,
+ * the scaling lane's parse_field_defs_into_type x2.9 (waste sanitizer,
+ * 2026-09-17). First-match semantics are kept exactly: a receiver's
+ * placeholder type added before its struct still receives the fields.
+ * NULL on allocation failure: the linear paths below run as before. */
+ CBMHashTable* first_type = cbm_ht_create((uint32_t)(def_count / 4 + 1024));
+ int noted_types = 0;
+#define GO_NOTE_NEW_TYPES() \
+ do { \
+ for (; first_type && noted_types < reg->type_count; noted_types++) { \
+ const char* tqn = reg->types[noted_types].qualified_name; \
+ if (tqn && !cbm_ht_has(first_type, tqn)) { \
+ cbm_ht_set(first_type, tqn, (void*)(uintptr_t)(noted_types + 1)); \
+ } \
+ } \
+ } while (0)
+ GO_NOTE_NEW_TYPES();
+
for (int i = 0; i < def_count; i++) {
CBMLSPDef* d = &defs[i];
if (!d->qualified_name || !d->short_name || !d->label) continue;
@@ -3250,9 +3340,20 @@ CBMTypeRegistry* cbm_go_build_cross_registry(
rt.method_names = split_pipe_strings(arena, d->method_names_str);
}
cbm_registry_add_type(reg, rt);
+ GO_NOTE_NEW_TYPES();
if (d->field_defs && d->field_defs[0]) {
- parse_field_defs_into_type(arena, reg, rt.qualified_name,
- d->field_defs, def_mod);
+ if (first_type) {
+ const char** names = NULL;
+ const CBMType** types = NULL;
+ uintptr_t at = (uintptr_t)cbm_ht_get(first_type, rt.qualified_name);
+ if (parse_field_defs(arena, d->field_defs, def_mod, &names, &types) && at) {
+ reg->types[at - 1].field_names = names;
+ reg->types[at - 1].field_types = types;
+ }
+ } else {
+ parse_field_defs_into_type(arena, reg, rt.qualified_name,
+ d->field_defs, def_mod);
+ }
}
}
@@ -3269,18 +3370,23 @@ CBMTypeRegistry* cbm_go_build_cross_registry(
rf.signature = cbm_type_func(arena, NULL, param_types, ret_types);
if (strcmp(d->label, "Method") == 0 && d->receiver_type && d->receiver_type[0]) {
rf.receiver_type = d->receiver_type;
- if (!cbm_registry_lookup_type(reg, rf.receiver_type)) {
+ bool known = first_type ? cbm_ht_has(first_type, rf.receiver_type)
+ : cbm_registry_lookup_type(reg, rf.receiver_type) != NULL;
+ if (!known) {
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 ? dot + 1 : rf.receiver_type;
cbm_registry_add_type(reg, auto_type);
+ GO_NOTE_NEW_TYPES();
}
}
cbm_registry_add_func(reg, rf);
}
}
+#undef GO_NOTE_NEW_TYPES
+ cbm_ht_free(first_type);
cbm_registry_finalize(reg);
reg->read_only = true; /* seal: shared Tier-2 registry is read-only during resolve */
diff --git a/internal/cbm/result_compact.c b/internal/cbm/result_compact.c
index 05470f6e1..47bf44e66 100644
--- a/internal/cbm/result_compact.c
+++ b/internal/cbm/result_compact.c
@@ -20,6 +20,7 @@
#include "cbm.h"
#include "foundation/arena.h"
+#include "foundation/compat.h" /* CBM_TLS */
#include "foundation/constants.h"
#include "foundation/mem_core.h"
#include "result_spill.h" /* cbm_result_relocate */
@@ -47,6 +48,14 @@ typedef struct {
bool failed;
cr_slot_t *slots;
size_t cap; /* power of two */
+ /* MEASURE records which slot each string reference landed in; COPY, which
+ * presents the very same references in the same order, replays them
+ * instead of measuring, hashing and comparing every string a second time
+ * (20 M repeated strlen + 31 M memcmp on the Go corpus, waste sanitizer
+ * 2026-09-17). */
+ uint32_t *seq;
+ size_t seq_len;
+ size_t seq_pos;
/* RELOCATE: pointers in [old_base, old_base + old_len) move by delta. */
const char *old_base;
size_t old_len;
@@ -131,8 +140,16 @@ static void cr_str(cr_ctx_t *c, const char **field) {
c->refs++;
return;
}
- cr_slot_t *slot = cr_slot(c, s);
+ cr_slot_t *slot;
+ if (c->phase == CR_COPY && c->seq && c->seq_pos < c->seq_len) {
+ slot = &c->slots[c->seq[c->seq_pos++]];
+ } else {
+ slot = cr_slot(c, s);
+ }
if (c->phase == CR_MEASURE) {
+ if (c->seq && c->seq_len < c->refs) {
+ c->seq[c->seq_len++] = (uint32_t)(slot - c->slots);
+ }
if (!slot->dst) {
slot->dst = (char *)s; /* mark as booked; reset before COPY */
c->bytes += cr_aligned(slot->len + SKIP_ONE);
@@ -390,6 +407,62 @@ static size_t cr_pow2_at_least(size_t n) {
return cap;
}
+/* The intern table and the replay sequence, kept per pipeline worker: a fresh
+ * zeroed table per file was 21,875 allocations and 2.0 GB of pure churn on the
+ * Go corpus (waste sanitizer, 2026-09-17). Kept only on threads whose
+ * cbm_work_arena_release is guaranteed to run (cbm_work_arena_keeping), and
+ * only up to CR_KEEP_BYTES; anything else allocates per call as before. */
+enum { CR_KEEP_BYTES = 8 * 1024 * 1024 };
+static CBM_TLS void *tl_cr_buf;
+static CBM_TLS size_t tl_cr_bytes;
+
+void cbm_result_compact_release_thread(void) {
+ cbm_free(CBM_MEM_CLASS_EXTRACT, tl_cr_buf);
+ tl_cr_buf = NULL;
+ tl_cr_bytes = 0;
+}
+
+/* One zeroed block holding the slot table followed by the sequence. Returns
+ * whether the block is the kept one (then the caller must not free it). */
+static bool cr_scratch_get(cr_ctx_t *c) {
+ size_t slot_bytes = c->cap * sizeof(cr_slot_t);
+ size_t bytes = slot_bytes + (c->refs * sizeof(uint32_t));
+ bool keep = bytes <= (size_t)CR_KEEP_BYTES && cbm_work_arena_keeping();
+ void *buf = NULL;
+ if (keep && tl_cr_buf && tl_cr_bytes >= bytes) {
+ buf = tl_cr_buf;
+ } else if (keep) {
+ size_t grown = CR_MIN_TABLE; /* powers of two: a growing file mix reallocates rarely */
+ while (grown < bytes) {
+ grown *= PAIR_LEN;
+ }
+ if (grown > (size_t)CR_KEEP_BYTES) {
+ grown = bytes;
+ }
+ cbm_result_compact_release_thread();
+ tl_cr_buf = cbm_alloc(CBM_MEM_CLASS_EXTRACT, grown);
+ tl_cr_bytes = tl_cr_buf ? grown : 0;
+ buf = tl_cr_buf;
+ } else {
+ buf = cbm_alloc(CBM_MEM_CLASS_EXTRACT, bytes);
+ }
+ if (!buf) {
+ return false;
+ }
+ memset(buf, 0, slot_bytes); /* MEASURE needs an empty table; the sequence is written before it is read */
+ c->slots = (cr_slot_t *)buf;
+ c->seq = c->refs ? (uint32_t *)((char *)buf + slot_bytes) : NULL;
+ return true;
+}
+
+static void cr_scratch_put(cr_ctx_t *c) {
+ if ((void *)c->slots != tl_cr_buf) {
+ cbm_free(CBM_MEM_CLASS_EXTRACT, c->slots);
+ }
+ c->slots = NULL;
+ c->seq = NULL;
+}
+
void cbm_result_compact(CBMFileResult *result) {
if (!result || result->arena.nblocks == 0) {
return;
@@ -405,8 +478,7 @@ void cbm_result_compact(CBMFileResult *result) {
cr_walk(&c, &tmp);
c.cap = cr_pow2_at_least(c.refs * CR_TABLE_LOAD + CR_MIN_TABLE);
- c.slots = (cr_slot_t *)cbm_calloc(CBM_MEM_CLASS_EXTRACT, c.cap * sizeof(cr_slot_t));
- if (!c.slots) {
+ if (!cr_scratch_get(&c)) {
return;
}
@@ -419,14 +491,14 @@ void cbm_result_compact(CBMFileResult *result) {
CBMArena fresh;
cbm_arena_init_exact(&fresh, c.bytes);
if (fresh.nblocks == 0) {
- cbm_free(CBM_MEM_CLASS_EXTRACT, c.slots);
+ cr_scratch_put(&c);
return;
}
c.phase = CR_COPY;
c.dst = &fresh;
cr_walk(&c, &tmp);
- cbm_free(CBM_MEM_CLASS_EXTRACT, c.slots);
+ cr_scratch_put(&c);
if (c.failed) {
cbm_arena_destroy(&fresh);
return;
diff --git a/internal/cbm/sqlite_writer.c b/internal/cbm/sqlite_writer.c
index 7391803f4..bbec4ed46 100644
--- a/internal/cbm/sqlite_writer.c
+++ b/internal/cbm/sqlite_writer.c
@@ -15,6 +15,8 @@
// - Varints: 1-9 bytes, big-endian, MSB continuation
#include "sqlite_writer.h"
+#include "foundation/arena.h" /* index-cell arena */
+#include "foundation/compat.h" /* CBM_TLS */
#include "foundation/constants.h"
#include "foundation/compat_fs.h"
#include "foundation/compat_thread.h"
@@ -301,29 +303,37 @@ typedef struct {
uint8_t *data;
int len;
int cap;
+ bool heap; /* data is malloc-owned; otherwise it is the caller's inline space */
} DynBuf;
-static void dynbuf_init(DynBuf *b) {
- b->data = NULL;
+/* Start on caller-provided space (a record builder's stack array). Growth past
+ * it moves to the heap once; a buffer that never outgrows it never allocates. */
+static void dynbuf_init_inline(DynBuf *b, uint8_t *space, int cap) {
+ b->data = space;
b->len = 0;
- b->cap = 0;
+ b->cap = cap;
+ b->heap = false;
}
static bool dynbuf_ensure(DynBuf *b, int needed) {
if (b->len + needed <= b->cap) {
return true;
}
- int newcap = b->cap == 0 ? INITIAL_PAGE_CAP : b->cap;
+ int newcap = b->cap < INITIAL_PAGE_CAP ? INITIAL_PAGE_CAP : b->cap;
while (newcap < b->len + needed) {
newcap *= GROWTH_FACTOR;
}
- uint8_t *p = (uint8_t *)realloc(b->data, newcap);
+ uint8_t *p = b->heap ? (uint8_t *)realloc(b->data, newcap) : (uint8_t *)malloc(newcap);
if (!p) {
(void)fprintf(stderr, "cbm_write_db: dynbuf realloc failed size=%d\n", newcap);
return false;
}
+ if (!b->heap && b->len > 0) {
+ memcpy(p, b->data, (size_t)b->len);
+ }
b->data = p;
b->cap = newcap;
+ b->heap = true;
return true;
}
@@ -343,22 +353,35 @@ static bool dynbuf_append(DynBuf *b, const void *data, int len) {
}
static void dynbuf_free(DynBuf *b) {
- free(b->data);
+ if (b->heap) {
+ free(b->data);
+ }
b->data = NULL;
b->len = b->cap = 0;
+ b->heap = false;
}
// --- Record builder ---
// Builds a SQLite record: header (header_len varint + serial types) + body (values)
+/* Inline space for one record. Every node, edge and index row used to allocate
+ * a 4 KB header buffer and a 4 KB body buffer and free both after copying a few
+ * dozen bytes out: the waste sanitizer measured 32 M allocations and ~126 GB of
+ * churn on the Go corpus, ~99 % of it never written (2026-09-17). A header is
+ * one serial type per column; a body is the row's text. Rows that do not fit
+ * (long property JSON) still grow onto the heap. */
+enum { REC_HEADER_INLINE = 64, REC_BODY_INLINE = 1536 };
+
typedef struct {
DynBuf header; // serial type varints
DynBuf body; // column values
+ uint8_t header_space[REC_HEADER_INLINE];
+ uint8_t body_space[REC_BODY_INLINE];
} RecordBuilder;
static void rec_init(RecordBuilder *r) {
- dynbuf_init(&r->header);
- dynbuf_init(&r->body);
+ dynbuf_init_inline(&r->header, r->header_space, REC_HEADER_INLINE);
+ dynbuf_init_inline(&r->body, r->body_space, REC_BODY_INLINE);
}
static void rec_free(RecordBuilder *r) {
@@ -408,8 +431,7 @@ static void rec_add_blob(RecordBuilder *r, const uint8_t *data, int len) {
// Finalize: returns the complete record bytes (header_len + header + body).
// Caller must free the returned buffer.
-static uint8_t *rec_finalize(RecordBuilder *r, int *out_len) {
- *out_len = 0;
+static int rec_total_header(const RecordBuilder *r) {
int header_content_len = r->header.len;
int header_len_varint_len = varint_len(header_content_len + varint_len(header_content_len));
// The header size varint includes itself, so we may need to iterate
@@ -420,20 +442,73 @@ static uint8_t *rec_finalize(RecordBuilder *r, int *out_len) {
header_len_varint_len = recalc;
total_header = header_len_varint_len + header_content_len;
}
+ return total_header;
+}
+
+// Bytes rec_write produces: the record exactly as rec_finalize returns it.
+static int rec_payload_len(const RecordBuilder *r) {
+ return rec_total_header(r) + r->body.len;
+}
+
+// Write the record (header_len + header + body) at dst; returns its length.
+static int rec_write(const RecordBuilder *r, uint8_t *dst) {
+ int pos = put_varint(dst, rec_total_header(r));
+ memcpy(dst + pos, r->header.data, r->header.len);
+ pos += r->header.len;
+ memcpy(dst + pos, r->body.data, r->body.len);
+ return pos + r->body.len;
+}
- int total = total_header + r->body.len;
+static uint8_t *rec_finalize(RecordBuilder *r, int *out_len) {
+ *out_len = 0;
+ int total = rec_payload_len(r);
uint8_t *buf = (uint8_t *)malloc(total);
if (!buf) {
return NULL;
}
- int pos = put_varint(buf, total_header);
- memcpy(buf + pos, r->header.data, header_content_len);
- pos += header_content_len;
- memcpy(buf + pos, r->body.data, r->body.len);
- *out_len = total;
+ *out_len = rec_write(r, buf);
return buf;
}
+/* One index build's cells live in an arena: they are all released together
+ * once the B-tree is written, where a malloc + free per row was 12.1 M
+ * allocations on the Go corpus (waste sanitizer, 2026-09-17). The writer
+ * builds one index at a time on one thread; NULL = the malloc path, which the
+ * unit tests and any other caller keep. */
+enum { CELLS_ARENA_BLOCK = 1024 * 1024 };
+static CBM_TLS CBMArena *tl_cells_arena;
+
+static uint8_t *cells_alloc(size_t bytes) {
+ if (tl_cells_arena) {
+ return (uint8_t *)cbm_arena_alloc(tl_cells_arena, bytes);
+ }
+ return (uint8_t *)malloc(bytes);
+}
+
+static void cells_free(uint8_t *cell) {
+ if (!tl_cells_arena) {
+ free(cell);
+ }
+}
+
+// An index leaf cell, varint(payload_len) + record, written in place: building
+// the record first and copying it into the cell was a second allocation and a
+// copy for every index entry (16 M record buffers of pure churn on the Go
+// corpus, waste sanitizer 2026-09-17). Same bytes.
+static uint8_t *rec_finalize_index_cell(const RecordBuilder *r, int *out_len) {
+ *out_len = 0;
+ int payload_len = rec_payload_len(r);
+ int total = varint_len(payload_len) + payload_len;
+ uint8_t *cell = cells_alloc((size_t)total);
+ if (!cell) {
+ return NULL;
+ }
+ int pos = put_varint(cell, payload_len);
+ pos += rec_write(r, cell + pos);
+ *out_len = pos;
+ return cell;
+}
+
// --- Page builder ---
// Accumulates cells (records) into B-tree leaf pages.
@@ -460,6 +535,11 @@ typedef struct {
PageRef *leaves;
int leaf_count;
int leaf_cap;
+
+ // One reusable buffer every table cell is assembled in before it is copied
+ // into the page (grows to the largest cell, freed by pb_free).
+ uint8_t *cell_buf;
+ int cell_cap;
} PageBuilder;
static void pb_init(PageBuilder *pb, FILE *fp, uint32_t start_page, bool is_index) {
@@ -475,6 +555,8 @@ static void pb_init(PageBuilder *pb, FILE *fp, uint32_t start_page, bool is_inde
pb->leaves = NULL;
pb->leaf_count = 0;
pb->leaf_cap = 0;
+ pb->cell_buf = NULL;
+ pb->cell_cap = 0;
}
static void pb_free(PageBuilder *pb) {
@@ -484,6 +566,21 @@ static void pb_free(PageBuilder *pb) {
}
free(pb->leaves);
}
+ free(pb->cell_buf);
+ pb->cell_buf = NULL;
+ pb->cell_cap = 0;
+}
+
+static uint8_t *pb_cell_scratch(PageBuilder *pb, int len) {
+ if (pb->cell_cap < len) {
+ uint8_t *grown = (uint8_t *)realloc(pb->cell_buf, (size_t)len);
+ if (!grown) {
+ return NULL;
+ }
+ pb->cell_buf = grown;
+ pb->cell_cap = len;
+ }
+ return pb->cell_buf;
}
// Flush current leaf page to file
@@ -736,74 +833,46 @@ static uint32_t pb_build_interior(PageBuilder *pb, bool is_index) {
// Build a nodes table record: (id, project, label, name, qualified_name, file_path, start_line,
// end_line, properties)
-static uint8_t *build_node_record(const CBMDumpNode *n, int *out_len) {
- RecordBuilder r;
- rec_init(&r);
-
- rec_add_int(&r, n->id);
- rec_add_text(&r, n->project);
- rec_add_text(&r, n->label);
- rec_add_text(&r, n->name);
- rec_add_text(&r, n->qualified_name);
- rec_add_text(&r, n->file_path ? n->file_path : "");
- rec_add_int(&r, n->start_line);
- rec_add_int(&r, n->end_line);
- rec_add_text(&r, n->properties ? n->properties : "{}");
-
- uint8_t *data = rec_finalize(&r, out_len);
- rec_free(&r);
- return data;
+static void fill_node_record(RecordBuilder *r, const CBMDumpNode *n) {
+ rec_add_int(r, n->id);
+ rec_add_text(r, n->project);
+ rec_add_text(r, n->label);
+ rec_add_text(r, n->name);
+ rec_add_text(r, n->qualified_name);
+ rec_add_text(r, n->file_path ? n->file_path : "");
+ rec_add_int(r, n->start_line);
+ rec_add_int(r, n->end_line);
+ rec_add_text(r, n->properties ? n->properties : "{}");
}
// Build an edges table record: (id, project, source_id, target_id, type, properties)
// url_path_gen and local_name_gen are VIRTUAL generated columns — NOT stored in the record.
-static uint8_t *build_edge_record(const CBMDumpEdge *e, int *out_len) {
- RecordBuilder r;
- rec_init(&r);
-
- rec_add_int(&r, e->id);
- rec_add_text(&r, e->project);
- rec_add_int(&r, e->source_id);
- rec_add_int(&r, e->target_id);
- rec_add_text(&r, e->type);
- rec_add_text(&r, e->properties ? e->properties : "{}");
-
- uint8_t *data = rec_finalize(&r, out_len);
- rec_free(&r);
- return data;
+static void fill_edge_record(RecordBuilder *r, const CBMDumpEdge *e) {
+ rec_add_int(r, e->id);
+ rec_add_text(r, e->project);
+ rec_add_int(r, e->source_id);
+ rec_add_int(r, e->target_id);
+ rec_add_text(r, e->type);
+ rec_add_text(r, e->properties ? e->properties : "{}");
}
// Build a node_vectors table record: (node_id, project, vector)
-// Includes node_id in the record body (same pattern as build_node_record).
-static uint8_t *build_vector_record(const CBMDumpVector *v, int *out_len) {
- RecordBuilder r;
- rec_init(&r);
-
- rec_add_int(&r, v->node_id);
- rec_add_text(&r, v->project);
- rec_add_blob(&r, v->vector, v->vector_len);
-
- uint8_t *data = rec_finalize(&r, out_len);
- rec_free(&r);
- return data;
+// Includes node_id in the record body (same pattern as the node record).
+static void fill_vector_record(RecordBuilder *r, const CBMDumpVector *v) {
+ rec_add_int(r, v->node_id);
+ rec_add_text(r, v->project);
+ rec_add_blob(r, v->vector, v->vector_len);
}
// Build a token_vectors table record: (id, project, token, vector, idf)
-static uint8_t *build_token_vec_record(const CBMDumpTokenVec *tv, int *out_len) {
- RecordBuilder r;
- rec_init(&r);
-
- rec_add_int(&r, tv->id);
- rec_add_text(&r, tv->project);
- rec_add_text(&r, tv->token);
- rec_add_blob(&r, tv->vector, tv->vector_len);
+static void fill_token_vec_record(RecordBuilder *r, const CBMDumpTokenVec *tv) {
+ rec_add_int(r, tv->id);
+ rec_add_text(r, tv->project);
+ rec_add_text(r, tv->token);
+ rec_add_blob(r, tv->vector, tv->vector_len);
/* Store IDF as integer × 1000 for fixed-point (avoid float in record) */
enum { IDF_FIXED_POINT_SCALE = 1000 };
- rec_add_int(&r, (int64_t)(tv->idf * IDF_FIXED_POINT_SCALE));
-
- uint8_t *data = rec_finalize(&r, out_len);
- rec_free(&r);
- return data;
+ rec_add_int(r, (int64_t)(tv->idf * IDF_FIXED_POINT_SCALE));
}
// Build a projects table record: (name, indexed_at, root_path)
@@ -924,27 +993,8 @@ static uint8_t *build_index_entry_2text_rowid(const char *col1, const char *col2
rec_add_text(&r, col1);
rec_add_text(&r, col2);
rec_add_int(&r, rowid);
- int payload_len = 0;
- uint8_t *payload = rec_finalize(&r, &payload_len);
+ uint8_t *cell = rec_finalize_index_cell(&r, out_len);
rec_free(&r);
- if (!payload) {
- *out_len = 0;
- return NULL;
- }
-
- // Index cell: varint(payload_len) + payload
- int vl = varint_len(payload_len);
- int total = vl + payload_len;
- uint8_t *cell = (uint8_t *)malloc(total);
- if (!cell) {
- free(payload);
- *out_len = 0;
- return NULL;
- }
- int pos = put_varint(cell, payload_len);
- memcpy(cell + pos, payload, payload_len);
- free(payload);
- *out_len = total;
return cell;
}
@@ -956,26 +1006,8 @@ static uint8_t *build_index_entry_int_text_rowid(int64_t val, const char *text,
rec_add_int(&r, val);
rec_add_text(&r, text);
rec_add_int(&r, rowid);
- int payload_len = 0;
- uint8_t *payload = rec_finalize(&r, &payload_len);
+ uint8_t *cell = rec_finalize_index_cell(&r, out_len);
rec_free(&r);
- if (!payload) {
- *out_len = 0;
- return NULL;
- }
-
- int vl = varint_len(payload_len);
- int total = vl + payload_len;
- uint8_t *cell = (uint8_t *)malloc(total);
- if (!cell) {
- free(payload);
- *out_len = 0;
- return NULL;
- }
- int pos = put_varint(cell, payload_len);
- memcpy(cell + pos, payload, payload_len);
- free(payload);
- *out_len = total;
return cell;
}
@@ -988,26 +1020,8 @@ static uint8_t *build_index_entry_text_int_text_rowid(const char *t1, int64_t va
rec_add_int(&r, val);
rec_add_text(&r, t2);
rec_add_int(&r, rowid);
- int payload_len = 0;
- uint8_t *payload = rec_finalize(&r, &payload_len);
+ uint8_t *cell = rec_finalize_index_cell(&r, out_len);
rec_free(&r);
- if (!payload) {
- *out_len = 0;
- return NULL;
- }
-
- int vl = varint_len(payload_len);
- int total = vl + payload_len;
- uint8_t *cell = (uint8_t *)malloc(total);
- if (!cell) {
- free(payload);
- *out_len = 0;
- return NULL;
- }
- int pos = put_varint(cell, payload_len);
- memcpy(cell + pos, payload, payload_len);
- free(payload);
- *out_len = total;
return cell;
}
@@ -1023,26 +1037,8 @@ static uint8_t *build_index_entry_unique_2int_2text_rowid(int64_t v1, int64_t v2
rec_add_text(&r, text);
rec_add_text(&r, text2);
rec_add_int(&r, rowid);
- int payload_len = 0;
- uint8_t *payload = rec_finalize(&r, &payload_len);
+ uint8_t *cell = rec_finalize_index_cell(&r, out_len);
rec_free(&r);
- if (!payload) {
- *out_len = 0;
- return NULL;
- }
-
- int vlen = varint_len(payload_len);
- int total = vlen + payload_len;
- uint8_t *cell = (uint8_t *)malloc(total);
- if (!cell) {
- free(payload);
- *out_len = 0;
- return NULL;
- }
- int pos = put_varint(cell, payload_len);
- memcpy(cell + pos, payload, payload_len);
- free(payload);
- *out_len = total;
return cell;
}
@@ -1115,18 +1111,34 @@ static uint8_t *overflowize_index_cell(FILE *fp, uint32_t *next_page, uint8_t *c
uint32_t first_ovfl =
write_overflow_pages(fp, next_page, cell + vlen + local, (int)plen - local);
int nlen = vlen + local + BTREE_PTR_SIZE;
- uint8_t *data = (uint8_t *)malloc((size_t)nlen);
+ uint8_t *data = cells_alloc((size_t)nlen);
if (!data) {
return cell; /* fall back to the (broken) inline form on OOM */
}
memcpy(data, cell, (size_t)(vlen + local));
put_u32(data + vlen + local, first_ovfl);
- free(cell);
+ cells_free(cell);
*cell_len = nlen;
return data;
}
#define TABLE_OVERFLOW_MIN_LOCAL 8199
+// Put a finished table cell on the current leaf, flushing a full leaf first.
+static bool pb_place_table_cell(PageBuilder *pb, const uint8_t *cell, int cell_len,
+ int64_t prev_rowid) {
+ if (!pb_cell_fits(pb, cell_len) && pb->cell_count > 0) {
+ if (!pb_ensure_leaf_cap(pb)) {
+ return false;
+ }
+ pb->leaves[pb->leaf_count].max_key = prev_rowid;
+ pb->leaves[pb->leaf_count].sep_cell = NULL;
+ pb->leaves[pb->leaf_count].sep_cell_len = 0;
+ pb_flush_leaf(pb);
+ }
+ pb_add_cell(pb, cell, cell_len);
+ return true;
+}
+
// Add a table cell to the PageBuilder, flushing leaf pages as needed.
// If the payload exceeds max_local, overflow pages are written and only the
// local portion plus a 4-byte overflow page pointer is stored in the leaf cell.
@@ -1160,20 +1172,37 @@ static void pb_add_table_cell_with_flush(PageBuilder *pb, int64_t rowid, const u
if (!cell) {
return;
}
+ (void)pb_place_table_cell(pb, cell, cell_len, prev_rowid);
+ free(cell);
+}
- if (!pb_cell_fits(pb, cell_len) && pb->cell_count > 0) {
- if (!pb_ensure_leaf_cap(pb)) {
- free(cell);
- return;
+// A table record written straight into the page builder's cell buffer: the
+// record builder's bytes go to the cell once, where rec_finalize +
+// build_table_cell allocated the record, allocated the cell, copied, and freed
+// both for every row. Same bytes; a record too large for the page takes the
+// overflow path unchanged. False only on allocation failure.
+static bool pb_add_table_record_with_flush(PageBuilder *pb, int64_t rowid, const RecordBuilder *r,
+ int64_t prev_rowid) {
+ int payload_len = rec_payload_len(r);
+ if (payload_len > TABLE_OVERFLOW_MAX_LOCAL) {
+ int rec_len = 0;
+ uint8_t *rec = rec_finalize((RecordBuilder *)r, &rec_len);
+ if (!rec) {
+ return false;
}
- pb->leaves[pb->leaf_count].max_key = prev_rowid;
- pb->leaves[pb->leaf_count].sep_cell = NULL;
- pb->leaves[pb->leaf_count].sep_cell_len = 0;
- pb_flush_leaf(pb);
+ pb_add_table_cell_with_flush(pb, rowid, rec, rec_len, prev_rowid);
+ free(rec);
+ return true;
}
-
- pb_add_cell(pb, cell, cell_len);
- free(cell);
+ int total = varint_len(payload_len) + varint_len(rowid) + payload_len;
+ uint8_t *cell = pb_cell_scratch(pb, total);
+ if (!cell) {
+ return false;
+ }
+ int pos = put_varint(cell, payload_len);
+ pos += put_varint(cell + pos, rowid);
+ pos += rec_write(r, cell + pos);
+ return pb_place_table_cell(pb, cell, pos, prev_rowid);
}
// Finalize a table PageBuilder: flush last leaf and build interior pages.
@@ -1607,21 +1636,8 @@ static uint8_t *ecell_url_path(const CBMDumpEdge *e, int *out_len) {
rec_add_null(&r);
}
rec_add_int(&r, e->id);
- int payload_len = 0;
- uint8_t *payload = rec_finalize(&r, &payload_len);
+ uint8_t *cell = rec_finalize_index_cell(&r, out_len);
rec_free(&r);
- int vlen = varint_len(payload_len);
- int total = vlen + payload_len;
- uint8_t *cell = (uint8_t *)malloc(total);
- if (!cell) {
- free(payload);
- *out_len = 0;
- return NULL;
- }
- int pos = put_varint(cell, payload_len);
- memcpy(cell + pos, payload, payload_len);
- free(payload);
- *out_len = total;
return cell;
}
@@ -1636,19 +1652,21 @@ static uint32_t build_edge_index_sorted(FILE *fp, uint32_t *next_page, CBMDumpEd
}
uint8_t **idx_cells = (uint8_t **)malloc(edge_count * sizeof(uint8_t *));
int *idx_lens = (int *)malloc(edge_count * sizeof(int));
+ CBMArena cells;
+ cbm_arena_init_lazy(&cells, CELLS_ARENA_BLOCK);
if (!idx_cells || !idx_lens) {
free(perm);
free(idx_cells);
free(idx_lens);
return 0;
}
+ tl_cells_arena = &cells;
for (int i = 0; i < edge_count; i++) {
int si = perm[i];
idx_cells[i] = cell_fn(&edges[si], &idx_lens[i]);
if (!idx_cells[i]) {
- for (int j = 0; j < i; j++) {
- free(idx_cells[j]);
- }
+ tl_cells_arena = NULL;
+ cbm_arena_destroy(&cells);
free(idx_cells);
free(idx_lens);
free(perm);
@@ -1657,9 +1675,8 @@ static uint32_t build_edge_index_sorted(FILE *fp, uint32_t *next_page, CBMDumpEd
}
free(perm);
uint32_t root = write_index_btree(fp, next_page, idx_cells, idx_lens, edge_count);
- for (int i = 0; i < edge_count; i++) {
- free(idx_cells[i]);
- }
+ tl_cells_arena = NULL;
+ cbm_arena_destroy(&cells);
free(idx_cells);
free(idx_lens);
return root;
@@ -1691,20 +1708,22 @@ static uint32_t build_node_index_sorted(FILE *fp, uint32_t *next_page, CBMDumpNo
}
uint8_t **idx_cells = (uint8_t **)malloc(node_count * sizeof(uint8_t *));
int *idx_lens = (int *)malloc(node_count * sizeof(int));
+ CBMArena cells;
+ cbm_arena_init_lazy(&cells, CELLS_ARENA_BLOCK);
if (!idx_cells || !idx_lens) {
free(perm);
free(idx_cells);
free(idx_lens);
return 0;
}
+ tl_cells_arena = &cells;
for (int i = 0; i < node_count; i++) {
int si = perm[i];
idx_cells[i] = build_index_entry_2text_rowid(nodes[si].project, col_fn(&nodes[si]),
nodes[si].id, &idx_lens[i]);
if (!idx_cells[i]) {
- for (int j = 0; j < i; j++) {
- free(idx_cells[j]);
- }
+ tl_cells_arena = NULL;
+ cbm_arena_destroy(&cells);
free(idx_cells);
free(idx_lens);
free(perm);
@@ -1713,9 +1732,8 @@ static uint32_t build_node_index_sorted(FILE *fp, uint32_t *next_page, CBMDumpNo
}
free(perm);
uint32_t root = write_index_btree(fp, next_page, idx_cells, idx_lens, node_count);
- for (int i = 0; i < node_count; i++) {
- free(idx_cells[i]);
- }
+ tl_cells_arena = NULL;
+ cbm_arena_destroy(&cells);
free(idx_cells);
free(idx_lens);
return root;
@@ -1796,13 +1814,13 @@ static int publish_writer_output(write_db_ctx_t *w) {
return 0;
}
-/* Callback type for building a record from an item at index i. */
-typedef uint8_t *(*build_record_fn)(const void *items, int i, int *out_len);
+/* Callback type for filling a record from an item at index i. */
+typedef void (*fill_record_fn)(RecordBuilder *r, const void *items, int i);
typedef int64_t (*get_rowid_fn)(const void *items, int i);
/* Write a streaming B-tree table from count items, or an empty table if count == 0. */
static int write_one_table(write_db_ctx_t *w, uint32_t *root, const void *items, int count,
- build_record_fn build_rec, get_rowid_fn get_id) {
+ fill_record_fn fill_rec, get_rowid_fn get_id) {
if (count <= 0 || !items) {
*root = write_table_btree(w->fp, &w->next_page, NULL, NULL, NULL, 0, false);
return 0;
@@ -1810,15 +1828,17 @@ static int write_one_table(write_db_ctx_t *w, uint32_t *root, const void *items,
PageBuilder pb;
pb_init(&pb, w->fp, w->next_page, false);
for (int i = 0; i < count; i++) {
- int rec_len;
- uint8_t *rec = build_rec(items, i, &rec_len);
- if (!rec) {
- return ERR_WRITE_FAILED;
- }
+ RecordBuilder r;
+ rec_init(&r);
+ fill_rec(&r, items, i);
int64_t rowid = get_id(items, i);
int64_t prev_id = i > 0 ? get_id(items, i - SKIP_ONE) : 0;
- pb_add_table_cell_with_flush(&pb, rowid, rec, rec_len, prev_id);
- free(rec);
+ bool placed = pb_add_table_record_with_flush(&pb, rowid, &r, prev_id);
+ rec_free(&r);
+ if (!placed) {
+ pb_free(&pb);
+ return ERR_WRITE_FAILED;
+ }
}
*root = pb_finalize_table(&pb, &w->next_page, get_id(items, count - SKIP_ONE));
return 0;
@@ -1826,20 +1846,20 @@ static int write_one_table(write_db_ctx_t *w, uint32_t *root, const void *items,
/* Adapter functions for write_one_table (nodes are written via the streaming
* PageBuilder in cbm_writer_append_nodes, so no node adapter is needed here). */
-static uint8_t *adapt_build_edge(const void *items, int i, int *out_len) {
- return build_edge_record(&((const CBMDumpEdge *)items)[i], out_len);
+static void adapt_fill_edge(RecordBuilder *r, const void *items, int i) {
+ fill_edge_record(r, &((const CBMDumpEdge *)items)[i]);
}
static int64_t adapt_edge_id(const void *items, int i) {
return ((const CBMDumpEdge *)items)[i].id;
}
-static uint8_t *adapt_build_vector(const void *items, int i, int *out_len) {
- return build_vector_record(&((const CBMDumpVector *)items)[i], out_len);
+static void adapt_fill_vector(RecordBuilder *r, const void *items, int i) {
+ fill_vector_record(r, &((const CBMDumpVector *)items)[i]);
}
static int64_t adapt_vector_id(const void *items, int i) {
return ((const CBMDumpVector *)items)[i].node_id;
}
-static uint8_t *adapt_build_token_vec(const void *items, int i, int *out_len) {
- return build_token_vec_record(&((const CBMDumpTokenVec *)items)[i], out_len);
+static void adapt_fill_token_vec(RecordBuilder *r, const void *items, int i) {
+ fill_token_vec_record(r, &((const CBMDumpTokenVec *)items)[i]);
}
static int64_t adapt_token_vec_id(const void *items, int i) {
return ((const CBMDumpTokenVec *)items)[i].id;
@@ -2066,17 +2086,17 @@ static int write_db_after_nodes(write_db_ctx_t *w, uint32_t nodes_root) {
uint32_t vectors_root;
uint32_t token_vecs_root;
int rc =
- write_one_table(w, &edges_root, w->edges, w->edge_count, adapt_build_edge, adapt_edge_id);
+ write_one_table(w, &edges_root, w->edges, w->edge_count, adapt_fill_edge, adapt_edge_id);
if (rc != 0) {
return discard_writer_output(w, rc);
}
- rc = write_one_table(w, &vectors_root, w->vectors, w->vector_count, adapt_build_vector,
+ rc = write_one_table(w, &vectors_root, w->vectors, w->vector_count, adapt_fill_vector,
adapt_vector_id);
if (rc != 0) {
return discard_writer_output(w, rc);
}
rc = write_one_table(w, &token_vecs_root, w->token_vecs, w->token_vec_count,
- adapt_build_token_vec, adapt_token_vec_id);
+ adapt_fill_token_vec, adapt_token_vec_id);
if (rc != 0) {
return discard_writer_output(w, rc);
}
@@ -2307,16 +2327,18 @@ int cbm_writer_append_nodes(cbm_db_writer_t *w, const CBMDumpNode *nodes, int co
return w->err;
}
for (int i = 0; i < count; i++) {
- int rec_len;
- uint8_t *rec = build_node_record(&nodes[i], &rec_len);
- if (!rec) {
+ RecordBuilder r;
+ rec_init(&r);
+ fill_node_record(&r, &nodes[i]);
+ /* prev_rowid is the previous node's id (0 for the very first), matching
+ * the one-shot write_one_table loop — so output is byte-identical. */
+ bool placed =
+ pb_add_table_record_with_flush(&w->nodes_pb, nodes[i].id, &r, w->last_node_rowid);
+ rec_free(&r);
+ if (!placed) {
w->err = ERR_WRITE_FAILED;
return w->err;
}
- /* prev_rowid is the previous node's id (0 for the very first), matching
- * the one-shot write_one_table loop — so output is byte-identical. */
- pb_add_table_cell_with_flush(&w->nodes_pb, nodes[i].id, rec, rec_len, w->last_node_rowid);
- free(rec);
w->last_node_rowid = nodes[i].id;
w->node_rows_written++;
}
diff --git a/src/foundation/arena.c b/src/foundation/arena.c
index 82a2f2af6..ec39d8d6c 100644
--- a/src/foundation/arena.c
+++ b/src/foundation/arena.c
@@ -12,6 +12,7 @@
#include "arena.h"
#include "foundation/constants.h"
#include "foundation/mem_core.h"
+#include "foundation/mem_events.h"
enum { ARENA_ALIGN = 7, ARENA_GROW_OK = 1 };
#include <stdlib.h>
@@ -23,12 +24,47 @@ enum { ARENA_ALIGN = 7, ARENA_GROW_OK = 1 };
#endif
#include <stdio.h>
+#if defined(CBM_MEMWASTE) && CBM_MEMWASTE
+/* Capacity against use for the life of an arena, reported when it resets or
+ * dies: bytes left at the end of every block it moved past, the unused rest of
+ * the block it stopped in, and blocks it owned but never reached. */
+static void arena_waste_report(const CBMArena *a) {
+ if (!a || a->nblocks == 0 || !cbm_memev_enabled()) {
+ return;
+ }
+ uint64_t capacity = 0;
+ uint64_t unreached = 0;
+ for (int i = 0; i < a->nblocks; i++) {
+ capacity += a->block_sizes[i];
+ if (i > a->cur) {
+ unreached += a->block_sizes[i];
+ }
+ }
+ uint64_t current_rest = a->block_size > a->used ? a->block_size - a->used : 0;
+ uint64_t wasted = a->waste_tail + current_rest + unreached;
+ uint64_t used = capacity > wasted ? capacity - wasted : 0;
+ cbm_memev_container(CBM_WORK_CT_ARENA, a->waste_site, capacity, used,
+ (uint64_t)a->waste_grows);
+}
+#define ARENA_SITE() __builtin_return_address(0)
+#else
+#define arena_waste_report(a) ((void)0)
+#define ARENA_SITE() NULL
+#endif
+
+static void arena_init_at(CBMArena *a, size_t block_size, void *site);
+
void cbm_arena_init(CBMArena *a) {
- cbm_arena_init_sized(a, CBM_ARENA_DEFAULT_BLOCK_SIZE);
+ arena_init_at(a, CBM_ARENA_DEFAULT_BLOCK_SIZE, ARENA_SITE());
}
void cbm_arena_init_sized(CBMArena *a, size_t block_size) {
+ arena_init_at(a, block_size, ARENA_SITE());
+}
+
+static void arena_init_at(CBMArena *a, size_t block_size, void *site) {
memset(a, 0, sizeof(*a));
+ a->waste_site = site;
if (block_size < CBM_SZ_64) {
block_size = CBM_SZ_64; /* minimum sanity */
}
@@ -41,10 +77,19 @@ void cbm_arena_init_sized(CBMArena *a, size_t block_size) {
}
}
+void cbm_arena_init_lazy(CBMArena *a, size_t block_size) {
+ memset(a, 0, sizeof(*a));
+ a->waste_site = ARENA_SITE();
+ if (block_size < CBM_SZ_64) {
+ block_size = CBM_SZ_64; /* minimum sanity */
+ }
+ a->grow_size = block_size; /* the opening block; arena_grow doubles from there */
+}
+
void cbm_arena_init_exact(CBMArena *a, size_t bytes) {
size_t block = (bytes + ARENA_ALIGN) & ~(size_t)ARENA_ALIGN;
- cbm_arena_init_sized(a, block);
- a->grow_size = CBM_ARENA_DEFAULT_BLOCK_SIZE;
+ arena_init_at(a, block, ARENA_SITE());
+ a->grow_size = CBM_ARENA_APPEND_BLOCK;
/* An exact block is a compacted result's image and may be written to
* disk whole (result_spill): the alignment gaps between objects and the
* padding inside structs are never written by the copy, and MemorySanitizer
@@ -59,6 +104,7 @@ void cbm_arena_init_exact(CBMArena *a, size_t bytes) {
static int arena_grow(CBMArena *a, size_t min_size) {
/* A rewound arena still owns blocks past the cursor: use the next one if
* it fits, otherwise drop it and everything after it and grow fresh. */
+ a->waste_tail += a->block_size > a->used ? a->block_size - a->used : 0;
if (a->cur + SKIP_ONE < a->nblocks && a->blocks[a->cur + SKIP_ONE]) {
if (a->block_sizes[a->cur + SKIP_ONE] >= min_size) {
a->cur++;
@@ -87,6 +133,7 @@ static int arena_grow(CBMArena *a, size_t min_size) {
}
a->blocks[a->nblocks] = block;
a->block_sizes[a->nblocks] = new_size;
+ a->waste_grows++;
a->cur = a->nblocks;
a->nblocks++;
a->block_size = new_size;
@@ -101,7 +148,12 @@ void *cbm_arena_alloc(CBMArena *a, size_t n) {
/* 8-byte alignment */
n = (n + ARENA_ALIGN) & ~(size_t)ARENA_ALIGN;
if (a->nblocks == 0) {
- return NULL;
+ /* A lazy arena opens here. A zeroed or destroyed one has no grow size
+ * and stays closed. */
+ if (a->grow_size == 0 || !arena_grow(a, n)) {
+ return NULL;
+ }
+ a->waste_grows = 0; /* the opening block is the arena's first, not a growth */
}
if (a->used + n > a->block_size) {
if (!arena_grow(a, n)) {
@@ -217,6 +269,9 @@ void cbm_arena_rewind(CBMArena *a) {
if (!a || a->nblocks == 0) {
return;
}
+ arena_waste_report(a);
+ a->waste_tail = 0;
+ a->waste_grows = 0;
a->cur = 0;
a->block_size = a->block_sizes[0];
a->used = 0;
@@ -232,6 +287,9 @@ size_t cbm_arena_capacity(const CBMArena *a) {
}
void cbm_arena_reset(CBMArena *a) {
+ arena_waste_report(a);
+ a->waste_tail = 0;
+ a->waste_grows = 0;
/* Keep first block, free the rest */
for (int i = SKIP_ONE; i < a->nblocks; i++) {
cbm_free(CBM_MEM_CLASS_ARENA, a->blocks[i]);
@@ -253,6 +311,7 @@ void cbm_arena_reset(CBMArena *a) {
}
void cbm_arena_destroy(CBMArena *a) {
+ arena_waste_report(a);
for (int i = 0; i < a->nblocks; i++) {
cbm_free(CBM_MEM_CLASS_ARENA, a->blocks[i]);
}
diff --git a/src/foundation/arena.h b/src/foundation/arena.h
index 8bac689ff..7f6b5dac6 100644
--- a/src/foundation/arena.h
+++ b/src/foundation/arena.h
@@ -26,6 +26,12 @@ typedef struct {
size_t total_alloc; /* cumulative bytes allocated (for stats) */
size_t grow_size; /* size of the NEXT block added; doubles per growth */
int cur; /* index of the block allocations come from (rewind sets 0) */
+ /* Waste sanitizer (mem_events.h): where the arena was created and how many
+ * bytes were left behind at the end of blocks it moved past. Present in
+ * every build so the struct is the same in every translation unit. */
+ void *waste_site;
+ size_t waste_tail;
+ size_t waste_grows; /* fresh blocks allocated since the last report */
} CBMArena;
/* Initialize arena with default block size. */
@@ -34,8 +40,24 @@ void cbm_arena_init(CBMArena *a);
/* Initialize arena with a custom initial block size. */
void cbm_arena_init_sized(CBMArena *a, size_t block_size);
+/* Initialize an arena that holds no memory until its first allocation, which
+ * opens a first block of `block_size` (later blocks double as usual). For an
+ * arena many callers create and most never use: the per-file extraction
+ * scratch opened a 512 KB block for every file, and 3.3 GB of those blocks were
+ * never read or written on the Go corpus (waste sanitizer access lane,
+ * 2026-09-17). A zeroed or destroyed arena stays closed: allocating from it
+ * still returns NULL. */
+void cbm_arena_init_lazy(CBMArena *a, size_t block_size);
+
+/* The block an exact arena takes when something appends to it after the fact:
+ * the cross-file LSP pass adds a few resolved calls to a compacted result, so
+ * a 64 KB default block was ~60 KB of untouched memory per appended-to result
+ * and 0.5 GB of the worker's peak on the Go corpus (waste sanitizer,
+ * 2026-09-17). It doubles from here like any other arena. */
+#define CBM_ARENA_APPEND_BLOCK ((size_t)8 * 1024)
+
/* Initialize an arena as ONE block of exactly `bytes` (rounded to alignment),
- * with later growth restarting at the default block size rather than doubling
+ * with later growth restarting at CBM_ARENA_APPEND_BLOCK rather than doubling
* the exact block. This is the compaction target: a result whose reachable
* data measures N bytes lands in one N-byte block with no tail, and a later
* append (the cross-file LSP pass adds resolved calls) costs one small block,
diff --git a/src/foundation/compat_thread.c b/src/foundation/compat_thread.c
index e018cdc41..e10cec8f1 100644
--- a/src/foundation/compat_thread.c
+++ b/src/foundation/compat_thread.c
@@ -4,6 +4,7 @@
* POSIX: thin wrappers around pthreads and posix_memalign.
* Windows: CreateThread, CRITICAL_SECTION, _aligned_malloc.
*/
+#include "foundation/mem_events.h"
#include "foundation/constants.h"
#include "foundation/compat_thread.h"
@@ -93,6 +94,9 @@ static bool thread_release_heap_enabled(void) {
static void NTAPI cbm_thread_detach_callback(PVOID handle, DWORD reason, PVOID reserved) {
(void)handle;
(void)reserved;
+ if (reason == DLL_THREAD_DETACH) {
+ cbm_memev_thread_end(); /* waste-sanitizer thread state; no-op outside that build */
+ }
if (reason == DLL_THREAD_DETACH && thread_release_heap_enabled()) {
mi_thread_done();
}
@@ -204,6 +208,16 @@ void cbm_mutex_init(cbm_mutex_t *m) {
}
void cbm_mutex_lock(cbm_mutex_t *m) {
+#if defined(CBM_MEMWASTE) && CBM_MEMWASTE
+ if (cbm_memev_enabled()) {
+ bool contended = !TryEnterCriticalSection(&m->cs);
+ if (contended) {
+ EnterCriticalSection(&m->cs);
+ }
+ cbm_work_note(CBM_WORK_MUTEX, __builtin_return_address(0), 0, contended ? 1 : 0, 0);
+ return;
+ }
+#endif
EnterCriticalSection(&m->cs);
}
@@ -222,7 +236,13 @@ void cbm_mutex_init(cbm_mutex_t *m) {
}
void cbm_mutex_lock(cbm_mutex_t *m) {
+#if defined(CBM_MEMWASTE) && CBM_MEMWASTE
+ /* pthread_mutex_lock itself counts in this flavour; going through the same
+ * lock with our caller as the site keeps the attribution and counts once. */
+ (void)cbm_memev_mutex_lock(&m->mtx, __builtin_return_address(0));
+#else
pthread_mutex_lock(&m->mtx);
+#endif
}
void cbm_mutex_unlock(cbm_mutex_t *m) {
diff --git a/src/foundation/dyn_array.h b/src/foundation/dyn_array.h
index 0f11ed01a..37c55f597 100644
--- a/src/foundation/dyn_array.h
+++ b/src/foundation/dyn_array.h
@@ -21,6 +21,14 @@
#include <stdlib.h>
#include <string.h>
#include "mem_core.h" /* same directory: units without -Isrc include this header relatively */
+#if defined(CBM_MEMWASTE) && CBM_MEMWASTE
+#include "mem_events.h"
+#define CBM_DA_WASTE_NOTE(da) \
+ cbm_memev_da_note((size_t)(da)->cap * sizeof(*(da)->items), \
+ (size_t)(da)->count * sizeof(*(da)->items), (size_t)(da)->cap)
+#else
+#define CBM_DA_WASTE_NOTE(da) ((void)0)
+#endif
/* Declare a dynamic array type for a given element type. */
#define CBM_DYN_ARRAY(T) \
@@ -84,6 +92,7 @@
/* Free all memory. */
#define cbm_da_free(da) \
do { \
+ CBM_DA_WASTE_NOTE(da); \
cbm_free(CBM_MEM_CLASS_DYN_ARRAY, (da)->items); \
(da)->items = NULL; \
(da)->count = 0; \
diff --git a/src/foundation/hash_table.c b/src/foundation/hash_table.c
index 58ff6ac54..8c18e4f55 100644
--- a/src/foundation/hash_table.c
+++ b/src/foundation/hash_table.c
@@ -20,6 +20,7 @@
*/
#include "foundation/constants.h"
#include "hash_table.h"
+#include "mem_events.h"
#include <stdlib.h>
#include <string.h>
@@ -54,16 +55,46 @@ static void ht_free_in(void *ptr, size_t size, cbm_mem_class_t *cls) {
* our own atomic-free counter). */
struct CBMHashTable {
cbm_vt vt;
+ /* Waste sanitizer: where the table was created, how often it grew, and a
+ * write counter (a lookup only repeats when nothing was written between). */
+ void *waste_site;
+ uint32_t waste_growths;
+ uint64_t waste_generation;
};
+#if defined(CBM_MEMWASTE) && CBM_MEMWASTE
+#define HT_SITE() __builtin_return_address(0)
+enum { HT_SLOT_BYTES = sizeof(cbm_vt_bucket) + sizeof(uint16_t) };
+#define HT_WROTE(ht) ((ht)->waste_generation++)
+#define HT_LOOKED_UP(ht, key, found) \
+ do { \
+ if (cbm_memev_enabled()) { \
+ /* site = the table's creation site: lookups are charged to the table */ \
+ cbm_work_note_ht(CBM_WORK_HT_GET, (ht)->waste_site, (ht), (key), \
+ (ht)->waste_generation, (found)); \
+ } \
+ } while (0)
+#else
+#define HT_SITE() NULL
+#define HT_WROTE(ht) ((void)0)
+#define HT_LOOKED_UP(ht, key, found) ((void)0)
+#endif
+
+static CBMHashTable *ht_create_at(cbm_mem_class_t cls, uint32_t initial_capacity, void *site);
+
CBMHashTable *cbm_ht_create(uint32_t initial_capacity) {
- return cbm_ht_create_in(CBM_MEM_CLASS_HASH_TABLE, initial_capacity);
+ return ht_create_at(CBM_MEM_CLASS_HASH_TABLE, initial_capacity, HT_SITE());
}
CBMHashTable *cbm_ht_create_in(cbm_mem_class_t cls, uint32_t initial_capacity) {
+ return ht_create_at(cls, initial_capacity, HT_SITE());
+}
+
+static CBMHashTable *ht_create_at(cbm_mem_class_t cls, uint32_t initial_capacity, void *site) {
CBMHashTable *ht = (CBMHashTable *)cbm_calloc(cls, sizeof(*ht));
if (!ht)
return NULL;
+ ht->waste_site = site;
cbm_vt_init(&ht->vt, cls);
if (initial_capacity > 0) {
/* Reserve enough buckets for the requested entries. Verstable
@@ -81,6 +112,13 @@ void cbm_ht_free(CBMHashTable *ht) {
if (!ht)
return;
cbm_mem_class_t cls = ht->vt.ctx;
+#if defined(CBM_MEMWASTE) && CBM_MEMWASTE
+ if (cbm_memev_enabled()) {
+ cbm_memev_container(CBM_WORK_CT_HASH_TABLE, ht->waste_site,
+ (uint64_t)cbm_vt_bucket_count(&ht->vt) * HT_SLOT_BYTES,
+ (uint64_t)cbm_vt_size(&ht->vt) * HT_SLOT_BYTES, ht->waste_growths);
+ }
+#endif
cbm_vt_cleanup(&ht->vt);
cbm_free(cls, ht);
}
@@ -88,16 +126,33 @@ void cbm_ht_free(CBMHashTable *ht) {
void *cbm_ht_set(CBMHashTable *ht, const char *key, void *value) {
if (!ht || !key)
return NULL;
- /* Capture previous value (if any) before overwriting.
- * Verstable's _insert overwrites silently and returns an iterator
- * to the (now updated) entry — we have to peek first to surface
- * the prior value to the caller (back-compat with our API). */
+ /* ONE probe: get_or_insert either inserts (key, value) or hands back the
+ * existing entry, whose previous value is returned and whose key AND value
+ * are replaced -- exactly what _insert did. The old get-then-insert hashed
+ * every key twice: 83 M repeated strlen calls inside vt_hash_string on the
+ * Go corpus (waste sanitizer, 2026-09-17). Verstable reports an insertion
+ * as a size change. */
void *prev = NULL;
- cbm_vt_itr itr = cbm_vt_get(&ht->vt, key);
- if (!cbm_vt_is_end(itr)) {
+ HT_WROTE(ht);
+#if defined(CBM_MEMWASTE) && CBM_MEMWASTE
+ size_t buckets_before = cbm_vt_bucket_count(&ht->vt);
+#endif
+ size_t size_before = cbm_vt_size(&ht->vt);
+ cbm_vt_itr itr = cbm_vt_get_or_insert(&ht->vt, key, value);
+ if (!cbm_vt_is_end(itr) && cbm_vt_size(&ht->vt) == size_before) {
prev = itr.data->val;
+ itr.data->key = key;
+ itr.data->val = value;
+ }
+#if defined(CBM_MEMWASTE) && CBM_MEMWASTE
+ bool grew = cbm_vt_bucket_count(&ht->vt) != buckets_before;
+ if (grew) {
+ ht->waste_growths++;
+ }
+ if (cbm_memev_enabled()) {
+ cbm_work_note(CBM_WORK_HT_SET, ht->waste_site, 0, grew ? 1 : 0, 0);
}
- (void)cbm_vt_insert(&ht->vt, key, value);
+#endif
return prev;
}
@@ -105,6 +160,7 @@ void *cbm_ht_get(const CBMHashTable *ht, const char *key) {
if (!ht || !key)
return NULL;
cbm_vt_itr itr = cbm_vt_get(&ht->vt, key);
+ HT_LOOKED_UP(ht, key, !cbm_vt_is_end(itr));
if (cbm_vt_is_end(itr))
return NULL;
return itr.data->val;
@@ -114,6 +170,7 @@ bool cbm_ht_has(const CBMHashTable *ht, const char *key) {
if (!ht || !key)
return false;
cbm_vt_itr itr = cbm_vt_get(&ht->vt, key);
+ HT_LOOKED_UP(ht, key, !cbm_vt_is_end(itr));
return !cbm_vt_is_end(itr);
}
@@ -121,6 +178,7 @@ const char *cbm_ht_get_key(const CBMHashTable *ht, const char *key) {
if (!ht || !key)
return NULL;
cbm_vt_itr itr = cbm_vt_get(&ht->vt, key);
+ HT_LOOKED_UP(ht, key, !cbm_vt_is_end(itr));
if (cbm_vt_is_end(itr))
return NULL;
return itr.data->key;
@@ -133,6 +191,7 @@ void *cbm_ht_delete(CBMHashTable *ht, const char *key) {
if (cbm_vt_is_end(itr))
return NULL;
void *prev = itr.data->val;
+ HT_WROTE(ht);
(void)cbm_vt_erase(&ht->vt, key);
return prev;
}
@@ -154,5 +213,6 @@ void cbm_ht_foreach(const CBMHashTable *ht, cbm_ht_iter_fn fn, void *userdata) {
void cbm_ht_clear(CBMHashTable *ht) {
if (!ht)
return;
+ HT_WROTE(ht);
cbm_vt_clear(&ht->vt);
}
diff --git a/src/pipeline/lsp_surface.c b/src/pipeline/lsp_surface.c
index 41d4e4d13..ba0ade761 100644
--- a/src/pipeline/lsp_surface.c
+++ b/src/pipeline/lsp_surface.c
@@ -19,12 +19,14 @@
#include "pipeline/lsp_surface.h"
#include "pipeline/pipeline_internal.h"
+#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
#include "cbm.h" /* cbm_label_is_relation — reg-only surface membership */
#include "foundation/log.h"
#include "foundation/sha256.h"
+#include "pipeline/worker_pool.h"
#include "yyjson/yyjson.h"
enum { SURFACE_CODEC_VERSION = 1 };
@@ -70,9 +72,10 @@ static void add_str_array_or_null(yyjson_mut_doc *doc, yyjson_mut_val *obj, cons
}
/* Serialize one file's surface: its slice of all_defs plus the registry-only
- * symbols from its raw extraction defs. Returns a malloc'd JSON string. */
+ * symbols from its raw extraction defs. Returns a malloc'd JSON string and its
+ * length. */
static char *surface_file_to_json(const CBMFileResult *result, const CBMLSPDef *defs,
- int def_count) {
+ int def_count, size_t *out_len) {
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
if (!doc) {
return NULL;
@@ -122,11 +125,64 @@ static char *surface_file_to_json(const CBMFileResult *result, const CBMLSPDef *
}
yyjson_mut_obj_add_val(doc, root, "reg", reg);
- char *json = yyjson_mut_write(doc, 0, NULL);
+ char *json = yyjson_mut_write(doc, 0, out_len);
yyjson_mut_doc_free(doc);
return json;
}
+/* One file's row, written in place: files are independent (their own result,
+ * their own def slice), so the pass runs them in parallel. Sequentially it was
+ * 1.15 s of the Go corpus's cross-LSP prepare (profile, 2026-09-17). */
+typedef struct {
+ const cbm_pipeline_ctx_t *ctx;
+ const char *project;
+ CBMFileResult **cache;
+ const cbm_file_info_t *files;
+ const CBMLSPDef *all_defs;
+ const int *def_starts;
+ cbm_lsp_surface_row_t *rows;
+ _Atomic bool failed;
+} surface_job_t;
+
+static void surface_row_one(int i, void *arg) {
+ surface_job_t *job = (surface_job_t *)arg;
+ if (atomic_load_explicit(&job->failed, memory_order_relaxed)) {
+ return;
+ }
+ bool loaded = false;
+ CBMFileResult *fr = cbm_pipeline_result_acquire(job->ctx, job->cache, i, NULL, &loaded);
+ if (!fr) {
+ /* Never parsed this run (read/extract skip): no surface claim.
+ * The routing layer treats a missing row as "must full-rebuild
+ * before this file can be reasoned about", which is the correct
+ * fail-closed default for an unreadable file. */
+ return;
+ }
+ int start = job->def_starts ? job->def_starts[i] : 0;
+ int end = job->def_starts ? job->def_starts[i + 1] : 0;
+ size_t json_len = 0;
+ char *json = surface_file_to_json(fr, job->all_defs ? job->all_defs + start : NULL,
+ end - start, &json_len);
+ cbm_pipeline_result_release(fr, loaded);
+ if (!json) {
+ atomic_store_explicit(&job->failed, true, memory_order_relaxed);
+ return;
+ }
+ char sha[CBM_SHA256_HEX_LEN + 1];
+ cbm_sha256_hex(json, json_len, sha);
+ cbm_lsp_surface_row_t *r = &job->rows[i];
+ r->defs_json = json; /* set first: it marks the row present for the compaction */
+ r->project = strdup(job->project);
+ r->rel_path = strdup(job->files[i].rel_path);
+ r->surface_sha = strdup(sha);
+ r->ref_bloom = NULL;
+ r->ref_bloom_len = 0;
+ r->config_ctx = strdup("");
+ if (!r->project || !r->rel_path || !r->surface_sha || !r->config_ctx) {
+ atomic_store_explicit(&job->failed, true, memory_order_relaxed);
+ }
+}
+
int cbm_lsp_surface_build_rows(const cbm_pipeline_ctx_t *ctx, const char *project,
CBMFileResult **cache, const cbm_file_info_t *files, int file_count,
const CBMLSPDef *all_defs, const int *def_starts,
@@ -140,39 +196,32 @@ int cbm_lsp_surface_build_rows(const cbm_pipeline_ctx_t *ctx, const char *projec
if (!rows) {
return -1;
}
+ surface_job_t job = {
+ .ctx = ctx,
+ .project = project,
+ .cache = cache,
+ .files = files,
+ .all_defs = all_defs,
+ .def_starts = def_starts,
+ .rows = rows,
+ };
+ atomic_init(&job.failed, false);
+ cbm_parallel_for(file_count, surface_row_one, &job,
+ (cbm_parallel_for_opts_t){.max_workers = 0, .force_pthreads = false});
+ if (atomic_load_explicit(&job.failed, memory_order_relaxed)) {
+ cbm_store_free_lsp_surfaces(rows, file_count); /* untouched rows are all NULL */
+ return -1;
+ }
+ /* Present rows to the front, in file order: the same rows, in the same
+ * order, as the sequential loop produced. */
int n = 0;
for (int i = 0; i < file_count; i++) {
- bool loaded = false;
- CBMFileResult *fr = cbm_pipeline_result_acquire(ctx, cache, i, NULL, &loaded);
- if (!fr) {
- /* Never parsed this run (read/extract skip): no surface claim.
- * The routing layer treats a missing row as "must full-rebuild
- * before this file can be reasoned about", which is the correct
- * fail-closed default for an unreadable file. */
+ if (!rows[i].defs_json) {
continue;
}
- int start = def_starts ? def_starts[i] : 0;
- int end = def_starts ? def_starts[i + 1] : 0;
- char *json = surface_file_to_json(fr, all_defs ? all_defs + start : NULL, end - start);
- cbm_pipeline_result_release(fr, loaded);
- if (!json) {
- cbm_store_free_lsp_surfaces(rows, n);
- return -1;
- }
- char sha[CBM_SHA256_HEX_LEN + 1];
- cbm_sha256_hex(json, strlen(json), sha);
- cbm_lsp_surface_row_t *r = &rows[n];
- r->project = strdup(project);
- r->rel_path = strdup(files[i].rel_path);
- r->surface_sha = strdup(sha);
- r->defs_json = json;
- r->ref_bloom = NULL;
- r->ref_bloom_len = 0;
- r->config_ctx = strdup("");
- if (!r->project || !r->rel_path || !r->surface_sha || !r->config_ctx) {
- n++;
- cbm_store_free_lsp_surfaces(rows, n);
- return -1;
+ if (n != i) {
+ rows[n] = rows[i];
+ memset(&rows[i], 0, sizeof(rows[i]));
}
n++;
}
diff --git a/src/pipeline/pass_configlink.c b/src/pipeline/pass_configlink.c
index 34c90bdf3..776a7d862 100644
--- a/src/pipeline/pass_configlink.c
+++ b/src/pipeline/pass_configlink.c
@@ -17,9 +17,12 @@
#include <stdint.h>
#include <stdio.h>
+#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
+#include "foundation/platform.h" /* cbm_default_worker_count */
+#include "pipeline/worker_pool.h"
#include "foundation/compat_regex.h"
/* ── Config link confidence scores ───────────────────────────────── */
@@ -223,6 +226,83 @@ static int collect_code_entries(cbm_gbuf_t *gb, code_entry_t *out, int max_out)
return n;
}
+/* The key x symbol comparison of strategy 1, per config key: every pair's
+ * verdict depends only on its two strings, so rows run in parallel and the
+ * edges are inserted afterwards in the original (config, code) order. One
+ * thread compared up to 4,096 x 8,192 pairs -- configlink's 0.93 s on the Go
+ * corpus (profile, 2026-09-17). A pair whose lengths rule both verdicts out
+ * skips the string calls: an exact match needs equal lengths, a substring one
+ * no longer than the code key. */
+typedef struct {
+ int co;
+ double confidence;
+} key_match_t;
+
+typedef struct {
+ key_match_t *items;
+ int count;
+ int cap;
+} key_match_list_t;
+
+typedef struct {
+ const config_entry_t *config;
+ const size_t *config_len;
+ int config_count;
+ const code_entry_t *code;
+ const size_t *code_len;
+ int code_count;
+ key_match_list_t *lists;
+ _Atomic int next;
+ _Atomic bool failed;
+} key_match_job_t;
+
+static double key_pair_confidence(const char *config_norm, size_t config_len,
+ const char *code_norm, size_t code_len) {
+ if (config_len > code_len) {
+ return 0.0;
+ }
+ if (config_len == code_len && memcmp(config_norm, code_norm, code_len) == 0) {
+ return CONF_KEY_EXACT; /* Exact match */
+ }
+ if (strstr(code_norm, config_norm) != NULL) {
+ return CONF_KEY_SUBSTRING; /* Substring match */
+ }
+ return 0.0;
+}
+
+static void key_match_worker(int worker_id, void *arg) {
+ (void)worker_id;
+ key_match_job_t *job = (key_match_job_t *)arg;
+ while (!atomic_load_explicit(&job->failed, memory_order_relaxed)) {
+ int ci = atomic_fetch_add_explicit(&job->next, SKIP_ONE, memory_order_relaxed);
+ if (ci >= job->config_count) {
+ break;
+ }
+ key_match_list_t *list = &job->lists[ci];
+ for (int co = 0; co < job->code_count; co++) {
+ double confidence =
+ key_pair_confidence(job->config[ci].normalized, job->config_len[ci],
+ job->code[co].normalized, job->code_len[co]);
+ if (confidence <= 0.0) {
+ continue;
+ }
+ if (list->count == list->cap) {
+ int cap = list->cap ? list->cap * PAIR_LEN : CBM_SZ_16;
+ key_match_t *grown = realloc(list->items, (size_t)cap * sizeof(*grown));
+ if (!grown) {
+ atomic_store_explicit(&job->failed, true, memory_order_relaxed);
+ return;
+ }
+ list->items = grown;
+ list->cap = cap;
+ }
+ list->items[list->count].co = co;
+ list->items[list->count].confidence = confidence;
+ list->count++;
+ }
+ }
+}
+
static int strategy_key_symbols(cbm_gbuf_t *gb) {
/* Get all Variable nodes */
const cbm_gbuf_node_t **vars = NULL;
@@ -243,6 +323,59 @@ static int strategy_key_symbols(cbm_gbuf_t *gb) {
int edge_count = 0;
+ size_t *config_len = malloc((size_t)config_count * sizeof(size_t));
+ size_t *code_len = malloc((size_t)(code_count > 0 ? code_count : 1) * sizeof(size_t));
+ key_match_list_t *lists = calloc((size_t)config_count, sizeof(key_match_list_t));
+ bool parallel_ok = config_len && code_len && lists;
+ if (parallel_ok) {
+ for (int ci = 0; ci < config_count; ci++) {
+ config_len[ci] = strlen(config_entries[ci].normalized);
+ }
+ for (int co = 0; co < code_count; co++) {
+ code_len[co] = strlen(code_entries[co].normalized);
+ }
+ key_match_job_t job = {
+ .config = config_entries,
+ .config_len = config_len,
+ .config_count = config_count,
+ .code = code_entries,
+ .code_len = code_len,
+ .code_count = code_count,
+ .lists = lists,
+ };
+ atomic_init(&job.next, 0);
+ atomic_init(&job.failed, false);
+ int workers = cbm_default_worker_count(false);
+ cbm_parallel_for(workers, key_match_worker, &job,
+ (cbm_parallel_for_opts_t){.max_workers = workers, .force_pthreads = false});
+ parallel_ok = !atomic_load_explicit(&job.failed, memory_order_relaxed);
+ }
+ if (parallel_ok) {
+ for (int ci = 0; ci < config_count; ci++) {
+ for (int m = 0; m < lists[ci].count; m++) {
+ const key_match_t *km = &lists[ci].items[m];
+ char props[CBM_SZ_512];
+ snprintf(props, sizeof(props),
+ "{\"strategy\":\"key_symbol\",\"confidence\":%.2f,\"config_key\":\"%s\"}",
+ km->confidence, config_entries[ci].name);
+
+ cbm_gbuf_insert_edge(gb, code_entries[km->co].node_id, config_entries[ci].node_id,
+ "CONFIGURES", props);
+ edge_count++;
+ }
+ }
+ }
+ for (int ci = 0; lists && ci < config_count; ci++) {
+ free(lists[ci].items);
+ }
+ free(lists);
+ free(config_len);
+ free(code_len);
+ if (parallel_ok) {
+ return edge_count;
+ }
+
+ /* Allocation failed: the one-thread comparison, as before. */
for (int ci = 0; ci < config_count; ci++) {
for (int co = 0; co < code_count; co++) {
double confidence = 0.0;
@@ -350,25 +483,90 @@ static void lowercase_into(char *buf, size_t bufsize, const char *src) {
buf[len < bufsize ? len : bufsize - SKIP_ONE] = '\0';
}
-/* Match a dep name (lowercased) against an import target node.
- * Returns confidence > 0 on match, 0 on no match. */
-static double match_dep_to_import(const cbm_gbuf_node_t *target, const char *dep_lower) {
- char target_lower[CBM_SZ_256];
- lowercase_into(target_lower, sizeof(target_lower), target->name);
+/* One IMPORTS edge prepared for matching: its endpoints resolved and its
+ * target's name and qualified name lowercased ONCE (same truncation as before:
+ * 255 and 511 bytes). Resolving both nodes and lowercasing both strings inside
+ * the dep x import loop was the whole cost of configlink -- 0.9 s on the Go
+ * corpus, and cbm_gbuf_find_by_id's x3.1 super-linear scaling (waste
+ * sanitizer, 2026-09-17). */
+typedef struct {
+ int64_t source_id;
+ const char *target_lower; /* into one shared block */
+ const char *qn_lower; /* NULL when the target has no qualified name */
+} dep_import_t;
- if (strcmp(target_lower, dep_lower) == 0) {
+/* Match a dep name (lowercased) against a prepared import target.
+ * Returns confidence > 0 on match, 0 on no match. */
+static double match_dep_to_import(const dep_import_t *imp, const char *dep_lower) {
+ if (strcmp(imp->target_lower, dep_lower) == 0) {
return CONF_DEP_EXACT;
}
- if (target->qualified_name) {
- char qn_lower[CBM_SZ_512];
- lowercase_into(qn_lower, sizeof(qn_lower), target->qualified_name);
- if (strstr(qn_lower, dep_lower) != NULL) {
- return CONF_DEP_QN_SUBSTR;
- }
+ if (imp->qn_lower && strstr(imp->qn_lower, dep_lower) != NULL) {
+ return CONF_DEP_QN_SUBSTR;
}
return 0.0;
}
+/* Resolve and lowercase every IMPORTS edge whose endpoints both exist, in edge
+ * order. Returns the count (0 with *out NULL on allocation failure). */
+static int prepare_dep_imports(cbm_gbuf_t *gb, const cbm_gbuf_edge_t *const *imports,
+ int import_count, dep_import_t **out, char **block_out) {
+ *out = NULL;
+ *block_out = NULL;
+ enum { NAME_CAP = CBM_SZ_256, QN_CAP = CBM_SZ_512 };
+ size_t bytes = 0;
+ for (int ii = 0; ii < import_count; ii++) {
+ const cbm_gbuf_node_t *target = cbm_gbuf_find_by_id(gb, imports[ii]->target_id);
+ const cbm_gbuf_node_t *source =
+ target ? cbm_gbuf_find_by_id(gb, imports[ii]->source_id) : NULL;
+ if (!target || !source) {
+ continue;
+ }
+ size_t nlen = target->name ? strlen(target->name) : 0;
+ bytes += (nlen < NAME_CAP ? nlen : NAME_CAP - SKIP_ONE) + SKIP_ONE;
+ if (target->qualified_name) {
+ size_t qlen = strlen(target->qualified_name);
+ bytes += (qlen < QN_CAP ? qlen : QN_CAP - SKIP_ONE) + SKIP_ONE;
+ }
+ }
+ dep_import_t *items = (dep_import_t *)malloc((size_t)(import_count > 0 ? import_count : 1) *
+ sizeof(dep_import_t));
+ char *block = (char *)malloc(bytes > 0 ? bytes : 1);
+ if (!items || !block) {
+ free(items);
+ free(block);
+ return 0;
+ }
+ int n = 0;
+ char *w = block;
+ for (int ii = 0; ii < import_count; ii++) {
+ const cbm_gbuf_node_t *target = cbm_gbuf_find_by_id(gb, imports[ii]->target_id);
+ const cbm_gbuf_node_t *source =
+ target ? cbm_gbuf_find_by_id(gb, imports[ii]->source_id) : NULL;
+ if (!target || !source) {
+ continue;
+ }
+ size_t nlen = target->name ? strlen(target->name) : 0;
+ size_t ncap = (nlen < NAME_CAP ? nlen : NAME_CAP - SKIP_ONE) + SKIP_ONE;
+ lowercase_into(w, ncap, target->name);
+ items[n].target_lower = w;
+ w += ncap;
+ items[n].qn_lower = NULL;
+ if (target->qualified_name) {
+ size_t qlen = strlen(target->qualified_name);
+ size_t qcap = (qlen < QN_CAP ? qlen : QN_CAP - SKIP_ONE) + SKIP_ONE;
+ lowercase_into(w, qcap, target->qualified_name);
+ items[n].qn_lower = w;
+ w += qcap;
+ }
+ items[n].source_id = source->id;
+ n++;
+ }
+ *out = items;
+ *block_out = block;
+ return n;
+}
+
static int strategy_dep_imports(cbm_gbuf_t *gb) {
const cbm_gbuf_node_t **vars = NULL;
int var_count = 0;
@@ -391,23 +589,17 @@ static int strategy_dep_imports(cbm_gbuf_t *gb) {
}
int edge_count = 0;
+ dep_import_t *prepared = NULL;
+ char *prepared_block = NULL;
+ int prepared_count =
+ prepare_dep_imports(gb, imports, import_count, &prepared, &prepared_block);
- for (int di = 0; di < dep_count; di++) {
+ for (int di = 0; di < dep_count && prepared; di++) {
char dep_lower[CBM_SZ_256];
lowercase_into(dep_lower, sizeof(dep_lower), deps[di].name);
- for (int ii = 0; ii < import_count; ii++) {
- const cbm_gbuf_node_t *target = cbm_gbuf_find_by_id(gb, imports[ii]->target_id);
- if (!target) {
- continue;
- }
-
- const cbm_gbuf_node_t *source = cbm_gbuf_find_by_id(gb, imports[ii]->source_id);
- if (!source) {
- continue;
- }
-
- double confidence = match_dep_to_import(target, dep_lower);
+ for (int ii = 0; ii < prepared_count; ii++) {
+ double confidence = match_dep_to_import(&prepared[ii], dep_lower);
if (confidence > 0.0) {
char props[CBM_SZ_512];
snprintf(
@@ -415,13 +607,16 @@ static int strategy_dep_imports(cbm_gbuf_t *gb) {
"{\"strategy\":\"dependency_import\",\"confidence\":%.2f,\"dep_name\":\"%s\"}",
confidence, deps[di].name);
- cbm_gbuf_insert_edge(gb, source->id, deps[di].node_id, "CONFIGURES", props);
+ cbm_gbuf_insert_edge(gb, prepared[ii].source_id, deps[di].node_id, "CONFIGURES",
+ props);
edge_count++;
}
}
}
- /* gbuf data is borrowed — no free */
+ /* gbuf data is borrowed; only the prepared copies are owned */
+ free(prepared);
+ free(prepared_block);
return edge_count;
}
diff --git a/src/pipeline/pass_k8s.c b/src/pipeline/pass_k8s.c
index 94dcd439a..806203f6c 100644
--- a/src/pipeline/pass_k8s.c
+++ b/src/pipeline/pass_k8s.c
@@ -22,7 +22,12 @@
#include "foundation/compat_fs.h"
#include "foundation/limits.h"
#include "cbm.h"
+#include "helpers.h" /* cbm_kind_in_set_free_cache */
+#include "foundation/slab_alloc.h" /* cbm_slab_destroy_thread */
+#include "foundation/platform.h" /* cbm_default_worker_count */
+#include "pipeline/worker_pool.h"
+#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
@@ -83,8 +88,10 @@ static const char *k8s_basename(const char *path) {
/* ── Kustomize handler ───────────────────────────────────────────── */
-static void handle_kustomize(cbm_pipeline_ctx_t *ctx, const char *path, const char *rel_path,
- CBMFileResult *result) {
+/* `result` is the cached parallel-pass result or, for an uncached file, the
+ * re-extraction the preparation step made; the caller owns it. */
+static void handle_kustomize(cbm_pipeline_ctx_t *ctx, const char *rel_path,
+ const CBMFileResult *result) {
/* Emit Module node for this kustomize overlay file */
char *mod_qn = cbm_infra_qn(ctx->project_name, rel_path, "kustomize", NULL);
if (!mod_qn) {
@@ -99,23 +106,10 @@ static void handle_kustomize(cbm_pipeline_ctx_t *ctx, const char *path, const ch
return;
}
- /* If we have a cached extraction result, emit IMPORTS edges for
+ /* With an extraction result, emit IMPORTS edges for
* resources/bases/patches/components entries */
int import_count = 0;
- CBMFileResult *res = result;
- bool allocated = false;
-
- if (!res) {
- /* Fall back to re-extraction */
- int src_len = 0;
- char *source = k8s_read_file(path, &src_len);
- if (source) {
- res = cbm_extract_file(source, src_len, CBM_LANG_KUSTOMIZE, ctx->project_name, rel_path,
- CBM_EXTRACT_BUDGET, NULL, NULL);
- free(source);
- allocated = true;
- }
- }
+ const CBMFileResult *res = result;
if (res) {
for (int j = 0; j < res->imports.count; j++) {
@@ -140,10 +134,6 @@ static void handle_kustomize(cbm_pipeline_ctx_t *ctx, const char *path, const ch
import_count++;
}
}
-
- if (allocated) {
- cbm_free_result(res);
- }
}
cbm_log_info("pass.k8s.kustomize", "file", rel_path, "imports", itoa_k8s(import_count));
@@ -372,13 +362,10 @@ static void k8s_link_selectors(cbm_pipeline_ctx_t *ctx, const k8s_record_array_t
* must free after this call returns). When `rec` is non-NULL it is populated
* with the first Resource's node id, name and label/selector values for later
* cross-manifest selector matching. */
-static void handle_k8s_manifest(cbm_pipeline_ctx_t *ctx, const char *path, const char *rel_path,
- const char *source, int src_len, k8s_record_t *rec) {
- (void)path; /* retained for symmetry; source is always provided now */
+static void handle_k8s_manifest(cbm_pipeline_ctx_t *ctx, const char *rel_path,
+ const char *source, const CBMFileResult *res, k8s_record_t *rec) {
int resource_count = 0;
- CBMFileResult *res = cbm_extract_file(source, src_len, CBM_LANG_K8S, ctx->project_name,
- rel_path, CBM_EXTRACT_BUDGET, NULL, NULL);
if (!res) {
return;
}
@@ -414,8 +401,6 @@ static void handle_k8s_manifest(cbm_pipeline_ctx_t *ctx, const char *path, const
resource_count++;
}
- cbm_free_result(res);
-
/* Record selector / pod-label values for later Service → workload linking. */
if (rec && rec->node_id > 0) {
k8s_scan_labels(source, rec);
@@ -622,6 +607,95 @@ static void handle_dep_manifest(cbm_pipeline_ctx_t *ctx, const char *rel_path, c
/* ── Pass entry point ────────────────────────────────────────────── */
+/* What one file contributes, prepared off the graph: reading, classifying and
+ * extracting are per-file work, so they run in parallel; everything that
+ * touches the graph buffer then runs on this thread in file order, exactly as
+ * the sequential loop did. Sequentially the pass re-read and re-parsed every
+ * YAML manifest on one thread -- 1.1 s of the Go corpus (6,275 manifests;
+ * profile + waste sanitizer, 2026-09-17). */
+typedef enum { K8S_SKIP = 0, K8S_DEP, K8S_KUSTOMIZE, K8S_HELM, K8S_MANIFEST } k8s_kind_t;
+
+typedef struct {
+ k8s_kind_t kind;
+ char *source; /* DEP, HELM, MANIFEST */
+ int src_len;
+ CBMFileResult *res; /* MANIFEST, or KUSTOMIZE without a cached result */
+} k8s_prep_t;
+
+typedef struct {
+ cbm_pipeline_ctx_t *ctx;
+ const cbm_file_info_t *files;
+ k8s_prep_t *prep; /* indexed from `begin` */
+ int begin;
+ int end;
+ _Atomic int next;
+} k8s_prep_job_t;
+
+enum { K8S_CHUNK = 1024 };
+
+static void k8s_prep_file(const k8s_prep_job_t *job, int i, k8s_prep_t *out) {
+ cbm_pipeline_ctx_t *ctx = job->ctx;
+ const char *path = job->files[i].path;
+ const char *rel = job->files[i].rel_path;
+ CBMLanguage lang = job->files[i].language;
+ const char *base = k8s_basename(rel);
+
+ if (is_gomod_file(base) || lang == CBM_LANG_GOMOD || is_requirements_file(base)) {
+ out->source = k8s_read_file(path, &out->src_len);
+ out->kind = out->source ? K8S_DEP : K8S_SKIP;
+ } else if (cbm_is_kustomize_file(base)) {
+ out->kind = K8S_KUSTOMIZE;
+ bool cached = ctx->result_cache && ctx->result_cache[i];
+ if (!cached) {
+ /* Fall back to re-extraction */
+ int src_len = 0;
+ char *source = k8s_read_file(path, &src_len);
+ if (source) {
+ out->res = cbm_extract_file(source, src_len, CBM_LANG_KUSTOMIZE,
+ ctx->project_name, rel, CBM_EXTRACT_BUDGET, NULL, NULL);
+ free(source);
+ }
+ }
+ } else if (lang == CBM_LANG_YAML || lang == CBM_LANG_K8S) {
+ /* Read source once to classify (and reuse for uncached extraction). */
+ out->source = k8s_read_file(path, &out->src_len);
+ if (!out->source) {
+ return;
+ }
+ if (is_helm_chart_file(base)) {
+ out->kind = K8S_HELM;
+ } else if (cbm_is_k8s_manifest(base, out->source)) {
+ /* Always re-extract with CBM_LANG_K8S regardless of any cached
+ * result: cached results were produced during the parallel YAML
+ * pass and contain no "Resource" definitions. */
+ out->kind = K8S_MANIFEST;
+ out->res = cbm_extract_file(out->source, out->src_len, CBM_LANG_K8S, ctx->project_name,
+ rel, CBM_EXTRACT_BUDGET, NULL, NULL);
+ } else {
+ free(out->source);
+ out->source = NULL;
+ }
+ }
+}
+
+static void k8s_prep_worker(int worker_id, void *arg) {
+ (void)worker_id;
+ k8s_prep_job_t *job = (k8s_prep_job_t *)arg;
+ cbm_work_arena_keep_begin(); /* one scratch arena for this worker's files */
+ while (true) {
+ int i = atomic_fetch_add_explicit(&job->next, SKIP_ONE, memory_order_relaxed);
+ if (i >= job->end) {
+ break;
+ }
+ k8s_prep_file(job, i, &job->prep[i - job->begin]);
+ }
+ /* Per-thread extraction state, torn down like extract_worker's. */
+ cbm_destroy_thread_parser();
+ cbm_work_arena_release();
+ cbm_slab_destroy_thread();
+ cbm_kind_in_set_free_cache();
+}
+
int cbm_pipeline_pass_k8s(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, int file_count) {
cbm_log_info("pass.start", "pass", "k8s", "files", itoa_k8s(file_count));
@@ -636,56 +710,77 @@ int cbm_pipeline_pass_k8s(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
recs.items = calloc(K8S_MAX_RECORDS, sizeof(*recs.items));
recs.cap = recs.items ? K8S_MAX_RECORDS : 0;
- for (int i = 0; i < file_count; i++) {
+ k8s_prep_t *prep = calloc(K8S_CHUNK, sizeof(*prep));
+ if (!prep) {
+ free(recs.items);
+ return CBM_NOT_FOUND;
+ }
+ int workers = cbm_default_worker_count(false);
+
+ for (int begin = 0; begin < file_count; begin += K8S_CHUNK) {
+ int end = begin + K8S_CHUNK < file_count ? begin + K8S_CHUNK : file_count;
if (cbm_pipeline_check_cancel(ctx)) {
+ free(prep);
free(recs.items);
return CBM_NOT_FOUND;
}
-
- const char *path = files[i].path;
- const char *rel = files[i].rel_path;
- CBMLanguage lang = files[i].language;
- const char *base = k8s_basename(rel);
-
- CBMFileResult *cached =
- (ctx->result_cache && ctx->result_cache[i]) ? ctx->result_cache[i] : NULL;
-
- if (is_gomod_file(base) || lang == CBM_LANG_GOMOD || is_requirements_file(base)) {
- int dep_len = 0;
- char *dep_src = k8s_read_file(path, &dep_len);
- if (dep_src) {
- handle_dep_manifest(ctx, rel, dep_src,
- is_requirements_file(base) ? "pypi" : "gomod");
- free(dep_src);
+ memset(prep, 0, (size_t)K8S_CHUNK * sizeof(*prep));
+ k8s_prep_job_t job = {.ctx = ctx, .files = files, .prep = prep, .begin = begin, .end = end};
+ atomic_init(&job.next, begin);
+ cbm_parallel_for(workers, k8s_prep_worker, &job,
+ (cbm_parallel_for_opts_t){.max_workers = workers, .force_pthreads = false});
+
+ bool cancelled = false;
+ for (int i = begin; i < end; i++) {
+ k8s_prep_t *p = &prep[i - begin];
+ if (!cancelled && cbm_pipeline_check_cancel(ctx)) {
+ cancelled = true;
}
- } else if (cbm_is_kustomize_file(base)) {
- handle_kustomize(ctx, path, rel, cached);
- kustomize_count++;
- } else if (lang == CBM_LANG_YAML || lang == CBM_LANG_K8S) {
- /* Read source once to classify (and reuse for uncached extraction). */
- int src_len = 0;
- char *source = k8s_read_file(path, &src_len);
- if (source) {
- if (is_helm_chart_file(base)) {
- handle_helm_chart(ctx, rel, source);
+ if (!cancelled) {
+ const char *rel = files[i].rel_path;
+ const char *base = k8s_basename(rel);
+ CBMFileResult *cached =
+ (ctx->result_cache && ctx->result_cache[i]) ? ctx->result_cache[i] : NULL;
+ switch (p->kind) {
+ case K8S_DEP:
+ handle_dep_manifest(ctx, rel, p->source,
+ is_requirements_file(base) ? "pypi" : "gomod");
+ break;
+ case K8S_KUSTOMIZE:
+ handle_kustomize(ctx, rel, cached ? cached : p->res);
+ kustomize_count++;
+ break;
+ case K8S_HELM:
+ handle_helm_chart(ctx, rel, p->source);
helm_count++;
- } else if (cbm_is_k8s_manifest(base, source)) {
- /* Always re-extract with CBM_LANG_K8S regardless of any cached
- * result: cached results were produced during the parallel YAML
- * pass and contain no "Resource" definitions. Pass the already-
- * read source buffer so handle_k8s_manifest does not re-read. */
- (void)cached; /* cached YAML result intentionally discarded */
- k8s_record_t *rec = (recs.count < recs.cap) ? &recs.items[recs.count] : NULL;
- handle_k8s_manifest(ctx, path, rel, source, src_len, rec);
+ break;
+ case K8S_MANIFEST: {
+ k8s_record_t *rec =
+ (recs.count < recs.cap) ? &recs.items[recs.count] : NULL;
+ handle_k8s_manifest(ctx, rel, p->source, p->res, rec);
if (rec && rec->node_id > 0) {
recs.count++;
}
manifest_count++;
+ break;
+ }
+ case K8S_SKIP:
+ default:
+ break;
}
- free(source);
}
+ free(p->source);
+ if (p->res) {
+ cbm_free_result(p->res);
+ }
+ }
+ if (cancelled) {
+ free(prep);
+ free(recs.items);
+ return CBM_NOT_FOUND;
}
}
+ free(prep);
/* Connect Services to the workloads their selectors target (INFRA_MAPS). */
k8s_link_selectors(ctx, &recs);
diff --git a/src/pipeline/pass_lsp_cross.c b/src/pipeline/pass_lsp_cross.c
index 3450d2c4f..69b0660a3 100644
--- a/src/pipeline/pass_lsp_cross.c
+++ b/src/pipeline/pass_lsp_cross.c
@@ -33,6 +33,7 @@
#include "foundation/hash_table.h"
#include "foundation/log.h"
#include "foundation/compat_fs.h"
+#include "foundation/compat.h" /* CBM_TLS */
#include <stdio.h>
#include <stdint.h>
@@ -956,6 +957,55 @@ bool cbm_pxc_has_cross_lsp(CBMLanguage lang) {
}
}
+/* Per-thread arenas for the per-file scratch below (the walk's overlay and the
+ * append dedup keys), kept rewound between files on a resolve worker that has
+ * promised to drop them at its end (cbm_pxc_thread_scratch_begin/_end).
+ * Opened fresh per file they were 18 k 64 KB-default arenas on the Go corpus,
+ * 1.6 GB allocated and 60 % never written (waste sanitizer, 2026-09-17). An
+ * arena one big file grew past PXC_KEEP_BYTES is dropped, not held; a nested
+ * take finds the slot empty and opens its own. Everywhere else (the sequential
+ * pass) take/give are exactly init/destroy. */
+enum { PXC_SCRATCH_DISPATCH = 0, PXC_SCRATCH_KEYS = 1, PXC_SCRATCH_COUNT = 2 };
+enum { PXC_KEEP_BYTES = 4 * 1024 * 1024 };
+static CBM_TLS bool tl_pxc_keep;
+static CBM_TLS CBMArena tl_pxc_arena[PXC_SCRATCH_COUNT];
+static CBM_TLS bool tl_pxc_live[PXC_SCRATCH_COUNT];
+
+static void pxc_scratch_take(int slot, CBMArena *into) {
+ if (tl_pxc_live[slot]) {
+ *into = tl_pxc_arena[slot];
+ tl_pxc_live[slot] = false;
+ cbm_arena_rewind(into);
+ return;
+ }
+ cbm_arena_init(into);
+}
+
+static void pxc_scratch_give(int slot, CBMArena *from) {
+ if (tl_pxc_keep && !tl_pxc_live[slot] && from->nblocks > 0 &&
+ cbm_arena_capacity(from) <= (size_t)PXC_KEEP_BYTES) {
+ tl_pxc_arena[slot] = *from;
+ tl_pxc_live[slot] = true;
+ memset(from, 0, sizeof(*from));
+ return;
+ }
+ cbm_arena_destroy(from);
+}
+
+void cbm_pxc_thread_scratch_begin(void) {
+ tl_pxc_keep = true;
+}
+
+void cbm_pxc_thread_scratch_end(void) {
+ for (int slot = 0; slot < PXC_SCRATCH_COUNT; slot++) {
+ if (tl_pxc_live[slot]) {
+ cbm_arena_destroy(&tl_pxc_arena[slot]);
+ tl_pxc_live[slot] = false;
+ }
+ }
+ tl_pxc_keep = false;
+}
+
/* Append cross-file results from `src_out` (allocated in a scratch arena
* about to be destroyed) into `dst_calls` (lives in cache_entry->arena),
* copying every string field into dst_arena. A manifest-qualified Rust
@@ -980,7 +1030,7 @@ static void pxc_append_results(CBMArena *dst_arena, CBMResolvedCallArray *dst_ca
return;
CBMArena keys;
- cbm_arena_init(&keys);
+ pxc_scratch_take(PXC_SCRATCH_KEYS, &keys);
CBMHashTable *seen = cbm_ht_create((uint32_t)(dst_calls->count + src_out->count + 1));
/* Per-file Rust resolution may already have confidently matched the tail
@@ -1073,7 +1123,7 @@ static void pxc_append_results(CBMArena *dst_arena, CBMResolvedCallArray *dst_ca
}
cbm_ht_free(seen);
- cbm_arena_destroy(&keys);
+ pxc_scratch_give(PXC_SCRATCH_KEYS, &keys);
}
/* Merge exact synthetic call carriers produced by a cross-LSP resolver. The
@@ -1087,7 +1137,7 @@ static void pxc_append_synthetic_calls(CBMArena *dst_arena, CBMCallArray *dst_ca
return;
CBMArena keys;
- cbm_arena_init(&keys);
+ pxc_scratch_take(PXC_SCRATCH_KEYS, &keys);
CBMHashTable *seen = cbm_ht_create((uint32_t)(dst_calls->count + src_calls->count + 1));
for (int i = 0; i < dst_calls->count; i++) {
@@ -1146,7 +1196,7 @@ static void pxc_append_synthetic_calls(CBMArena *dst_arena, CBMCallArray *dst_ca
}
cbm_ht_free(seen);
- cbm_arena_destroy(&keys);
+ pxc_scratch_give(PXC_SCRATCH_KEYS, &keys);
}
/* ── Rust workspace manifest (Cargo.toml) for cross-CRATE resolution ──
@@ -1214,7 +1264,7 @@ void cbm_pxc_run_one(CBMLanguage lang, CBMFileResult *r, const char *source, int
TSTree *tree = r->cached_tree; /* may be NULL — LSP re-parses then */
CBMArena scratch;
- cbm_arena_init(&scratch);
+ pxc_scratch_take(PXC_SCRATCH_DISPATCH, &scratch);
CBMResolvedCallArray out;
memset(&out, 0, sizeof(out));
CBMCallArray synthetic_calls;
@@ -1271,7 +1321,7 @@ void cbm_pxc_run_one(CBMLanguage lang, CBMFileResult *r, const char *source, int
pxc_append_results(&r->arena, &r->resolved_calls, &out);
pxc_append_synthetic_calls(&r->arena, &r->calls, &synthetic_calls);
- cbm_arena_destroy(&scratch);
+ pxc_scratch_give(PXC_SCRATCH_DISPATCH, &scratch);
}
/* Variant of cbm_pxc_run_one for TS/JS/JSX/TSX with explicit dialect
@@ -1281,7 +1331,7 @@ void cbm_pxc_run_one_ts(CBMFileResult *r, const char *source, int source_len, co
const char **imp_qns, int imp_count, bool js_mode, bool jsx_mode,
bool dts_mode) {
CBMArena scratch;
- cbm_arena_init(&scratch);
+ pxc_scratch_take(PXC_SCRATCH_DISPATCH, &scratch);
CBMResolvedCallArray out;
memset(&out, 0, sizeof(out));
@@ -1289,7 +1339,7 @@ void cbm_pxc_run_one_ts(CBMFileResult *r, const char *source, int source_len, co
def_count, imp_names, imp_qns, imp_count, r->cached_tree, &out);
pxc_append_results(&r->arena, &r->resolved_calls, &out);
- cbm_arena_destroy(&scratch);
+ pxc_scratch_give(PXC_SCRATCH_DISPATCH, &scratch);
}
/* Parse the project's root Cargo.toml (if present) into `out_m`, using
@@ -1371,7 +1421,7 @@ void cbm_pxc_dispatch_file(CBMLanguage lang, CBMFileResult *result, const char *
* shared registry -- a use-after-free class the moment the arena was
* not the result arena (ASan, lsp_resolution_probe). */
CBMArena scratch;
- cbm_arena_init(&scratch);
+ pxc_scratch_take(PXC_SCRATCH_DISPATCH, &scratch);
CBMTypeRegistry overlay;
cbm_registry_init(&overlay, &scratch);
overlay.fallback = prebuilt;
@@ -1452,7 +1502,7 @@ void cbm_pxc_dispatch_file(CBMLanguage lang, CBMFileResult *result, const char *
pxc_append_results(&result->arena, &result->resolved_calls, &out);
pxc_append_synthetic_calls(&result->arena, &result->calls, &synthetic_calls);
}
- cbm_arena_destroy(&scratch);
+ pxc_scratch_give(PXC_SCRATCH_DISPATCH, &scratch);
}
if (used_prebuilt) {
@@ -1493,7 +1543,7 @@ void cbm_pxc_dispatch_file(CBMLanguage lang, CBMFileResult *result, const char *
CBMTypeRegistry *shared = rust_shared_get ? rust_shared_get(rust_shared_ctx) : NULL;
if (shared) {
CBMArena scratch;
- cbm_arena_init(&scratch);
+ pxc_scratch_take(PXC_SCRATCH_DISPATCH, &scratch);
CBMResolvedCallArray out = {0};
CBMCallArray synthetic_calls = {0};
cbm_run_rust_lsp_cross_with_registry(
@@ -1501,7 +1551,7 @@ void cbm_pxc_dispatch_file(CBMLanguage lang, CBMFileResult *result, const char *
result->cached_tree, cbm_pxc_get_rust_manifest(), &out, &synthetic_calls);
pxc_append_results(&result->arena, &result->resolved_calls, &out);
pxc_append_synthetic_calls(&result->arena, &result->calls, &synthetic_calls);
- cbm_arena_destroy(&scratch);
+ pxc_scratch_give(PXC_SCRATCH_DISPATCH, &scratch);
} else {
cbm_pxc_run_one(lang, result, source, source_len, def_module, file_defs, file_def_count,
imp_keys, imp_vals, imp_count);
diff --git a/src/pipeline/pass_lsp_cross.h b/src/pipeline/pass_lsp_cross.h
index 04a65f2ba..5bf9ba5d5 100644
--- a/src/pipeline/pass_lsp_cross.h
+++ b/src/pipeline/pass_lsp_cross.h
@@ -187,6 +187,10 @@ struct CBMCargoManifest;
bool cbm_pxc_build_rust_manifest(const cbm_pipeline_ctx_t *ctx, CBMArena *manifest_arena,
struct CBMCargoManifest *out_manifest);
void cbm_pxc_set_rust_manifest(const struct CBMCargoManifest *manifest);
+/* A resolve worker keeps its per-file cross-LSP scratch arenas between files
+ * from _begin to _end; _end must run on the same thread before it exits. */
+void cbm_pxc_thread_scratch_begin(void);
+void cbm_pxc_thread_scratch_end(void);
const struct CBMCargoManifest *cbm_pxc_get_rust_manifest(void);
/* Run the cross-file LSP resolver for non-TS languages. Appends
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index f5e4ab4d2..7e9dbbdea 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -1532,8 +1532,11 @@ int cbm_parallel_extract(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
/* ── Phase 3B: Serial Registry Build ─────────────────────────────── */
/* Register one definition and create DEFINES + DEFINES_METHOD edges. Returns edge count. */
-static int register_and_link_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, const char *rel,
- int *reg_entries) {
+/* `file_node_id` is the defining file's node id (0 when it has none), looked up
+ * once per file by the caller: computing the file QN and finding its node for
+ * every definition was 700 k allocations and lookups on the Go corpus. */
+static int register_and_link_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def,
+ int64_t file_node_id, int *reg_entries) {
int edges = 0;
if (!def->name || !def->qualified_name || !def->label) {
return 0;
@@ -1544,14 +1547,11 @@ static int register_and_link_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *d
cbm_registry_add(ctx->registry, def->name, def->qualified_name, def->label);
(*reg_entries)++;
}
- char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
- const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
const cbm_gbuf_node_t *def_node = cbm_gbuf_find_by_qn(ctx->gbuf, def->qualified_name);
- if (file_node && def_node) {
- cbm_gbuf_insert_edge(ctx->gbuf, file_node->id, def_node->id, "DEFINES", "{}");
+ if (file_node_id > 0 && def_node) {
+ cbm_gbuf_insert_edge(ctx->gbuf, file_node_id, def_node->id, "DEFINES", "{}");
edges++;
}
- free(file_qn);
if (def->parent_class && strcmp(def->label, "Method") == 0) {
const cbm_gbuf_node_t *parent = cbm_gbuf_find_by_qn(ctx->gbuf, def->parent_class);
if (parent && def_node) {
@@ -1675,8 +1675,15 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
const char *rel = files[i].rel_path;
/* Register callable symbols + DEFINES/DEFINES_METHOD edges */
- for (int d = 0; d < result->defs.count; d++) {
- defines_edges += register_and_link_def(ctx, &result->defs.items[d], rel, &reg_entries);
+ if (result->defs.count > 0) {
+ char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
+ const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
+ int64_t file_node_id = file_node ? file_node->id : 0;
+ free(file_qn);
+ for (int d = 0; d < result->defs.count; d++) {
+ defines_edges +=
+ register_and_link_def(ctx, &result->defs.items[d], file_node_id, &reg_entries);
+ }
}
imports_edges += create_imports_edges(ctx, result, rel, namespace_map);
@@ -2415,6 +2422,36 @@ static void emit_service_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
}
/* Find the source node for an edge: enclosing function or file node. */
+/* This worker's last file-node answer. The graph buffer is read-only for the
+ * whole of phase 4 and a file's rel_path pointer is stable within it, so the
+ * pair identifies the answer; resolve_worker clears it at both ends so a later
+ * phase can never match a recycled address. Computing the file QN and looking
+ * it up ran for every call, usage, throw and read/write whose enclosing
+ * function is not a graph node of its own. */
+static CBM_TLS const cbm_gbuf_t *tl_file_node_gbuf;
+static CBM_TLS const char *tl_file_node_rel;
+static CBM_TLS const cbm_gbuf_node_t *tl_file_node;
+
+static void file_node_cache_clear(void) {
+ tl_file_node_gbuf = NULL;
+ tl_file_node_rel = NULL;
+ tl_file_node = NULL;
+}
+
+static const cbm_gbuf_node_t *file_node_for(const cbm_gbuf_t *gbuf, const char *project,
+ const char *rel) {
+ if (tl_file_node_gbuf == gbuf && tl_file_node_rel == rel) {
+ return tl_file_node;
+ }
+ char *file_qn = cbm_pipeline_fqn_compute(project, rel, "__file__");
+ const cbm_gbuf_node_t *node = cbm_gbuf_find_by_qn(gbuf, file_qn);
+ free(file_qn);
+ tl_file_node_gbuf = gbuf;
+ tl_file_node_rel = rel;
+ tl_file_node = node;
+ return node;
+}
+
static const cbm_gbuf_node_t *find_source_node(const cbm_gbuf_t *gbuf, const char *project,
const char *rel, const char *enclosing_qn) {
const cbm_gbuf_node_t *src = NULL;
@@ -2428,9 +2465,7 @@ static const cbm_gbuf_node_t *find_source_node(const cbm_gbuf_t *gbuf, const cha
}
}
if (!src) {
- char *file_qn = cbm_pipeline_fqn_compute(project, rel, "__file__");
- src = cbm_gbuf_find_by_qn(gbuf, file_qn);
- free(file_qn);
+ src = file_node_for(gbuf, project, rel);
}
return src;
}
@@ -3353,6 +3388,8 @@ static void resolve_worker(int worker_id, void *ctx_ptr) {
* lookup after the first miss for each QN. Scoped to the worker's
* lifetime in the parallel_resolve phase. */
cbm_service_pattern_cache_begin();
+ cbm_pxc_thread_scratch_begin();
+ file_node_cache_clear();
while (SKIP_ONE) {
int file_idx =
@@ -3604,6 +3641,8 @@ static void resolve_worker(int worker_id, void *ctx_ptr) {
cbm_destroy_thread_parser();
cbm_slab_destroy_thread();
cbm_service_pattern_cache_end();
+ cbm_pxc_thread_scratch_end();
+ file_node_cache_clear();
}
int cbm_parallel_resolve(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, int file_count,
diff --git a/src/pipeline/pass_semantic.c b/src/pipeline/pass_semantic.c
index 58821beb0..c2275c1ae 100644
--- a/src/pipeline/pass_semantic.c
+++ b/src/pipeline/pass_semantic.c
@@ -307,6 +307,103 @@ static int check_go_class_implements(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_nod
return edges;
}
+/* ── Method-name index over the concrete types ────────────────────
+ *
+ * Checking every interface against every struct and named type is
+ * interfaces x types work: the scaling lane measured it at 4.00x per 2x corpus
+ * (2026-09-17). A type can only satisfy an interface if it supplies EVERY
+ * method name, so it is in the candidate list of each of those names: checking
+ * just the shortest such list, in the original order, runs exactly the checks
+ * that can succeed, in exactly the order they ran before -- the edges and their
+ * insertion order are unchanged. A type supplies a name by either lookup
+ * check_go_class_implements uses: a DEFINES_METHOD edge to a node of that name,
+ * or a node whose QN is "<TypeQN>.<name>". */
+typedef struct {
+ int *items; /* ascending type indices, no duplicates once sealed */
+ int count;
+ int cap;
+} go_type_list_t;
+
+typedef struct {
+ CBMHashTable *by_name; /* method name (borrowed) -> go_type_list_t* */
+ CBMHashTable *type_by_qn; /* type QN (borrowed) -> (void *)(index + 1) */
+} go_method_index_t;
+
+static void go_index_add(go_method_index_t *ix, const char *name, int type_index) {
+ go_type_list_t *list = cbm_ht_get(ix->by_name, name);
+ if (!list) {
+ list = cbm_calloc(CBM_MEM_CLASS_SEMANTIC, sizeof(*list));
+ if (!list) {
+ return;
+ }
+ cbm_ht_set(ix->by_name, name, list);
+ }
+ if (list->count > 0 && list->items[list->count - 1] == type_index) {
+ return;
+ }
+ if (list->count == list->cap) {
+ int cap = list->cap ? list->cap * PAIR_LEN : CBM_SZ_4;
+ int *grown = cbm_realloc(CBM_MEM_CLASS_SEMANTIC, list->items, (size_t)cap * sizeof(int));
+ if (!grown) {
+ return;
+ }
+ list->items = grown;
+ list->cap = cap;
+ }
+ list->items[list->count++] = type_index;
+}
+
+static void go_index_visit_node(const cbm_gbuf_node_t *node, void *userdata) {
+ go_method_index_t *ix = userdata;
+ const char *qn = node->qualified_name;
+ const char *dot = qn ? strrchr(qn, '.') : NULL;
+ if (!dot || dot == qn || !dot[SKIP_ONE]) {
+ return;
+ }
+ char parent[CBM_SZ_512];
+ size_t len = (size_t)(dot - qn);
+ if (len >= sizeof(parent)) {
+ return; /* longer than the lookup buffer check_go_class_implements builds */
+ }
+ memcpy(parent, qn, len);
+ parent[len] = '\0';
+ void *slot = cbm_ht_get(ix->type_by_qn, parent);
+ if (slot) {
+ go_index_add(ix, dot + SKIP_ONE, (int)((intptr_t)slot - SKIP_ONE));
+ }
+}
+
+static int cmp_int_asc(const void *a, const void *b) {
+ int x = *(const int *)a;
+ int y = *(const int *)b;
+ return (x > y) - (x < y);
+}
+
+static void go_index_seal_list(const char *key, void *val, void *userdata) {
+ (void)key;
+ (void)userdata;
+ go_type_list_t *list = val;
+ if (list->count < PAIR_LEN) {
+ return;
+ }
+ qsort(list->items, (size_t)list->count, sizeof(int), cmp_int_asc);
+ int out = SKIP_ONE;
+ for (int i = SKIP_ONE; i < list->count; i++) {
+ if (list->items[i] != list->items[out - SKIP_ONE]) {
+ list->items[out++] = list->items[i];
+ }
+ }
+ list->count = out;
+}
+
+static void go_index_free_list(const char *key, void *val, void *userdata) {
+ (void)key;
+ (void)userdata;
+ go_type_list_t *list = val;
+ cbm_free(CBM_MEM_CLASS_SEMANTIC, list->items);
+ cbm_free(CBM_MEM_CLASS_SEMANTIC, list);
+}
+
int cbm_pipeline_implements_go(cbm_pipeline_ctx_t *ctx) {
int edge_count = 0;
@@ -330,6 +427,42 @@ int cbm_pipeline_implements_go(cbm_pipeline_ctx_t *ctx) {
return 0;
}
+ /* Candidate index (see go_method_index_t). Type index t is the position in
+ * the original iteration order: structs first, then classes. */
+ int type_count = struct_count + class_count;
+ const cbm_gbuf_node_t **types =
+ cbm_alloc(CBM_MEM_CLASS_SEMANTIC, (size_t)type_count * sizeof(*types));
+ go_method_index_t ix = {
+ .by_name = cbm_ht_create_in(CBM_MEM_CLASS_SEMANTIC, (uint32_t)CBM_SZ_1K),
+ .type_by_qn = cbm_ht_create_in(CBM_MEM_CLASS_SEMANTIC, (uint32_t)type_count),
+ };
+ bool indexed = types && ix.by_name && ix.type_by_qn;
+ if (indexed) {
+ for (int c = 0; c < struct_count; c++) {
+ types[c] = structs[c];
+ }
+ for (int c = 0; c < class_count; c++) {
+ types[struct_count + c] = classes[c];
+ }
+ for (int t = 0; t < type_count; t++) {
+ if (types[t]->qualified_name) {
+ cbm_ht_set(ix.type_by_qn, types[t]->qualified_name, (void *)(intptr_t)(t + SKIP_ONE));
+ }
+ const cbm_gbuf_edge_t **tdm = NULL;
+ int tdm_count = 0;
+ cbm_gbuf_find_edges_by_source_type(ctx->gbuf, types[t]->id, "DEFINES_METHOD", &tdm,
+ &tdm_count);
+ for (int d = 0; d < tdm_count; d++) {
+ const cbm_gbuf_node_t *cm = cbm_gbuf_find_by_id(ctx->gbuf, tdm[d]->target_id);
+ if (cm && cm->name) {
+ go_index_add(&ix, cm->name, t);
+ }
+ }
+ }
+ cbm_gbuf_foreach_node(ctx->gbuf, go_index_visit_node, &ix);
+ cbm_ht_foreach(ix.by_name, go_index_seal_list, NULL);
+ }
+
for (int i = 0; i < iface_count; i++) {
const cbm_gbuf_node_t *iface = ifaces[i];
if (!iface->file_path || !fp_ends_with(iface->file_path, ".go")) {
@@ -358,15 +491,43 @@ int cbm_pipeline_implements_go(cbm_pipeline_ctx_t *ctx) {
continue;
}
- /* Check each concrete-type node (Struct + Class) for method-set
- * satisfaction. */
- for (int c = 0; c < struct_count; c++) {
- edge_count += check_go_class_implements(ctx, structs[c], iface, imethods, im_count);
+ if (!indexed) {
+ /* Index unavailable (allocation failure): the full scan. */
+ for (int c = 0; c < struct_count; c++) {
+ edge_count += check_go_class_implements(ctx, structs[c], iface, imethods, im_count);
+ }
+ for (int c = 0; c < class_count; c++) {
+ edge_count += check_go_class_implements(ctx, classes[c], iface, imethods, im_count);
+ }
+ continue;
}
- for (int c = 0; c < class_count; c++) {
- edge_count += check_go_class_implements(ctx, classes[c], iface, imethods, im_count);
+ /* Only the types that supply the interface's rarest method name. */
+ const go_type_list_t *shortest = NULL;
+ bool missing = false;
+ for (int m = 0; m < im_count; m++) {
+ const go_type_list_t *list = cbm_ht_get(ix.by_name, imethods[m].name);
+ if (!list || list->count == 0) {
+ missing = true; /* no type supplies this name: none can satisfy */
+ break;
+ }
+ if (!shortest || list->count < shortest->count) {
+ shortest = list;
+ }
+ }
+ if (missing || !shortest) {
+ continue;
+ }
+ for (int k = 0; k < shortest->count; k++) {
+ edge_count +=
+ check_go_class_implements(ctx, types[shortest->items[k]], iface, imethods, im_count);
}
}
+ if (ix.by_name) {
+ cbm_ht_foreach(ix.by_name, go_index_free_list, NULL);
+ cbm_ht_free(ix.by_name);
+ }
+ cbm_ht_free(ix.type_by_qn);
+ cbm_free(CBM_MEM_CLASS_SEMANTIC, types);
return edge_count;
}
diff --git a/src/pipeline/pass_semantic_edges.c b/src/pipeline/pass_semantic_edges.c
index b71c85a83..dd8057d61 100644
--- a/src/pipeline/pass_semantic_edges.c
+++ b/src/pipeline/pass_semantic_edges.c
@@ -704,12 +704,22 @@ static void vec_build_worker(int worker_id, void *ctx_ptr) {
int tc = vc->token_counts[f];
char **tokens = &vc->all_tokens[vc->offsets[f]];
+ /* Each token is resolved to its corpus index ONCE: both loops below ask
+ * about every token, and asking by name cost a hash lookup plus a
+ * strtol per question -- three per token (35 M repeated lookups of an
+ * unchanged table on the Go corpus, waste sanitizer 2026-09-17). */
+ int *token_index = cbm_alloc(CBM_MEM_CLASS_SEMANTIC, (size_t)tc * sizeof(int));
+ for (int t = 0; token_index && t < tc; t++) {
+ token_index[t] = cbm_sem_corpus_token_index(vc->corpus, tokens[t]);
+ }
+
/* TF-IDF weights */
int *indices = cbm_alloc(CBM_MEM_CLASS_SEMANTIC, (size_t)tc * sizeof(int));
float *weights = cbm_alloc(CBM_MEM_CLASS_SEMANTIC, (size_t)tc * sizeof(float));
int tfidf_len = 0;
for (int t = 0; t < tc; t++) {
- float idf = cbm_sem_corpus_idf(vc->corpus, tokens[t]);
+ float idf = token_index ? cbm_sem_corpus_idf_at(vc->corpus, token_index[t])
+ : cbm_sem_corpus_idf(vc->corpus, tokens[t]);
if (idf > 0.0F) {
indices[tfidf_len] = t;
weights[tfidf_len] = idf;
@@ -726,12 +736,16 @@ static void vec_build_worker(int worker_id, void *ctx_ptr) {
cbm_sem_vec_t ri_dense;
memset(&ri_dense, 0, sizeof(cbm_sem_vec_t));
for (int t = 0; t < tc; t++) {
- const cbm_sem_vec_t *ri = cbm_sem_corpus_ri_vec(vc->corpus, tokens[t]);
+ const cbm_sem_vec_t *ri = token_index
+ ? cbm_sem_corpus_ri_vec_at(vc->corpus, token_index[t])
+ : cbm_sem_corpus_ri_vec(vc->corpus, tokens[t]);
if (ri) {
- float idf = cbm_sem_corpus_idf(vc->corpus, tokens[t]);
+ float idf = token_index ? cbm_sem_corpus_idf_at(vc->corpus, token_index[t])
+ : cbm_sem_corpus_idf(vc->corpus, tokens[t]);
cbm_sem_vec_add_scaled(&ri_dense, ri, idf);
}
}
+ cbm_free(CBM_MEM_CLASS_SEMANTIC, token_index);
cbm_sem_normalize(&ri_dense);
cbm_rsq_encode(ri_dense.v, &vc->funcs[f].ri_code);
@@ -829,18 +843,30 @@ enum {
SCORE_SEEN_EMPTY = -1,
};
-/* Check whether `j` has already been recorded in the open-addressed `seen`
- * set for this function; insert it if not. Returns true when the insertion
- * was fresh (caller should add to candidates). */
-static bool score_seen_insert(int *seen, int j) {
+/* The open-addressed `seen` set of one worker, reused for every function it
+ * scores: a slot belongs to the current function only when its stamp says so,
+ * so starting a function costs nothing where re-filling 8,192 slots cost a
+ * 32 KB write per function (90 k on the Go corpus, waste sanitizer
+ * 2026-09-17). Same size and probe order: the same candidates, in order. */
+typedef struct {
+ int ids[SCORE_SEEN_CAP];
+ int stamp[SCORE_SEEN_CAP];
+ int current; /* function index + 1: unique per function, never 0 */
+} score_seen_t;
+
+/* Check whether `j` has already been recorded in the `seen` set for this
+ * function; insert it if not. Returns true when the insertion was fresh
+ * (caller should add to candidates). */
+static bool score_seen_insert(score_seen_t *seen, int j) {
uint32_t slot = (uint32_t)j & SCORE_SEEN_MASK;
for (int p = 0; p < SCORE_SEEN_CAP; p++) {
uint32_t idx = (slot + (uint32_t)p) & SCORE_SEEN_MASK;
- if (seen[idx] == SCORE_SEEN_EMPTY) {
- seen[idx] = j;
+ if (seen->stamp[idx] != seen->current) {
+ seen->stamp[idx] = seen->current;
+ seen->ids[idx] = j;
return true;
}
- if (seen[idx] == j) {
+ if (seen->ids[idx] == j) {
return false;
}
}
@@ -850,7 +876,7 @@ static bool score_seen_insert(int *seen, int j) {
/* Collect the unique candidate function indices for node `i` by iterating
* every LSH band and merging bucket members via `seen[]`. Returns the
* populated candidate count. */
-static int score_collect_candidates(score_ctx_t *sc, int i, int *seen, int *candidates,
+static int score_collect_candidates(score_ctx_t *sc, int i, score_seen_t *seen, int *candidates,
int cand_cap) {
int cand_count = 0;
for (int b = 0; b < SEM_LSH_BANDS && cand_count < cand_cap; b++) {
@@ -902,22 +928,24 @@ static void score_try_emit(score_ctx_t *sc, int i, int j, int c, deferred_edge_b
static void score_worker(int worker_id, void *ctx_ptr) {
score_ctx_t *sc = ctx_ptr;
deferred_edge_buf_t *my_buf = &sc->worker_bufs[worker_id];
+ score_seen_t *seen = cbm_calloc(CBM_MEM_CLASS_SEMANTIC, sizeof(score_seen_t));
+ if (!seen) {
+ return; /* the other workers claim the functions this one leaves */
+ }
while (true) {
int i = atomic_fetch_add_explicit(&sc->next_idx, SKIP_ONE, memory_order_relaxed);
if (i >= sc->func_count) {
break;
}
- int seen[SCORE_SEEN_CAP];
- for (int s = 0; s < SCORE_SEEN_CAP; s++) {
- seen[s] = SCORE_SEEN_EMPTY;
- }
+ seen->current = i + SKIP_ONE;
int candidates[SEM_MAX_CANDIDATES];
int cand_count = score_collect_candidates(sc, i, seen, candidates, SEM_MAX_CANDIDATES);
for (int c = 0; c < cand_count; c++) {
score_try_emit(sc, i, candidates[c], c, my_buf);
}
}
+ cbm_free(CBM_MEM_CLASS_SEMANTIC, seen);
}
/* ── Parallel Phase 1b: decode minhash/profile + build per-func vectors ── */
diff --git a/src/pipeline/pass_similarity.c b/src/pipeline/pass_similarity.c
index 8b456e100..8bef7933d 100644
--- a/src/pipeline/pass_similarity.c
+++ b/src/pipeline/pass_similarity.c
@@ -207,8 +207,13 @@ static void sim_query_worker(int worker_id, void *ctx_ptr) {
sim_query_ctx_t *sc = ctx_ptr;
sim_edge_buf_t *my_buf = &sc->worker_bufs[worker_id];
- /* Thread-local candidate buffer (stack-allocated) */
+ /* Thread-local candidate buffer (stack-allocated) and one dedup set for
+ * all of this worker's queries. */
const cbm_lsh_entry_t *cands[SIM_CAND_CAP];
+ cbm_lsh_seen_t *seen = cbm_lsh_seen_new();
+ if (!seen) {
+ return; /* the other workers claim the entries this one leaves */
+ }
while (true) {
int i = atomic_fetch_add_explicit(&sc->next_idx, SKIP_ONE, memory_order_relaxed);
@@ -222,7 +227,7 @@ static void sim_query_worker(int worker_id, void *ctx_ptr) {
}
const fp_entry_t *src = &sc->entries[i];
- int cand_count = cbm_lsh_query_into(sc->lsh, &src->fp, cands, SIM_CAND_CAP);
+ int cand_count = cbm_lsh_query_into_seen(sc->lsh, &src->fp, cands, SIM_CAND_CAP, seen);
int emitted = 0;
for (int c = 0; c < cand_count; c++) {
@@ -260,6 +265,7 @@ static void sim_query_worker(int worker_id, void *ctx_ptr) {
atomic_fetch_add_explicit(&sc->edge_counts[i], emitted, memory_order_relaxed);
}
}
+ cbm_lsh_seen_free(seen);
}
/* Merge worker edge buffers into gbuf. Returns total edge count. Frees worker buffers. */
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 1d9b72eac..187a13a1e 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -317,6 +317,7 @@ static void log_phase_mem(const char *phase) {
cbm_log_info("mem.phase", "phase", phase, "rss_mb", rss_mb, "footprint_mb", footprint_mb,
"commit_mb", commit_mb, "tracked_mb", tracked_mb, "peak_mb", peak_mb,
"peak_charged_mb", peak_charged_mb);
+ cbm_mem_allocator_stats_log(phase);
}
/* ── Lifecycle ──────────────────────────────────────────────────── */
@@ -1567,6 +1568,7 @@ static int run_parallel_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
* disk by now); the per-language cross registries share it. */
CBMArena cross_lsp_arena;
cbm_arena_init(&cross_lsp_arena);
+ CBM_PROF_START(t_collect_defs);
if (run_cross_lsp) {
def_modules = (char **)calloc((size_t)file_count, sizeof(char *));
def_starts = (int *)calloc((size_t)file_count + 1, sizeof(int));
@@ -1575,10 +1577,12 @@ static int run_parallel_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
def_modules, &def_count, def_starts)
: NULL;
}
+ CBM_PROF_END_N("lsp_cross_prepare", "1_collect_all_defs", t_collect_defs, def_count);
/* Serialize per-file LSP surfaces NOW — the result cache dies with this
* pass, and the rows are what lets an incremental run detect body-only
* edits and rehydrate cross registries without re-parsing the world.
* Failure only degrades: no rows → the incremental route full-rebuilds. */
+ CBM_PROF_START(t_surfaces);
if (ctx->pipeline && all_defs && def_starts) {
cbm_lsp_surface_row_t *surface_rows = NULL;
int surface_count = 0;
@@ -1589,6 +1593,7 @@ static int run_parallel_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
cbm_log_warn("lsp_surface.serialize_failed", "files", itoa_buf(file_count));
}
}
+ CBM_PROF_END_N("lsp_cross_prepare", "2_surface_rows", t_surfaces, file_count);
free(def_starts);
/* Build inverted index: module_qn → defs. The fused resolve_worker
* uses this to filter the global all_defs[] down to just the defs
@@ -1596,8 +1601,10 @@ static int run_parallel_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
* gopls "package summary" pattern. Drops per-file registry build
* cost from O(all_defs) to O(relevant_defs), typically 50-100×
* smaller per file. */
+ CBM_PROF_START(t_module_index);
CBMModuleDefIndex *module_def_index =
all_defs ? cbm_pxc_build_module_def_index(all_defs, def_count) : NULL;
+ CBM_PROF_END_N("lsp_cross_prepare", "3_module_def_index", t_module_index, def_count);
/* Tier 2 full: pre-build per-language cross-LSP registries.
* Built ONCE here; shared READ-ONLY across all files of that language
* during resolve. Per-file work is then: parse + AST walk + O(1) lookups
diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.c
index 7c84726e2..28ec2f282 100644
--- a/src/pipeline/pipeline_incremental.c
+++ b/src/pipeline/pipeline_incremental.c
@@ -172,6 +172,10 @@ static int semantic_manifest_hash_file(const char *abs_path, char out[CBM_SHA256
if (!f) {
return CBM_NOT_FOUND;
}
+ /* Reads go through buf below: a stdio buffer of its own was one unused
+ * 4 KB allocation per file (43 k on the Go corpus, waste sanitizer
+ * 2026-09-17). */
+ (void)setvbuf(f, NULL, _IONBF, 0);
cbm_sha256_ctx sha;
cbm_sha256_init(&sha);
unsigned char buf[CBM_SZ_64K];
diff --git a/src/pipeline/registry.c b/src/pipeline/registry.c
index fa926d3c5..3a2fb7912 100644
--- a/src/pipeline/registry.c
+++ b/src/pipeline/registry.c
@@ -72,8 +72,18 @@ const char *cbm_confidence_band(double score) {
/* ── Internal types ──────────────────────────────────────────────── */
-/* Array of QN strings for byName index */
-typedef CBM_DYN_ARRAY(char *) qn_array_t;
+/* Array of QN strings for byName index, with the test/mock verdict of each
+ * name cached beside it: the scorer asked is_test_qn for every candidate of
+ * every unresolved call, and the answer depends only on the name, which never
+ * changes once registered (waste sanitizer scaling lane, 2026-09-17). Field
+ * order and names match CBM_DYN_ARRAY(char *) so cbm_da_push works on it. */
+typedef struct {
+ char **items;
+ int count;
+ int cap;
+ uint8_t *is_test; /* is_test_qn(items[i]), one byte per entry */
+ int is_test_cap;
+} qn_array_t;
struct cbm_registry {
/* Interned label strings (<=~30 distinct labels; owned here, freed in
@@ -111,64 +121,124 @@ static const char *simple_name(const char *qn) {
/* Extract everything before the last dot. Returns heap-allocated string. */
-/* Count common dot-separated prefix segments. */
+/* Count common dot-separated prefix segments. One pass over the shared prefix:
+ * the same answer as measuring each segment of both strings with strchr and
+ * strlen and comparing them, which walked every candidate's whole name per
+ * call (the scaling lane's x3.4, waste sanitizer 2026-09-17). */
static int common_prefix_len(const char *a, const char *b) {
if (!a || !b) {
return 0;
}
int count = 0;
- while (*a && *b) {
- /* Find next segment in each */
- const char *adot = strchr(a, '.');
- const char *bdot = strchr(b, '.');
- size_t alen = adot ? (size_t)(adot - a) : strlen(a);
- size_t blen = bdot ? (size_t)(bdot - b) : strlen(b);
- if (alen != blen || memcmp(a, b, alen) != 0) {
- break;
+ size_t i = 0;
+ while (a[i] != '\0' && b[i] != '\0') { /* a segment starts here in both */
+ while (a[i] == b[i] && a[i] != '.' && a[i] != '\0') {
+ i++;
+ }
+ bool a_ends = a[i] == '.' || a[i] == '\0';
+ bool b_ends = b[i] == '.' || b[i] == '\0';
+ if (!a_ends || !b_ends) {
+ break; /* the segments differ in content or in length */
}
count++;
- a += alen + (adot ? SKIP_ONE : 0);
- b += blen + (bdot ? SKIP_ONE : 0);
- if (!adot || !bdot) {
- break;
+ if (a[i] == '\0' || b[i] == '\0') {
+ break; /* one name stops here: no further segment to compare */
}
+ i++; /* both stand on '.': step into the next segment */
}
return count;
}
enum { REG_TEST_PENALTY = 1000 };
-/* Check if a qualified name looks like a test/mock path. */
+/* Check if a qualified name looks like a test/mock path: does it contain any of
+ * Test, test, Mock, mock, Stub, stub, Fake, fake, Fixture, spec? One pass that
+ * dispatches on the first character instead of ten strstr scans of the whole
+ * name (this ran for every candidate of every unresolved call: the scaling
+ * lane's x4.0, waste sanitizer 2026-09-17). */
static bool is_test_qn(const char *qn) {
if (!qn) {
return false;
}
- return (strstr(qn, "Test") != NULL || strstr(qn, "test") != NULL ||
- strstr(qn, "Mock") != NULL || strstr(qn, "mock") != NULL ||
- strstr(qn, "Stub") != NULL || strstr(qn, "stub") != NULL ||
- strstr(qn, "Fake") != NULL || strstr(qn, "fake") != NULL ||
- strstr(qn, "Fixture") != NULL || strstr(qn, "spec") != NULL);
+ for (const char *p = qn; *p; p++) {
+ switch (*p) {
+ case 'T':
+ if (strncmp(p, "Test", 4) == 0) {
+ return true;
+ }
+ break;
+ case 't':
+ if (strncmp(p, "test", 4) == 0) {
+ return true;
+ }
+ break;
+ case 'M':
+ if (strncmp(p, "Mock", 4) == 0) {
+ return true;
+ }
+ break;
+ case 'm':
+ if (strncmp(p, "mock", 4) == 0) {
+ return true;
+ }
+ break;
+ case 'S':
+ if (strncmp(p, "Stub", 4) == 0) {
+ return true;
+ }
+ break;
+ case 's':
+ if (strncmp(p, "stub", 4) == 0 || strncmp(p, "spec", 4) == 0) {
+ return true;
+ }
+ break;
+ case 'F':
+ if (strncmp(p, "Fake", 4) == 0 || strncmp(p, "Fixture", 7) == 0) {
+ return true;
+ }
+ break;
+ case 'f':
+ if (strncmp(p, "fake", 4) == 0) {
+ return true;
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ return false;
+}
+
+/* The cached test verdicts, only when they cover every entry: a failed growth
+ * leaves the cache short, and then the scorer asks per candidate as before. */
+static const uint8_t *qn_test_flags(const qn_array_t *arr) {
+ return arr->is_test && arr->is_test_cap >= arr->count ? arr->is_test : NULL;
}
/* Score a candidate for tiebreaking. Higher = better.
* Layer 1: Non-test code preferred over test code (+1000)
- * Layer 2: Namespace proximity via common prefix length (+plen) */
-static int candidate_score(const char *candidate_qn, const char *module_qn) {
+ * Layer 2: Namespace proximity via common prefix length (+plen)
+ * `is_test` is the cached verdict when the caller has one (-1: ask). */
+static int candidate_score(const char *candidate_qn, const char *module_qn, int is_test) {
int score = 0;
- if (!is_test_qn(candidate_qn)) {
+ bool test = is_test >= 0 ? is_test != 0 : is_test_qn(candidate_qn);
+ if (!test) {
score += REG_TEST_PENALTY;
}
score += common_prefix_len(candidate_qn, module_qn);
return score;
}
-/* Pick candidate with highest composite score (test-deprioritization + namespace proximity). */
-static const char *best_by_import_distance(const char **candidates, int count,
- const char *module_qn) {
+/* Pick candidate with highest composite score (test-deprioritization + namespace
+ * proximity). `is_test_flags` is the candidates' cached test verdicts, or NULL
+ * when the caller has none (a filtered subset carries its own copy). */
+static const char *best_by_import_distance(const char **candidates, const uint8_t *is_test_flags,
+ int count, const char *module_qn) {
const char *best = NULL;
int best_score = CBM_NOT_FOUND;
for (int i = 0; i < count; i++) {
- int score = candidate_score(candidates[i], module_qn);
+ int score = candidate_score(candidates[i], module_qn,
+ is_test_flags ? (int)is_test_flags[i] : -1);
if (score > best_score) {
best_score = score;
best = candidates[i];
@@ -190,23 +260,30 @@ static const char *best_by_import_distance(const char **candidates, int count,
* file exit. Thread-local so each worker has its own cache without
* contention. */
static CBM_TLS CBMHashTable *_reach_cache = NULL;
+/* The cache's key copies live in one per-file arena, released with the cache:
+ * a strdup per memoized candidate was 12.5 M malloc + free pairs on the Go
+ * corpus (waste sanitizer, 2026-09-17). Keys are still copies -- the public
+ * cbm_registry_is_import_reachable may be handed a caller's buffer. */
+static CBM_TLS CBMArena _reach_keys;
+static CBM_TLS bool _reach_keys_live = false;
+/* The arena opens on the first memoized key, with a small first block that
+ * doubles: opened eagerly at the 64 KB default, the per-file arenas were 1.4 GB
+ * of cumulative allocation on the Go corpus, almost all of it never written
+ * (waste sanitizer, 2026-09-17). */
+enum { REACH_KEYS_FIRST_BLOCK = 4096 };
/* 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);
+ if (_reach_keys_live) {
+ cbm_arena_reset(&_reach_keys);
+ }
return;
}
if (estimated_capacity < 16)
@@ -217,9 +294,12 @@ void cbm_registry_reach_cache_begin(int 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;
+ if (_reach_keys_live) {
+ cbm_arena_destroy(&_reach_keys);
+ _reach_keys_live = false;
+ }
}
/* ── Per-file import-map prefix → module-QN hash ──────────────────
@@ -341,7 +421,11 @@ static bool is_import_reachable(const char *candidate_qn, const char **import_va
}
if (_reach_cache) {
- char *kdup = strdup(candidate_qn);
+ if (!_reach_keys_live) {
+ cbm_arena_init_sized(&_reach_keys, REACH_KEYS_FIRST_BLOCK);
+ _reach_keys_live = true;
+ }
+ char *kdup = cbm_arena_strdup(&_reach_keys, candidate_qn);
if (kdup) {
cbm_ht_set(_reach_cache, kdup, reachable ? REACH_CACHE_TRUE : REACH_CACHE_FALSE);
}
@@ -623,6 +707,7 @@ static void free_qn_array(const char *key, void *value, void *ud) {
qn_array_t *arr = value;
if (arr) {
/* items borrow the exact map's keys — freed there, not here */
+ cbm_free(CBM_MEM_CLASS_DYN_ARRAY, arr->is_test);
cbm_da_free(arr);
free(arr);
}
@@ -689,7 +774,23 @@ 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);
}
+ int before = arr->count;
cbm_da_push(arr, (char *)owned_qn);
+ if (arr->count == before) {
+ return; /* the name could not be recorded: no verdict to cache */
+ }
+ if (arr->count > arr->is_test_cap) {
+ int want = arr->cap > 0 ? arr->cap : arr->count;
+ uint8_t *grown =
+ cbm_realloc(CBM_MEM_CLASS_DYN_ARRAY, arr->is_test, (size_t)want * sizeof(uint8_t));
+ if (grown) {
+ arr->is_test = grown;
+ arr->is_test_cap = want;
+ }
+ }
+ if (arr->count <= arr->is_test_cap) {
+ arr->is_test[arr->count - SKIP_ONE] = is_test_qn(owned_qn) ? 1 : 0;
+ }
}
/* ── Lookup ──────────────────────────────────────────────────────── */
@@ -842,10 +943,16 @@ static cbm_resolution_t resolve_same_module(const cbm_registry_t *r, const char
static cbm_resolution_t resolve_multi_with_imports(const qn_array_t *arr, const char *module_qn,
const char **import_vals, int import_count) {
const char *filtered[CBM_SZ_256];
+ uint8_t filtered_test[CBM_SZ_256];
+ const uint8_t *flags = qn_test_flags(arr);
int fcount = 0;
for (int i = 0; i < arr->count && fcount < CBM_SZ_256; i++) {
if (is_import_reachable(arr->items[i], import_vals, import_count)) {
- filtered[fcount++] = arr->items[i];
+ if (flags) {
+ filtered_test[fcount] = flags[i];
+ }
+ filtered[fcount] = arr->items[i];
+ fcount++;
}
}
if (fcount == SKIP_ONE) {
@@ -853,14 +960,17 @@ static cbm_resolution_t resolve_multi_with_imports(const qn_array_t *arr, const
return (cbm_resolution_t){filtered[0], "suffix_match", conf, arr->count};
}
if (fcount > SKIP_ONE) {
- const char *best = best_by_import_distance(filtered, fcount, module_qn);
+ const char *best =
+ best_by_import_distance(filtered, flags ? filtered_test : NULL, fcount, module_qn);
if (best) {
double conf = candidate_count_penalty(CONF_SUFFIX_MATCH, fcount);
return (cbm_resolution_t){best, "suffix_match", conf, fcount};
}
}
/* No import-reachable — use all candidates with penalty */
- const char *best = best_by_import_distance((const char **)arr->items, arr->count, module_qn);
+ const char *best =
+ best_by_import_distance((const char **)arr->items, qn_test_flags(arr), arr->count,
+ module_qn);
if (best) {
double conf = candidate_count_penalty(CONF_SUFFIX_MATCH * REG_HALF_PENALTY, arr->count);
return (cbm_resolution_t){best, "suffix_match", conf, arr->count};
@@ -1046,7 +1156,9 @@ static cbm_resolution_t resolve_name_lookup(const cbm_registry_t *r, const char
if (import_vals && import_count > 0) {
return resolve_multi_with_imports(arr, module_qn, import_vals, import_count);
}
- const char *best = best_by_import_distance((const char **)arr->items, arr->count, module_qn);
+ const char *best =
+ best_by_import_distance((const char **)arr->items, qn_test_flags(arr), arr->count,
+ module_qn);
if (best) {
if (!receiver_chain_admits(callee_name, best)) {
return empty_result();
@@ -1227,7 +1339,8 @@ cbm_fuzzy_result_t cbm_registry_fuzzy_resolve(const cbm_registry_t *r, const cha
if (fcount == 0) {
/* No import-reachable — use originals with penalty */
const char *best =
- best_by_import_distance((const char **)arr->items, arr->count, module_qn);
+ best_by_import_distance((const char **)arr->items, qn_test_flags(arr), arr->count,
+ module_qn);
if (!best) {
return no_match;
}
@@ -1241,7 +1354,10 @@ cbm_fuzzy_result_t cbm_registry_fuzzy_resolve(const cbm_registry_t *r, const cha
{fptr[0], "fuzzy", candidate_count_penalty(CONF_FUZZY_SINGLE, arr->count), arr->count},
true};
}
- const char *best = best_by_import_distance(fptr, fcount, module_qn);
+ const char *best =
+ best_by_import_distance(fptr,
+ fptr == (const char **)arr->items ? qn_test_flags(arr) : NULL,
+ fcount, module_qn);
if (!best) {
return no_match;
}
diff --git a/src/pipeline/worker_pool.c b/src/pipeline/worker_pool.c
index ac0ab9552..908b514bb 100644
--- a/src/pipeline/worker_pool.c
+++ b/src/pipeline/worker_pool.c
@@ -8,6 +8,7 @@
* Each worker pulls indices from a shared atomic counter — zero
* contention, natural load balancing across heterogeneous cores.
*/
+#include "foundation/mem_events.h"
#include "pipeline/worker_pool.h"
#include "foundation/constants.h"
@@ -40,6 +41,33 @@ typedef struct {
int count;
} pthread_worker_arg_t;
+#if defined(CBM_MEMWASTE) && CBM_MEMWASTE
+/* Per-thread item counts, so the waste sanitizer can see an unbalanced pool:
+ * workers that sat idle while one thread did the work. */
+typedef struct {
+ pthread_worker_arg_t *wa;
+ int done;
+ uint64_t ops; /* events this worker produced: the work it actually did */
+} wp_counted_arg_t;
+
+static void *pthread_worker_counted(void *arg) {
+ wp_counted_arg_t *ca = arg;
+ pthread_worker_arg_t *wa = ca->wa;
+ uint64_t ops_before = cbm_memev_thread_ops();
+ while (WP_TRUE) {
+ int idx = atomic_fetch_add_explicit(wa->next_idx, WP_STEP, memory_order_relaxed);
+ if (idx >= wa->count) {
+ break;
+ }
+ wa->fn(idx, wa->ctx);
+ ca->done++;
+ }
+ ca->ops = cbm_memev_thread_ops() - ops_before;
+ cbm_mem_class_flush_thread();
+ return NULL;
+}
+#endif
+
static void *pthread_worker(void *arg) {
pthread_worker_arg_t *wa = arg;
while (WP_TRUE) {
@@ -55,6 +83,65 @@ static void *pthread_worker(void *arg) {
return NULL;
}
+#if defined(CBM_MEMWASTE) && CBM_MEMWASTE
+static void run_pthreads_counted(int count, cbm_parallel_fn fn, void *ctx, int nworkers,
+ void *site) {
+ _Atomic int next_idx = 0;
+ pthread_worker_arg_t wa = {.fn = fn, .ctx = ctx, .next_idx = &next_idx, .count = count};
+ cbm_thread_t *threads = (cbm_thread_t *)malloc((size_t)nworkers * sizeof(cbm_thread_t));
+ wp_counted_arg_t *args = (wp_counted_arg_t *)calloc((size_t)nworkers, sizeof(*args));
+ if (!threads || !args) {
+ free(threads);
+ free(args);
+ run_serial(count, fn, ctx);
+ return;
+ }
+ int started = 0;
+ for (int i = 0; i < nworkers; i++) {
+ args[i].wa = &wa;
+ if (cbm_thread_create(&threads[i], CBM_WORKER_STACK_SIZE, pthread_worker_counted,
+ &args[i]) != 0) {
+ break;
+ }
+ started++;
+ }
+ int main_done = 0;
+ uint64_t main_ops_before = cbm_memev_thread_ops();
+ while (WP_TRUE) {
+ int idx = atomic_fetch_add_explicit(&next_idx, WP_STEP, memory_order_relaxed);
+ if (idx >= count) {
+ break;
+ }
+ fn(idx, ctx);
+ main_done++;
+ }
+ uint64_t main_ops = cbm_memev_thread_ops() - main_ops_before;
+ for (int i = 0; i < started; i++) {
+ cbm_thread_join(&threads[i]);
+ }
+ int max_done = main_done;
+ uint64_t max_ops = main_ops;
+ uint64_t sum_ops = main_ops;
+ for (int i = 0; i < started; i++) {
+ if (args[i].done > max_done) {
+ max_done = args[i].done;
+ }
+ if (args[i].ops > max_ops) {
+ max_ops = args[i].ops;
+ }
+ sum_ops += args[i].ops;
+ }
+ int participants = started + 1;
+ uint64_t balanced_max = (uint64_t)participants * (uint64_t)max_done;
+ uint64_t imbalance = balanced_max > (uint64_t)count ? balanced_max - (uint64_t)count : 0;
+ cbm_work_note(CBM_WORK_PARALLEL_FOR, site, (uint64_t)count, imbalance, (uint64_t)participants);
+ cbm_work_note(CBM_WORK_POOL_OPS, site, sum_ops, (uint64_t)participants * max_ops - sum_ops,
+ (uint64_t)participants);
+ free(threads);
+ free(args);
+}
+#endif
+
static void run_pthreads(int count, cbm_parallel_fn fn, void *ctx, int nworkers) {
_Atomic int next_idx = 0;
@@ -117,5 +204,11 @@ void cbm_parallel_for(int count, cbm_parallel_fn fn, void *ctx, cbm_parallel_for
return;
}
+#if defined(CBM_MEMWASTE) && CBM_MEMWASTE
+ if (cbm_memev_enabled()) {
+ run_pthreads_counted(count, fn, ctx, nworkers, __builtin_return_address(0));
+ return;
+ }
+#endif
run_pthreads(count, fn, ctx, nworkers);
}
diff --git a/src/semantic/semantic.c b/src/semantic/semantic.c
index 4592f4874..2aa1df5a3 100644
--- a/src/semantic/semantic.c
+++ b/src/semantic/semantic.c
@@ -1491,6 +1491,33 @@ const cbm_sem_vec_t *cbm_sem_corpus_ri_vec(const cbm_sem_corpus_t *corpus, const
return &corpus->entries[idx].enriched_vec;
}
+int cbm_sem_corpus_token_index(const cbm_sem_corpus_t *corpus, const char *token) {
+ if (!corpus || !token) {
+ return CBM_NOT_FOUND;
+ }
+ int idx = parse_token_index(cbm_ht_get(corpus->token_map, token));
+ return (idx < 0 || idx >= corpus->entry_count) ? CBM_NOT_FOUND : idx;
+}
+
+/* Same arithmetic as cbm_sem_corpus_idf, by index. */
+float cbm_sem_corpus_idf_at(const cbm_sem_corpus_t *corpus, int index) {
+ if (!corpus || corpus->doc_count == 0 || index < 0 || index >= corpus->entry_count) {
+ return 0.0F;
+ }
+ int df = corpus->entries[index].doc_freq;
+ if (df <= 0) {
+ return 0.0F;
+ }
+ return logf((float)corpus->doc_count / (float)df);
+}
+
+const cbm_sem_vec_t *cbm_sem_corpus_ri_vec_at(const cbm_sem_corpus_t *corpus, int index) {
+ if (!corpus || index < 0 || index >= corpus->entry_count) {
+ return NULL;
+ }
+ return &corpus->entries[index].enriched_vec;
+}
+
int cbm_sem_corpus_doc_count(const cbm_sem_corpus_t *corpus) {
return corpus ? corpus->doc_count : 0;
}
diff --git a/src/semantic/semantic.h b/src/semantic/semantic.h
index 335c38f0e..1f99a54c0 100644
--- a/src/semantic/semantic.h
+++ b/src/semantic/semantic.h
@@ -184,6 +184,14 @@ float cbm_sem_corpus_idf(const cbm_sem_corpus_t *corpus, const char *token);
/* Get the enriched Random Indexing vector for a token (after co-occurrence). */
const cbm_sem_vec_t *cbm_sem_corpus_ri_vec(const cbm_sem_corpus_t *corpus, const char *token);
+/* The same two answers by token index, for a caller that asks both about many
+ * tokens: resolve each token once (cbm_sem_corpus_token_index, -1 when unknown)
+ * and read idf and vector by index. The name-keyed pair costs one hash lookup and
+ * one strtol per question -- three per token in the vector build. */
+int cbm_sem_corpus_token_index(const cbm_sem_corpus_t *corpus, const char *token);
+float cbm_sem_corpus_idf_at(const cbm_sem_corpus_t *corpus, int index);
+const cbm_sem_vec_t *cbm_sem_corpus_ri_vec_at(const cbm_sem_corpus_t *corpus, int index);
+
/* Get the total document count. */
int cbm_sem_corpus_doc_count(const cbm_sem_corpus_t *corpus);
diff --git a/src/simhash/minhash.c b/src/simhash/minhash.c
index 168a38e74..d8680c8cc 100644
--- a/src/simhash/minhash.c
+++ b/src/simhash/minhash.c
@@ -177,32 +177,45 @@ static int collect_ast_tokens(TSNode root, const char **tokens, int max_tokens)
/* Unique-trigram set: open addressing on 64-bit hashes. */
enum { UNIQ_SET_SIZE = 4096, UNIQ_SET_MASK = 4095 };
+/* The set is sized per function: at least twice its trigram count (so it can
+ * never fill), at most UNIQ_SET_SIZE as before. Zeroing all 4,096 slots for
+ * every function was a 32 KB memset per fingerprint. */
typedef struct {
uint64_t slots[UNIQ_SET_SIZE];
+ uint32_t mask;
int count;
} uniq_trig_set_t;
-static void uniq_trig_init(uniq_trig_set_t *s) {
- memset(s->slots, 0, sizeof(s->slots));
+enum { UNIQ_SET_MIN = 64 };
+
+static void uniq_trig_init(uniq_trig_set_t *s, int trigram_count) {
+ uint32_t size = UNIQ_SET_MIN;
+ while (size < UNIQ_SET_SIZE && (int64_t)size < (int64_t)trigram_count * PAIR_LEN) {
+ size <<= SKIP_ONE;
+ }
+ memset(s->slots, 0, (size_t)size * sizeof(s->slots[0]));
+ s->mask = size - SKIP_ONE;
s->count = 0;
}
-/* Insert a trigram hash. Returns true if newly inserted. */
-static bool uniq_trig_insert(uniq_trig_set_t *s, uint64_t trig_hash) {
+enum { UNIQ_PRESENT = 0, UNIQ_NEW = 1, UNIQ_FULL = 2 };
+
+/* Insert a trigram hash: UNIQ_NEW, UNIQ_PRESENT, or UNIQ_FULL (not counted). */
+static int uniq_trig_insert(uniq_trig_set_t *s, uint64_t trig_hash) {
uint64_t val = trig_hash | SKIP_ONE; /* ensure non-zero */
- uint32_t slot = (uint32_t)(trig_hash & UNIQ_SET_MASK);
- for (int probe = 0; probe < UNIQ_SET_SIZE; probe++) {
- uint32_t idx = (slot + (uint32_t)probe) & UNIQ_SET_MASK;
+ uint32_t slot = (uint32_t)(trig_hash & s->mask);
+ for (uint32_t probe = 0; probe <= s->mask; probe++) {
+ uint32_t idx = (slot + probe) & s->mask;
if (s->slots[idx] == 0) {
s->slots[idx] = val;
s->count++;
- return true;
+ return UNIQ_NEW;
}
if (s->slots[idx] == val) {
- return false;
+ return UNIQ_PRESENT;
}
}
- return false;
+ return UNIQ_FULL;
}
/* Apply weighted MinHash for one trigram: hash w times per seed. */
@@ -225,7 +238,7 @@ static int hash_trigrams(const char **tokens, int token_count, cbm_minhash_t *ou
}
uniq_trig_set_t uniq;
- uniq_trig_init(&uniq);
+ uniq_trig_init(&uniq, token_count > TRIGRAM_WINDOW ? token_count - TRIGRAM_WINDOW : 0);
char trigram_buf[TRIGRAM_BUF_LEN];
for (int i = 0; i + TRIGRAM_WINDOW < token_count; i++) {
@@ -241,8 +254,13 @@ static int hash_trigrams(const char **tokens, int token_count, cbm_minhash_t *ou
continue;
}
- uniq_trig_insert(&uniq, XXH3_64bits(trigram_buf, (size_t)len));
- weighted_minhash_update(out, trigram_buf, len, w);
+ /* A repeated trigram is the same string with the same weight: its
+ * update would lower the same minima to the same values. Only a first
+ * occurrence pays the 64 x w seeded hashes (identical fingerprints;
+ * the scaling lane had XXH3 at 15 % of all counted work). */
+ if (uniq_trig_insert(&uniq, XXH3_64bits(trigram_buf, (size_t)len)) != UNIQ_PRESENT) {
+ weighted_minhash_update(out, trigram_buf, len, w);
+ }
}
return uniq.count;
}
@@ -493,15 +511,52 @@ void cbm_lsh_query(const cbm_lsh_index_t *idx, const cbm_minhash_t *fp,
*count = mut_idx->result_count;
}
-int cbm_lsh_query_into(const cbm_lsh_index_t *idx, const cbm_minhash_t *fp,
- const cbm_lsh_entry_t **out_buf, int out_cap) {
- if (!idx || !fp || !out_buf || out_cap <= 0) {
- return 0;
+/* The reusable set: a slot belongs to the running query only when its stamp is
+ * the current generation. Same size, hash and probe order as seen_set_t, so the
+ * candidates found (and their order) are exactly those of a fresh table. */
+struct cbm_lsh_seen {
+ int64_t ids[SEEN_SET_SIZE];
+ uint32_t stamp[SEEN_SET_SIZE];
+ uint32_t current;
+};
+
+cbm_lsh_seen_t *cbm_lsh_seen_new(void) {
+ return cbm_calloc(CBM_MEM_CLASS_SEMANTIC, sizeof(cbm_lsh_seen_t));
+}
+
+void cbm_lsh_seen_free(cbm_lsh_seen_t *seen) {
+ cbm_free(CBM_MEM_CLASS_SEMANTIC, seen);
+}
+
+static void lsh_seen_begin(cbm_lsh_seen_t *s) {
+ if (++s->current == 0) { /* generation wrapped: every old stamp must go */
+ memset(s->stamp, 0, sizeof(s->stamp));
+ s->current = 1;
}
+}
- /* Thread-local dedup — no shared state touched. */
- seen_set_t seen;
- seen_set_init(&seen);
+static bool lsh_seen_insert(cbm_lsh_seen_t *s, int64_t node_id) {
+ uint32_t idx = (uint32_t)(node_id * KNUTH_MULT) & SEEN_SET_MASK;
+ for (int probe = 0; probe < SEEN_SET_SIZE; probe++) {
+ uint32_t slot = (idx + (uint32_t)probe) & SEEN_SET_MASK;
+ if (s->stamp[slot] != s->current) {
+ s->stamp[slot] = s->current;
+ s->ids[slot] = node_id;
+ return true; /* inserted (was not present) */
+ }
+ if (s->ids[slot] == node_id) {
+ return false; /* already present */
+ }
+ }
+ return false; /* table full */
+}
+
+int cbm_lsh_query_into_seen(const cbm_lsh_index_t *idx, const cbm_minhash_t *fp,
+ const cbm_lsh_entry_t **out_buf, int out_cap, cbm_lsh_seen_t *seen) {
+ if (!idx || !fp || !out_buf || out_cap <= 0 || !seen) {
+ return 0;
+ }
+ lsh_seen_begin(seen);
int count = 0;
for (int b = 0; b < CBM_LSH_BANDS; b++) {
@@ -512,7 +567,7 @@ int cbm_lsh_query_into(const cbm_lsh_index_t *idx, const cbm_minhash_t *fp,
}
for (int i = 0; i < bucket->count && count < out_cap; i++) {
const cbm_lsh_entry_t *candidate = &idx->entries[bucket->items[i]];
- if (!seen_set_insert(&seen, candidate->node_id)) {
+ if (!lsh_seen_insert(seen, candidate->node_id)) {
continue;
}
out_buf[count++] = candidate;
@@ -521,8 +576,20 @@ int cbm_lsh_query_into(const cbm_lsh_index_t *idx, const cbm_minhash_t *fp,
break;
}
}
+ return count;
+}
- seen_set_free(&seen);
+int cbm_lsh_query_into(const cbm_lsh_index_t *idx, const cbm_minhash_t *fp,
+ const cbm_lsh_entry_t **out_buf, int out_cap) {
+ if (!idx || !fp || !out_buf || out_cap <= 0) {
+ return 0;
+ }
+ cbm_lsh_seen_t *seen = cbm_lsh_seen_new();
+ if (!seen) {
+ return 0;
+ }
+ int count = cbm_lsh_query_into_seen(idx, fp, out_buf, out_cap, seen);
+ cbm_lsh_seen_free(seen);
return count;
}
diff --git a/src/simhash/minhash.h b/src/simhash/minhash.h
index cd7a7151a..8546debea 100644
--- a/src/simhash/minhash.h
+++ b/src/simhash/minhash.h
@@ -114,6 +114,18 @@ void cbm_lsh_query(const cbm_lsh_index_t *idx, const cbm_minhash_t *fp,
int cbm_lsh_query_into(const cbm_lsh_index_t *idx, const cbm_minhash_t *fp,
const cbm_lsh_entry_t **out_buf, int out_cap);
+/* A dedup set one thread reuses across many queries. Starting a query costs a
+ * generation increment, not a fresh 131 KB zeroed table: the similarity pass
+ * allocated one per query, 5.3 GB of churn over 42 k queries on the Go corpus
+ * (waste sanitizer, 2026-09-17). Not shareable between threads. */
+typedef struct cbm_lsh_seen cbm_lsh_seen_t;
+cbm_lsh_seen_t *cbm_lsh_seen_new(void);
+void cbm_lsh_seen_free(cbm_lsh_seen_t *seen);
+
+/* cbm_lsh_query_into with a caller-owned dedup set: same candidates, same order. */
+int cbm_lsh_query_into_seen(const cbm_lsh_index_t *idx, const cbm_minhash_t *fp,
+ const cbm_lsh_entry_t **out_buf, int out_cap, cbm_lsh_seen_t *seen);
+
/* Free the LSH index and all internal storage. */
void cbm_lsh_free(cbm_lsh_index_t *idx);
diff --git a/tests/test_arena.c b/tests/test_arena.c
index 20021a4be..eb91fa4ce 100644
--- a/tests/test_arena.c
+++ b/tests/test_arena.c
@@ -5,12 +5,12 @@
#include "../src/foundation/arena.h"
#include <stdint.h>
-TEST(arena_init_exact_one_block_then_default_growth) {
+TEST(arena_init_exact_one_block_then_append_block_growth) {
CBMArena a;
cbm_arena_init_exact(&a, 100);
ASSERT_EQ(a.nblocks, 1);
ASSERT_EQ(a.block_sizes[0], 104); /* rounded to the 8-byte allocation grain */
- ASSERT_EQ(a.grow_size, CBM_ARENA_DEFAULT_BLOCK_SIZE);
+ ASSERT_EQ(a.grow_size, CBM_ARENA_APPEND_BLOCK);
void *p = cbm_arena_alloc(&a, 100);
ASSERT_NOT_NULL(p);
ASSERT_EQ(a.used, 104);
@@ -18,8 +18,10 @@ TEST(arena_init_exact_one_block_then_default_growth) {
void *q = cbm_arena_alloc(&a, 8);
ASSERT_NOT_NULL(q);
ASSERT_EQ(a.nblocks, 2);
- ASSERT_EQ(a.block_sizes[1], CBM_ARENA_DEFAULT_BLOCK_SIZE); /* not 2 x 104 */
- ASSERT_EQ(a.grow_size, 2 * CBM_ARENA_DEFAULT_BLOCK_SIZE);
+ /* An append takes a small block, not 2 x 104 and not the 64 KB default:
+ * the appended-to result arenas are the worker's peak. */
+ ASSERT_EQ(a.block_sizes[1], CBM_ARENA_APPEND_BLOCK);
+ ASSERT_EQ(a.grow_size, 2 * CBM_ARENA_APPEND_BLOCK);
cbm_arena_destroy(&a);
PASS();
}
@@ -466,8 +468,44 @@ TEST(arena_strndup_zero_len) {
PASS();
}
+/* A lazy arena holds nothing until its first allocation, which opens exactly
+ * the configured block; growth then doubles as usual, and destroying it closes
+ * it again (an allocation after destroy is refused, never a silent reopen). */
+TEST(arena_lazy_opens_on_first_alloc_only) {
+ CBMArena a;
+ cbm_arena_init_lazy(&a, 4096);
+ ASSERT_EQ(a.nblocks, 0);
+ ASSERT_EQ(cbm_arena_capacity(&a), 0);
+ cbm_arena_rewind(&a); /* safe on a lazy arena that never opened */
+
+ void *p = cbm_arena_alloc(&a, 100);
+ ASSERT_NOT_NULL(p);
+ ASSERT_EQ(a.nblocks, 1);
+ ASSERT_EQ(a.block_sizes[0], 4096);
+ ASSERT_EQ(a.used, 104);
+ ASSERT_EQ(a.waste_grows, 0); /* the opening block is not a growth */
+
+ void *q = cbm_arena_alloc(&a, 4000); /* does not fit the rest: one doubled block */
+ ASSERT_NOT_NULL(q);
+ ASSERT_EQ(a.nblocks, 2);
+ ASSERT_EQ(a.block_sizes[1], 8192);
+
+ cbm_arena_destroy(&a);
+ ASSERT_EQ(a.nblocks, 0);
+ ASSERT_NULL(cbm_arena_alloc(&a, 16));
+
+ /* An opening allocation larger than the configured block gets a block that fits. */
+ cbm_arena_init_lazy(&a, 4096);
+ void *big = cbm_arena_alloc(&a, 10000);
+ ASSERT_NOT_NULL(big);
+ ASSERT_EQ(a.nblocks, 1);
+ ASSERT_GTE(a.block_sizes[0], 10000);
+ cbm_arena_destroy(&a);
+ PASS();
+}
+
SUITE(arena) {
- RUN_TEST(arena_init_exact_one_block_then_default_growth);
+ RUN_TEST(arena_init_exact_one_block_then_append_block_growth);
RUN_TEST(arena_growth_doubles_grow_size_and_reset_restores_it);
RUN_TEST(arena_init_default);
RUN_TEST(arena_init_sized);
@@ -501,4 +539,5 @@ SUITE(arena) {
RUN_TEST(arena_total_through_reset);
RUN_TEST(arena_reset_block_size_invariant);
RUN_TEST(arena_strndup_zero_len);
+ RUN_TEST(arena_lazy_opens_on_first_alloc_only);
}
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index 14d75bf8b..a47466343 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -7291,11 +7291,15 @@ TEST(extract_compact_keeps_every_field_and_shrinks_the_arena) {
ASSERT(shared);
/* The arena stays usable for the cross-file pass: growth restarts at the
- * default block, never at twice the compact block. */
+ * small append block, never at twice the compact block. (It restarted at
+ * the 64 KB default until 2026-09-17: the cross-file pass appends a few
+ * resolved calls, so that block was ~60 KB of untouched memory per
+ * appended-to result — 0.5 GB of the worker's peak on the Go corpus.
+ * See CBM_ARENA_APPEND_BLOCK.) */
char *later = cbm_arena_strdup(&r->arena, "appended after compaction");
ASSERT_NOT_NULL(later);
ASSERT_EQ(r->arena.nblocks, 2);
- ASSERT_EQ(r->arena.block_sizes[1], CBM_ARENA_DEFAULT_BLOCK_SIZE);
+ ASSERT_EQ(r->arena.block_sizes[1], CBM_ARENA_APPEND_BLOCK);
cbm_free_result(r);
cbm_free_result(ref);
diff --git a/tests/test_hash_table.c b/tests/test_hash_table.c
index cd50bcc77..58bbcd8c9 100644
--- a/tests/test_hash_table.c
+++ b/tests/test_hash_table.c
@@ -4,6 +4,8 @@
#include "test_framework.h"
#include "../src/foundation/hash_table.h"
+#include <string.h>
+
TEST(ht_create_free) {
CBMHashTable *ht = cbm_ht_create(16);
ASSERT_NOT_NULL(ht);
@@ -37,6 +39,29 @@ TEST(ht_set_overwrite) {
PASS();
}
+/* Keys are BORROWED. Overwriting an existing key must adopt the new key pointer
+ * too, because the caller may free the old string right after: cbm_ht_set does
+ * one probe now (get_or_insert) instead of get-then-insert, and the replace of
+ * both key and value is what _insert used to do implicitly. */
+TEST(ht_set_overwrite_adopts_the_new_key_pointer) {
+ CBMHashTable *ht = cbm_ht_create(8);
+ char first[8];
+ char second[8];
+ memcpy(first, "shared", 7);
+ memcpy(second, "shared", 7);
+ int v1 = 1;
+ int v2 = 2;
+ ASSERT_NULL(cbm_ht_set(ht, first, &v1));
+ ASSERT_EQ(cbm_ht_get_key(ht, "shared"), first);
+ ASSERT_EQ(cbm_ht_set(ht, second, &v2), &v1);
+ ASSERT_EQ(cbm_ht_get_key(ht, "shared"), second); /* the old buffer is no longer referenced */
+ memcpy(first, "gone!!", 7); /* the caller reuses the old buffer */
+ ASSERT_EQ(*(int *)cbm_ht_get(ht, "shared"), 2);
+ ASSERT_EQ(cbm_ht_count(ht), 1);
+ cbm_ht_free(ht);
+ PASS();
+}
+
TEST(ht_get_missing) {
CBMHashTable *ht = cbm_ht_create(8);
ASSERT_NULL(cbm_ht_get(ht, "nope"));
@@ -367,6 +392,7 @@ SUITE(hash_table) {
RUN_TEST(ht_create_free);
RUN_TEST(ht_set_get);
RUN_TEST(ht_set_overwrite);
+ RUN_TEST(ht_set_overwrite_adopts_the_new_key_pointer);
RUN_TEST(ht_get_missing);
RUN_TEST(ht_has);
RUN_TEST(ht_delete);
diff --git a/tests/test_main.c b/tests/test_main.c
index e25de3026..850dfbd75 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -23,7 +23,7 @@ int tf_skip_count = 0;
#include "daemon/version_cohort.h" /* Windows crash-turnover re-exec probe */
#include "mcp/index_supervisor.h" /* cbm_index_set_worker_role */
#include "mcp/mcp.h" /* cbm_mcp_handle_tool — act as a real worker */
-#include "ui/http_server.h" /* deleted-self executable probe */
+#include "ui/http_server.h" /* deleted-self executable probe */
#include <sqlite3.h>
#include <errno.h>
#include <stdbool.h>
@@ -880,6 +880,7 @@ extern void suite_repro_harness_cleanup(void);
extern void suite_repro_runner_filter(void);
extern void suite_call_reference_contract(void);
extern void suite_mem(void);
+extern void suite_mem_events(void);
extern void suite_ui(void);
extern void suite_httpd(void);
extern void suite_security(void);
@@ -1204,6 +1205,7 @@ int main(int argc, char **argv) {
/* mem + arena + slab integration */
RUN_SELECTED_SUITE(slab_alloc);
RUN_SELECTED_SUITE(mem);
+ RUN_SELECTED_SUITE(mem_events);
/* UI (config, external asset pack, layout) */
RUN_SELECTED_SUITE(ui);
diff --git a/tests/test_mem.c b/tests/test_mem.c
index a943282e4..a2643a963 100644
--- a/tests/test_mem.c
+++ b/tests/test_mem.c
@@ -1338,6 +1338,130 @@ TEST(extract_traversal_stacks_come_from_ctx_scratch_issue2010) {
PASS();
}
+static TSTree *parse_for_test(TSParser *parser, CBMLanguage lang, const char *src) {
+ ts_parser_set_language(parser, cbm_ts_language(lang));
+ return ts_parser_parse_string(parser, NULL, src, (uint32_t)strlen(src));
+}
+
+/* The field-id cache behind ts_node_child_by_field_name (cbm.h) must answer
+ * exactly what tree-sitter answers, for every node and name -- including names
+ * that are not fields, a name handed in through a REUSED buffer, and the same
+ * name in another grammar. The real function is reached with parentheses, which
+ * suppress the macro. */
+TEST(field_id_cache_answers_exactly_what_tree_sitter_answers) {
+ static const char *const names[] = {"name", "body", "type", "parameters",
+ "result", "receiver", "value", "not_a_field"};
+ const char *go_src = "package p\n"
+ "type T struct { A int }\n"
+ "func (t *T) Area(x int) (int, error) { return x, nil }\n"
+ "func F() { var v = T{}; _ = v }\n";
+ TSParser *parser = ts_parser_new();
+ ASSERT_NOT_NULL(parser);
+ TSTree *tree = parse_for_test(parser, CBM_LANG_GO, go_src);
+ ASSERT_NOT_NULL(tree);
+
+ int compared = 0;
+ TSTreeCursor walk = ts_tree_cursor_new(ts_tree_root_node(tree));
+ bool more = true;
+ while (more) {
+ TSNode node = ts_tree_cursor_current_node(&walk);
+ for (size_t n = 0; n < sizeof(names) / sizeof(names[0]); n++) {
+ uint32_t len = (uint32_t)strlen(names[n]);
+ TSNode cached = cbm_ts_child_by_field_name(node, names[n], len);
+ TSNode real = (ts_node_child_by_field_name)(node, names[n], len);
+ ASSERT_TRUE(ts_node_eq(cached, real));
+ compared++;
+ }
+ if (ts_tree_cursor_goto_first_child(&walk)) {
+ continue;
+ }
+ while (!ts_tree_cursor_goto_next_sibling(&walk)) {
+ if (!ts_tree_cursor_goto_parent(&walk)) {
+ more = false;
+ break;
+ }
+ }
+ }
+ ts_tree_cursor_delete(&walk);
+ ASSERT_GT(compared, 100);
+
+ /* A reused buffer: same pointer, different name. */
+ TSNode root = ts_tree_root_node(tree);
+ TSNode method = ts_node_named_child(root, 2);
+ char buf[16];
+ memcpy(buf, "name", 5);
+ TSNode by_name = cbm_ts_child_by_field_name(method, buf, 4);
+ memcpy(buf, "body", 5);
+ TSNode by_body = cbm_ts_child_by_field_name(method, buf, 4);
+ ASSERT_TRUE(ts_node_eq(by_body, (ts_node_child_by_field_name)(method, "body", 4)));
+ ASSERT_FALSE(ts_node_eq(by_name, by_body));
+
+ /* The same name in another grammar resolves against that grammar. */
+ TSTree *ts_tree = parse_for_test(parser, CBM_LANG_TYPESCRIPT, "function g(a) { return a; }\n");
+ ASSERT_NOT_NULL(ts_tree);
+ TSNode fn = ts_node_named_child(ts_tree_root_node(ts_tree), 0);
+ ASSERT_TRUE(ts_node_eq(cbm_ts_child_by_field_name(fn, "name", 4),
+ (ts_node_child_by_field_name)(fn, "name", 4)));
+ ASSERT_FALSE(ts_node_is_null(cbm_ts_child_by_field_name(fn, "name", 4)));
+
+ ts_tree_delete(ts_tree);
+ ts_tree_delete(tree);
+ ts_parser_delete(parser);
+ PASS();
+}
+
+/* Recursive walkers take one pooled cursor per depth (cbm_cursor_acquire). A
+ * depth whose slot is still held -- another walker nested on this thread --
+ * must get a private cursor, never the busy one; and a pooled cursor walks the
+ * children exactly like a fresh one. */
+TEST(cursor_pool_hands_each_depth_its_own_cursor) {
+ TSParser *parser = ts_parser_new();
+ ASSERT_NOT_NULL(parser);
+ TSTree *tree = parse_for_test(parser, CBM_LANG_GO,
+ "package p\nfunc A() {}\nfunc B() {}\nvar C = 1\n");
+ ASSERT_NOT_NULL(tree);
+ TSNode root = ts_tree_root_node(tree);
+
+ cbm_cursor_lease_t d0;
+ cbm_cursor_lease_t d1;
+ cbm_cursor_lease_t nested;
+ TSTreeCursor *c0 = cbm_cursor_acquire(&d0, 0, root);
+ TSTreeCursor *c1 = cbm_cursor_acquire(&d1, 1, ts_node_named_child(root, 0));
+ TSTreeCursor *cn = cbm_cursor_acquire(&nested, 0, root); /* depth 0 is busy */
+ ASSERT_TRUE(d0.slot == 0);
+ ASSERT_TRUE(d1.slot == 1);
+ ASSERT_TRUE(nested.slot == -1);
+ ASSERT_TRUE(c0 != c1 && cn != c0 && cn != c1);
+
+ /* The pooled cursor walks the same children as a fresh one. */
+ TSTreeCursor fresh = ts_tree_cursor_new(root);
+ bool pooled_ok = ts_tree_cursor_goto_first_child(c0);
+ bool fresh_ok = ts_tree_cursor_goto_first_child(&fresh);
+ int siblings = 0;
+ while (pooled_ok && fresh_ok) {
+ ASSERT_TRUE(ts_node_eq(ts_tree_cursor_current_node(c0), ts_tree_cursor_current_node(&fresh)));
+ siblings++;
+ pooled_ok = ts_tree_cursor_goto_next_sibling(c0);
+ fresh_ok = ts_tree_cursor_goto_next_sibling(&fresh);
+ }
+ ASSERT_FALSE(pooled_ok || fresh_ok);
+ ASSERT_GT(siblings, 2);
+ ts_tree_cursor_delete(&fresh);
+
+ cbm_cursor_release(&nested);
+ cbm_cursor_release(&d1);
+ cbm_cursor_release(&d0);
+ /* Released: the same depth hands out its pooled cursor again. */
+ cbm_cursor_lease_t again;
+ ASSERT_TRUE(cbm_cursor_acquire(&again, 0, root) == c0);
+ cbm_cursor_release(&again);
+
+ cbm_destroy_thread_parser(); /* releases the pool */
+ ts_tree_delete(tree);
+ ts_parser_delete(parser);
+ PASS();
+}
+
/* ── mem_core: the central allocation route ────────────────────────────
*
* Every assertion below is a DELTA, never an absolute. Other code in this
@@ -1635,6 +1759,8 @@ SUITE(mem) {
/* extraction scratch arena (#2010) */
RUN_TEST(extract_traversal_stacks_come_from_ctx_scratch_issue2010);
+ RUN_TEST(field_id_cache_answers_exactly_what_tree_sitter_answers);
+ RUN_TEST(cursor_pool_hands_each_depth_its_own_cursor);
RUN_TEST(mem_core_accounts_alloc_and_free);
RUN_TEST(mem_core_classes_do_not_bleed);
RUN_TEST(mem_core_realloc_replaces_the_old_charge);
diff --git a/tests/test_registry.c b/tests/test_registry.c
index 247ca61ad..8d9ecaaf9 100644
--- a/tests/test_registry.c
+++ b/tests/test_registry.c
@@ -578,6 +578,44 @@ TEST(resolve_import_reachable_prefix) {
PASS();
}
+/* The per-file reachability memo answers exactly what the uncached check
+ * answers, file after file: its key arena opens on the first memoized key and
+ * is destroyed with the file, and one key is longer than the arena's first
+ * block. The two files import different packages, so a memo leaking between
+ * files would flip an answer. */
+TEST(reach_cache_memo_matches_uncached_across_files) {
+ static char long_qn[6000];
+ memset(long_qn, 'a', sizeof(long_qn) - 1);
+ long_qn[sizeof(long_qn) - 1] = '\0';
+ memcpy(long_qn, "pkg.io.", 7);
+ long_qn[5000] = '.';
+ const char *cands[] = {"pkg.io.Reader", "pkg.net.Dial", "other.fmt.Println", long_qn};
+ enum { NCANDS = 4 };
+ const char *imports_io[] = {"pkg.io"};
+ const char *imports_net[] = {"pkg.net", "other.fmt"};
+ for (int file = 0; file < 4; file++) {
+ bool io = (file % 2) == 0;
+ const char **imports = io ? imports_io : imports_net;
+ int nimports = io ? 1 : 2;
+ bool expect[NCANDS];
+ for (int i = 0; i < NCANDS; i++) {
+ expect[i] = cbm_registry_is_import_reachable(cands[i], imports, nimports);
+ }
+ /* the uncached answers really differ between the files */
+ ASSERT_EQ(expect[0], io);
+ ASSERT_EQ(expect[1], !io);
+ ASSERT_EQ(expect[3], io);
+ cbm_registry_reach_cache_begin(8);
+ for (int pass = 0; pass < 2; pass++) { /* the second pass is served by the memo */
+ for (int i = 0; i < NCANDS; i++) {
+ ASSERT_EQ(cbm_registry_is_import_reachable(cands[i], imports, nimports), expect[i]);
+ }
+ }
+ cbm_registry_reach_cache_end();
+ }
+ PASS();
+}
+
/* ── Negative import evidence ─────────────────────────────────── */
TEST(negative_import_rejects_unimported) {
@@ -1046,6 +1084,7 @@ SUITE(registry) {
/* Import reachability */
RUN_TEST(resolve_is_import_reachable);
RUN_TEST(resolve_import_reachable_prefix);
+ RUN_TEST(reach_cache_memo_matches_uncached_across_files);
/* Negative import evidence */
RUN_TEST(negative_import_rejects_unimported);
/* Fuzzy resolve */
diff --git a/tests/test_semantic.c b/tests/test_semantic.c
index d1bf7b54b..e56c02c23 100644
--- a/tests/test_semantic.c
+++ b/tests/test_semantic.c
@@ -328,6 +328,33 @@ TEST(sem_corpus_idf) {
PASS();
}
+/* The vector build resolves each token once and reads idf and vector by index
+ * (pass_semantic_edges.c): the index accessors must give the same answers as
+ * the name-keyed ones, before and after finalize, and agree on unknown tokens. */
+TEST(sem_corpus_index_accessors_match_name_lookups) {
+ cbm_sem_corpus_t *c = cbm_sem_corpus_new();
+ ASSERT_NOT_NULL(c);
+ const char *doc1[] = {"alpha", "beta", "gamma"};
+ const char *doc2[] = {"alpha", "delta", "beta"};
+ const char *doc3[] = {"epsilon", "alpha"};
+ cbm_sem_corpus_add_doc(c, doc1, 3);
+ cbm_sem_corpus_add_doc(c, doc2, 3);
+ cbm_sem_corpus_add_doc(c, doc3, 2);
+ static const char *const probe[] = {"alpha", "beta", "gamma", "delta", "epsilon", "absent"};
+ for (int round = 0; round < 2; round++) {
+ for (size_t i = 0; i < sizeof(probe) / sizeof(probe[0]); i++) {
+ int idx = cbm_sem_corpus_token_index(c, probe[i]);
+ ASSERT_FLOAT_EQ(cbm_sem_corpus_idf_at(c, idx), cbm_sem_corpus_idf(c, probe[i]), 0.0);
+ ASSERT_TRUE(cbm_sem_corpus_ri_vec_at(c, idx) == cbm_sem_corpus_ri_vec(c, probe[i]));
+ }
+ ASSERT_EQ(cbm_sem_corpus_token_index(c, "absent"), -1);
+ ASSERT_NULL(cbm_sem_corpus_ri_vec_at(c, -1));
+ cbm_sem_corpus_finalize(c); /* second round: the finalized corpus */
+ }
+ cbm_sem_corpus_free(c);
+ PASS();
+}
+
TEST(sem_corpus_add_null_doc) {
cbm_sem_corpus_t *c = cbm_sem_corpus_new();
ASSERT_NOT_NULL(c);
@@ -503,6 +530,7 @@ SUITE(semantic) {
RUN_TEST(sem_corpus_new_free);
RUN_TEST(sem_corpus_add_one_doc);
RUN_TEST(sem_corpus_idf);
+ RUN_TEST(sem_corpus_index_accessors_match_name_lookups);
RUN_TEST(sem_corpus_add_null_doc);
RUN_TEST(sem_corpus_free_null);
RUN_TEST(sem_get_config_defaults);
diff --git a/tests/test_simhash.c b/tests/test_simhash.c
index 9f17576bf..9c114c431 100644
--- a/tests/test_simhash.c
+++ b/tests/test_simhash.c
@@ -1178,6 +1178,48 @@ TEST(pipeline_minhash_incremental_new_clone) {
* Suite Registration
* ═══════════════════════════════════════════════════════════════════ */
+/* The reused dedup set answers exactly what a fresh one does, query after
+ * query: same candidates, same order, no leakage between queries. */
+TEST(lsh_reused_seen_set_matches_fresh_query) {
+ enum { ENTRY_COUNT = 40, QUERIES = 6 };
+ static cbm_minhash_t fps[ENTRY_COUNT];
+ static cbm_lsh_entry_t entries[ENTRY_COUNT];
+ cbm_lsh_index_t *idx = cbm_lsh_new();
+ for (int e = 0; e < ENTRY_COUNT; e++) {
+ for (int i = 0; i < CBM_MINHASH_K; i++) {
+ /* Neighbouring entries share most values, so buckets collide and
+ * the same candidate turns up through several bands. */
+ fps[e].values[i] = (uint32_t)((i * 7 + 13) + (i % 4 == 0 ? e / 3 : 0));
+ }
+ entries[e].node_id = e + 1;
+ entries[e].fingerprint = &fps[e];
+ entries[e].file_path = "a.go";
+ entries[e].file_ext = ".go";
+ entries[e].qualified_name = "pkg.fn";
+ cbm_lsh_insert(idx, &entries[e]);
+ }
+
+ cbm_lsh_seen_t *seen = cbm_lsh_seen_new();
+ ASSERT_NOT_NULL(seen);
+ enum { CAP = 64 };
+ const cbm_lsh_entry_t *fresh_buf[CAP];
+ const cbm_lsh_entry_t *reused_buf[CAP];
+ int total = 0;
+ for (int q = 0; q < QUERIES; q++) {
+ int fresh = cbm_lsh_query_into(idx, &fps[q * 3], fresh_buf, CAP);
+ int reused = cbm_lsh_query_into_seen(idx, &fps[q * 3], reused_buf, CAP, seen);
+ ASSERT_EQ(reused, fresh);
+ for (int i = 0; i < fresh; i++) {
+ ASSERT_EQ(reused_buf[i]->node_id, fresh_buf[i]->node_id);
+ }
+ total += fresh;
+ }
+ ASSERT_GT(total, 0); /* the queries really returned candidates */
+ cbm_lsh_seen_free(seen);
+ cbm_lsh_free(idx);
+ PASS();
+}
+
SUITE(simhash) {
/* Suite 1: MinHash Core */
RUN_TEST(minhash_identical_source_same_fingerprint);
@@ -1194,6 +1236,7 @@ SUITE(simhash) {
RUN_TEST(minhash_hex_roundtrip);
RUN_TEST(lsh_same_bucket_similar);
RUN_TEST(lsh_different_bucket_dissimilar);
+ RUN_TEST(lsh_reused_seen_set_matches_fresh_query);
RUN_TEST(lsh_index_build_and_query);
/* Suite 3: Edge Generation */