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.

Fix O(E*N) scan in arch_boundaries: binary-search the id->package map

Martin Vogelb904cf0ab5da
diff --git a/src/store/store.c b/src/store/store.c
index 1e7624514..09bb76028 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -3418,11 +3418,22 @@ static int arch_hotspots(cbm_store_t *s, const char *project, cbm_architecture_i
return CBM_STORE_OK;
}
-/* Look up package name for a node ID in the parallel arrays. */
+/* Look up package name for a node ID in the parallel arrays. nids must be
+ * sorted ascending (the node query orders by id) — a linear scan here is
+ * O(E×N) across the edge loop and spun for >10 minutes on Linux-kernel-sized
+ * graphs (~1.4M defs × ~1.4M CALLS edges). */
static const char *lookup_pkg(const int64_t *nids, char **npkgs, int nn, int64_t id) {
- for (int i = 0; i < nn; i++) {
- if (nids[i] == id) {
- return npkgs[i];
+ int lo = 0;
+ int hi = nn - SKIP_ONE;
+ while (lo <= hi) {
+ int mid = lo + (hi - lo) / PAIR_LEN;
+ if (nids[mid] == id) {
+ return npkgs[mid];
+ }
+ if (nids[mid] < id) {
+ lo = mid + SKIP_ONE;
+ } else {
+ hi = mid - SKIP_ONE;
}
}
return NULL;
@@ -3450,9 +3461,9 @@ static void accum_boundary(const char *src_pkg, const char *tgt_pkg, char **bfro
static int arch_boundaries(cbm_store_t *s, const char *project, cbm_cross_pkg_boundary_t **out_arr,
int *out_count) {
- /* Build nodeID → package map */
+ /* Build nodeID → package map. ORDER BY id so lookup_pkg can binary-search. */
const char *nsql = "SELECT id, qualified_name FROM nodes WHERE project=?1 AND label IN "
- "('Function','Method','Class')";
+ "('Function','Method','Class') ORDER BY id";
sqlite3_stmt *nstmt = NULL;
if (sqlite3_prepare_v2(s->db, nsql, CBM_NOT_FOUND, &nstmt, NULL) != SQLITE_OK) {
store_set_error_sqlite(s, "arch_boundaries_nodes");
diff --git a/tests/test_store_arch.c b/tests/test_store_arch.c
index 7bebadbe7..104552049 100644
--- a/tests/test_store_arch.c
+++ b/tests/test_store_arch.c
@@ -24,6 +24,8 @@
#include <store/store.h>
#include <string.h>
#include <stdlib.h>
+#include <stdint.h>
+#include <time.h>
/* ── Helper: create architecture test store ──────────────────────── */
@@ -314,6 +316,66 @@ TEST(arch_boundaries) {
PASS();
}
+/* arch_boundaries used a LINEAR SCAN over all def nodes for EVERY CALLS edge
+ * (lookup_pkg over parallel arrays) — O(E×N). On the Linux kernel graph
+ * (~1.4M defs × ~1.4M CALLS) get_architecture spun >10 min at 100% CPU. The
+ * bug was latent while C call extraction was broken (few CALLS edges) and
+ * surfaced when extraction was fixed. This guards the linear(ish) behavior:
+ * 80k defs + 160k CALLS must finish the boundaries aspect well under the bound
+ * (the quadratic ≈ 1.3×10^10 scan steps takes ~17 s here; a log-time lookup
+ * is instant). */
+TEST(arch_boundaries_no_quadratic_scan) {
+ enum { BN_NODES = 80000, BN_EDGES = 160000, BN_PKGS = 200, BN_BOUND_MS = 8000 };
+ cbm_store_t *s = cbm_store_open_memory();
+ ASSERT_NOT_NULL(s);
+ cbm_store_upsert_project(s, "perf", "/tmp/perf");
+
+ cbm_store_begin(s);
+ int64_t *ids = malloc(BN_NODES * sizeof(int64_t));
+ ASSERT_NOT_NULL(ids);
+ for (int i = 0; i < BN_NODES; i++) {
+ char name[32], qn[64];
+ snprintf(name, sizeof(name), "fn%d", i);
+ snprintf(qn, sizeof(qn), "perf.pkg%d.fn%d", i % BN_PKGS, i);
+ cbm_node_t n = {.project = "perf",
+ .label = "Function",
+ .name = name,
+ .qualified_name = qn,
+ .file_path = "f.c"};
+ ids[i] = cbm_store_upsert_node(s, &n);
+ }
+ uint64_t rng = 42;
+ for (int i = 0; i < BN_EDGES; i++) {
+ rng = rng * 6364136223846793005ULL + 1442695040888963407ULL;
+ int a = (int)((rng >> 33) % BN_NODES);
+ rng = rng * 6364136223846793005ULL + 1442695040888963407ULL;
+ int b = (int)((rng >> 33) % BN_NODES);
+ cbm_edge_t e = {
+ .project = "perf", .source_id = ids[a], .target_id = ids[b], .type = "CALLS"};
+ cbm_store_insert_edge(s, &e);
+ }
+ cbm_store_commit(s);
+
+ cbm_architecture_info_t info;
+ memset(&info, 0, sizeof(info));
+ const char *aspects[] = {"boundaries"};
+ struct timespec t0, t1;
+ clock_gettime(CLOCK_MONOTONIC, &t0);
+ ASSERT_EQ(cbm_store_get_architecture(s, "perf", aspects, 1, &info), CBM_STORE_OK);
+ clock_gettime(CLOCK_MONOTONIC, &t1);
+ double ms =
+ (double)(t1.tv_sec - t0.tv_sec) * 1000.0 + (double)(t1.tv_nsec - t0.tv_nsec) / 1000000.0;
+ printf(" boundaries on %dk nodes/%dk edges: %.0f ms (bound %d)\n", BN_NODES / 1000,
+ BN_EDGES / 1000, ms, BN_BOUND_MS);
+ ASSERT_TRUE(info.boundary_count > 0); /* cross-package CALLS exist */
+ ASSERT_TRUE(ms < (double)BN_BOUND_MS);
+
+ cbm_store_architecture_free(&info);
+ free(ids);
+ cbm_store_close(s);
+ PASS();
+}
+
TEST(arch_layers) {
cbm_store_t *s = setup_arch_test_store();
cbm_architecture_info_t info;
@@ -1194,6 +1256,7 @@ SUITE(store_arch) {
RUN_TEST(arch_routes);
RUN_TEST(arch_hotspots);
RUN_TEST(arch_boundaries);
+ RUN_TEST(arch_boundaries_no_quadratic_scan);
RUN_TEST(arch_layers);
RUN_TEST(arch_file_tree);
RUN_TEST(arch_clusters);