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(service_patterns): per-worker TLS cache for cbm_service_pattern_match

Martin Vogel1af1dd868858
diff --git a/internal/cbm/service_patterns.c b/internal/cbm/service_patterns.c
index a723a84dd..9e799d221 100644
--- a/internal/cbm/service_patterns.c
+++ b/internal/cbm/service_patterns.c
@@ -14,6 +14,7 @@
#include <stdbool.h>
#include <stddef.h>
+#include <stdlib.h>
#include <string.h>
/* ── Library identifier → edge type ────────────────────────────── */
@@ -545,6 +546,45 @@ static const lib_pattern_t *match_qn(const char *qn, const lib_pattern_t *patter
/* ── Public API ────────────────────────────────────────────────── */
+/* Per-worker TLS cache of cbm_service_pattern_match results.
+ * The hot path in resolve_file_calls invokes pattern matching for
+ * EVERY resolved CALL (via emit_service_edge) — that's 6 pattern-list
+ * scans × ~30 patterns × strstr per call. On kubernetes (~600k
+ * resolved call edges), the same resolved QN (e.g. "context.Context.
+ * Done", "fmt.Errorf", "errors.New") repeats hundreds of thousands of
+ * times. A simple TLS hash cache turns the linear scan into one
+ * lookup after the first miss for that QN. Lifetime is per-worker for
+ * the duration of the parallel_resolve phase. */
+#include "foundation/hash_table.h"
+#include "foundation/compat.h"
+
+static CBM_TLS CBMHashTable *_svc_cache = NULL;
+/* Encode the enum + 1 in the pointer so 0/NULL means "miss". */
+static inline void *svc_enum_to_ptr(cbm_svc_kind_t k) {
+ return (void *)(uintptr_t)((unsigned)k + 1u);
+}
+static inline cbm_svc_kind_t svc_ptr_to_enum(void *p) {
+ return (cbm_svc_kind_t)((uintptr_t)p - 1u);
+}
+
+static void svc_cache_free_key(const char *key, void *val, void *ud) {
+ (void)val;
+ (void)ud;
+ free((char *)key);
+}
+
+void cbm_service_pattern_cache_begin(void) {
+ if (_svc_cache) return; /* idempotent */
+ _svc_cache = cbm_ht_create(8192);
+}
+
+void cbm_service_pattern_cache_end(void) {
+ if (!_svc_cache) return;
+ cbm_ht_foreach(_svc_cache, svc_cache_free_key, NULL);
+ cbm_ht_free(_svc_cache);
+ _svc_cache = NULL;
+}
+
void cbm_service_patterns_init(void) {
/* No-op — tables are static const */
}
@@ -554,44 +594,31 @@ cbm_svc_kind_t cbm_service_pattern_match(const char *resolved_qn) {
return CBM_SVC_NONE;
}
- /* Route registration checked first — prevents gin/echo from matching
- * as HTTP clients (both have .get/.post suffixes). */
- const lib_pattern_t *p = match_qn(resolved_qn, route_reg_libraries);
- if (p) {
- return p->kind;
- }
-
- p = match_qn(resolved_qn, http_libraries);
- if (p) {
- return p->kind;
- }
-
- p = match_qn(resolved_qn, async_libraries);
- if (p) {
- return p->kind;
- }
-
- p = match_qn(resolved_qn, config_libraries);
- if (p) {
- return p->kind;
- }
-
- p = match_qn(resolved_qn, grpc_libraries);
- if (p) {
- return p->kind;
+ if (_svc_cache) {
+ void *cached = cbm_ht_get(_svc_cache, resolved_qn);
+ if (cached) {
+ return svc_ptr_to_enum(cached);
+ }
}
- p = match_qn(resolved_qn, graphql_libraries);
- if (p) {
- return p->kind;
- }
+ cbm_svc_kind_t result = CBM_SVC_NONE;
+ const lib_pattern_t *p;
- p = match_qn(resolved_qn, trpc_libraries);
- if (p) {
- return p->kind;
+ /* Route registration checked first — prevents gin/echo from matching
+ * as HTTP clients (both have .get/.post suffixes). */
+ if ((p = match_qn(resolved_qn, route_reg_libraries))) result = p->kind;
+ else if ((p = match_qn(resolved_qn, http_libraries))) result = p->kind;
+ else if ((p = match_qn(resolved_qn, async_libraries))) result = p->kind;
+ else if ((p = match_qn(resolved_qn, config_libraries))) result = p->kind;
+ else if ((p = match_qn(resolved_qn, grpc_libraries))) result = p->kind;
+ else if ((p = match_qn(resolved_qn, graphql_libraries))) result = p->kind;
+ else if ((p = match_qn(resolved_qn, trpc_libraries))) result = p->kind;
+
+ if (_svc_cache) {
+ char *kdup = strdup(resolved_qn);
+ if (kdup) cbm_ht_set(_svc_cache, kdup, svc_enum_to_ptr(result));
}
-
- return CBM_SVC_NONE;
+ return result;
}
const char *cbm_service_pattern_http_method(const char *callee_name) {
diff --git a/internal/cbm/service_patterns.h b/internal/cbm/service_patterns.h
index 6f042123e..8a87c63fa 100644
--- a/internal/cbm/service_patterns.h
+++ b/internal/cbm/service_patterns.h
@@ -32,6 +32,16 @@ void cbm_service_patterns_init(void);
* "project.venv.requests.api.get"). Import-alias transparent. */
cbm_svc_kind_t cbm_service_pattern_match(const char *resolved_qn);
+/* Per-worker TLS cache for cbm_service_pattern_match results. The
+ * pattern matcher runs once per resolved CALL edge in emit_service_
+ * edge — that's 6 pattern lists × ~30 patterns × strstr per call ≈
+ * ~180 strstrs per call. The same resolved QN repeats across most of
+ * the call edges in a project (e.g. "fmt.Errorf"), so caching turns
+ * a linear pattern-list scan into one hash lookup. Call _begin once
+ * per worker thread before the resolve loop and _end at the end. */
+void cbm_service_pattern_cache_begin(void);
+void cbm_service_pattern_cache_end(void);
+
/* Get the HTTP method from the callee name suffix (e.g., ".get" → "GET").
* Returns NULL if method cannot be inferred. */
const char *cbm_service_pattern_http_method(const char *callee_name);
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index f8ed3a256..9b2d3ce30 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -1848,6 +1848,14 @@ static void resolve_worker(int worker_id, void *ctx_ptr) {
cbm_gbuf_new_shared_ids(rc->project_name, rc->repo_path, rc->shared_ids);
}
+ /* Per-worker service-pattern result cache. The same resolved QN
+ * (e.g. "fmt.Errorf", "context.Context.Done") appears in many
+ * call edges across many files within a project — caching turns
+ * cbm_service_pattern_match's 6 × 30 × strstr scan into one hash
+ * lookup after the first miss for each QN. Scoped to the worker's
+ * lifetime in the parallel_resolve phase. */
+ cbm_service_pattern_cache_begin();
+
while (SKIP_ONE) {
int file_idx =
atomic_fetch_add_explicit(&rc->next_file_idx, SKIP_ONE, memory_order_relaxed);
@@ -2092,6 +2100,8 @@ static void resolve_worker(int worker_id, void *ctx_ptr) {
extract_now_ns() - _loop_t0,
memory_order_relaxed);
}
+
+ cbm_service_pattern_cache_end();
}
int cbm_parallel_resolve(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, int file_count,