Files
codebase-memory-mcp/src/foundation/hash_table.h
Martin Vogel 2ee0c68d4a perf(foundation): swap CBMHashTable internals to Verstable v2.2.1
Replaces the prior Robin-Hood open-addressing implementation with
Verstable (https://github.com/JacksonAllan/Verstable), a 2024 state-
of-the-art C hash table. The public API in foundation/hash_table.h
is UNCHANGED — CBMHashTable is now an opaque type and all 14
production callers (graph_buffer, registry, pipeline passes,
service_patterns, watcher, semantic, etc.) get the new impl
transparently. str_intern.c keeps its specialised (ptr+len) hash —
different semantics, not a clean swap target.

Why: per-sub-phase profiling pinpointed cbm_pipeline_lsp_target_node
and cbm_registry_resolve as the dominant resolve_calls cost. Both
ultimately hit cbm_ht_get; both run millions of times on a kubernetes
index. Verstable's key innovations target exactly this:

  * 4-bit hash-fragment metadata in a uint16_t sidecar array lets
    chain walks skip most key comparisons WITHOUT touching the
    bucket payload (cache wins on hot lookups).
  * Quadratic probing with per-bucket displacement chains (not linear
    Robin-Hood psl).
  * Tombstone-free deletion that moves at most one existing key.

Vendored at internal/cbm/vendored/verstable/verstable.h (2069 lines,
84KB, MIT). Wrapped as a const char* → void* template inside
hash_table.c. The header struct is opaque (forward declaration only);
audited the entire tree — no caller accesses internals or takes
sizeof(CBMHashTable), so opacity is safe.

Test refactor: test_hash_table.c had two impl-specific tests:
  * ht_robin_hood_bounded_psl — Robin-Hood specific; removed.
    ht_stress_10000 covers the functional equivalent.
  * ht_power_of_two_capacity — renamed ht_create_small_capacity,
    replaced struct probes with a functional insert+lookup check.
ht_create_capacity_zero/one similarly de-internalised.

Benchmark on kubernetes (12 workers, 20,650 files):
- parallel_resolve wall: 36,376 -> 27,217 ms (-25.2%)
- pipeline.done total:   61,485 -> 51,745 ms (-15.8%)
- registry_build:        ~1,000 -> 779 ms (faster — registry uses
                         cbm_ht_set heavily during build)
- All 3,615 tests pass; hash_table suite 100% green
- ASan/UBSan/SEGV: none
- Peak RSS: ~6.83 GB
- Graph: nodes 297,082 -> 297,102 (+20, 0.007%), edges 1,155,444 ->
  1,154,025 (-1,419, -0.12%) — within tolerance; minor delta likely
  from hash-iteration-order differences affecting tie-breaks in
  resolution. Sanity (zod/serilog): nodes exact, edges within 0.1%.

Cumulative trajectory from broken-state baseline ~35 min on K8s ->
51.7s wall = ~40x total speedup.
2026-05-21 22:52:16 +02:00

58 lines
2.1 KiB
C

/*
* hash_table.h — String → void* hash table.
*
* Public API unchanged across implementation rewrites. As of v2 the
* internals are Verstable (https://github.com/JacksonAllan/Verstable),
* a 2024 state-of-the-art open-addressing hash table with quadratic
* probing + per-bucket 4-bit hash fragments. The struct is opaque —
* callers MUST go through cbm_ht_create() and the API functions.
*
* Keys are borrowed pointers — the table does not copy or free them.
* Callers own the key strings for the lifetime of the entry.
*/
#ifndef CBM_HASH_TABLE_H
#define CBM_HASH_TABLE_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
/* Opaque — full definition lives in hash_table.c. */
typedef struct CBMHashTable CBMHashTable;
/* Create a hash table with initial capacity hint (used to pre-reserve
* buckets and avoid early growth; 0 = library default). */
CBMHashTable *cbm_ht_create(uint32_t initial_capacity);
/* Free the hash table (does NOT free keys or values). */
void cbm_ht_free(CBMHashTable *ht);
/* Insert or update. Returns previous value (NULL if new key). */
void *cbm_ht_set(CBMHashTable *ht, const char *key, void *value);
/* Lookup. Returns NULL if not found. */
void *cbm_ht_get(const CBMHashTable *ht, const char *key);
/* Check if key exists. */
bool cbm_ht_has(const CBMHashTable *ht, const char *key);
/* Return the stored key pointer for a given lookup key, or NULL.
* Useful when you need the canonical (heap-owned) key string
* rather than your own local copy. */
const char *cbm_ht_get_key(const CBMHashTable *ht, const char *key);
/* Delete. Returns removed value (NULL if not found). */
void *cbm_ht_delete(CBMHashTable *ht, const char *key);
/* Number of entries. */
uint32_t cbm_ht_count(const CBMHashTable *ht);
/* Iteration: call fn(key, value, userdata) for each entry. */
typedef void (*cbm_ht_iter_fn)(const char *key, void *value, void *userdata);
void cbm_ht_foreach(const CBMHashTable *ht, cbm_ht_iter_fn fn, void *userdata);
/* Clear all entries (keeps allocated memory). */
void cbm_ht_clear(CBMHashTable *ht);
#endif /* CBM_HASH_TABLE_H */