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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
perf(persist): batch file-hash upserts + span the post-dump tail
Martin Vogelbb99d40e9989
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.cindex e9f81f45a..c3b7d572e 100644--- a/src/pipeline/pipeline.c+++ b/src/pipeline/pipeline.c@@ -1046,30 +1046,71 @@ static int dump_and_persist_hashes(cbm_pipeline_t *p, const cbm_file_info_t *filreturn rc;}cbm_log_info("pass.timing", "pass", "dump", "elapsed_ms", itoa_buf((int)elapsed_ms(*t)));+ /* Persist-tail spans (phase "persist"): attribute the ~60s that lands here+ * AFTER cbm_gbuf_dump_to_sqlite returns. Active only under CBM_PROFILE. */+ CBM_PROF_START(t_reopen);cbm_store_t *hash_store = cbm_store_open_path(db_path);+ CBM_PROF_END("persist", "1_reopen", t_reopen);if (hash_store) {+ CBM_PROF_START(t_delhash);cbm_store_delete_file_hashes(hash_store, p->project_name);+ CBM_PROF_END("persist", "2_delete_file_hashes", t_delhash);/* Restore the ADR captured before the dump. Surface a failed restore* rather than silently dropping the ADR (the original #516 symptom). */+ CBM_PROF_START(t_adr);if (p->saved_adr) {if (cbm_store_adr_store(hash_store, p->project_name, p->saved_adr) != CBM_STORE_OK) {cbm_log_error("pipeline.err", "phase", "adr_restore", "project", p->project_name);}}- for (int i = 0; i < file_count; i++) {- struct stat fst;- if (stat(files[i].path, &fst) == 0) {- cbm_store_upsert_file_hash(hash_store, p->project_name, files[i].rel_path, "",- stat_mtime_ns(&fst), fst.st_size);+ CBM_PROF_END("persist", "3_adr_restore", t_adr);++ /* Batch the per-file hash upserts into ONE transaction. The per-file+ * cbm_store_upsert_file_hash path autocommits, i.e. file_count fsyncs+ * (~89k on the kernel); cbm_store_upsert_file_hash_batch wraps the same+ * cached INSERT ... ON CONFLICT upsert in a single begin/commit. Same+ * (project, rel_path, sha256="", mtime_ns, size) tuples, same replace+ * semantics — only the transaction boundary changes. */+ CBM_PROF_START(t_fh);+ cbm_file_hash_t *fhashes = (cbm_file_hash_t *)malloc(+ (size_t)(file_count > 0 ? file_count : 1) * sizeof(cbm_file_hash_t));+ if (fhashes) {+ int fh_n = 0;+ for (int i = 0; i < file_count; i++) {+ struct stat fst;+ if (stat(files[i].path, &fst) == 0) {+ fhashes[fh_n].project = p->project_name;+ fhashes[fh_n].rel_path = files[i].rel_path;+ fhashes[fh_n].sha256 = "";+ fhashes[fh_n].mtime_ns = stat_mtime_ns(&fst);+ fhashes[fh_n].size = fst.st_size;+ fh_n++;+ }+ }+ if (cbm_store_upsert_file_hash_batch(hash_store, fhashes, fh_n) != CBM_STORE_OK) {+ cbm_log_error("pipeline.err", "phase", "persist_file_hashes", "project",+ p->project_name);+ }+ free(fhashes);+ } else {+ /* OOM fallback: the original per-file path (identical result, slower). */+ for (int i = 0; i < file_count; i++) {+ struct stat fst;+ if (stat(files[i].path, &fst) == 0) {+ cbm_store_upsert_file_hash(hash_store, p->project_name, files[i].rel_path, "",+ stat_mtime_ns(&fst), fst.st_size);+ }}}+ CBM_PROF_END_N("persist", "4_file_hashes", t_fh, file_count);/* FTS5 backfill: populate nodes_fts with camelCase-split names.* Contentless FTS5 requires the special 'delete-all' command instead of* DELETE FROM to wipe prior rows (there's no underlying content table).* Falls back to plain names if cbm_camel_split is unavailable (which* shouldn't happen because we always register it, but we stay defensive). */+ CBM_PROF_START(t_fts);cbm_store_exec(hash_store, "INSERT INTO nodes_fts(nodes_fts) VALUES('delete-all');");if (cbm_store_exec(hash_store,"INSERT INTO nodes_fts(rowid, name, qualified_name, label, file_path) "@@ -1079,6 +1120,7 @@ static int dump_and_persist_hashes(cbm_pipeline_t *p, const cbm_file_info_t *fil"INSERT INTO nodes_fts(rowid, name, qualified_name, label, file_path) ""SELECT id, name, qualified_name, label, file_path FROM nodes;");}+ CBM_PROF_END("persist", "5_fts_backfill", t_fts);cbm_store_close(hash_store);cbm_log_info("pass.timing", "pass", "persist_hashes", "files", itoa_buf(file_count));@@ -1088,7 +1130,9 @@ static int dump_and_persist_hashes(cbm_pipeline_t *p, const cbm_file_info_t *fil/* Export persistent artifact if enabled */if (p->persistence) {+ CBM_PROF_START(t_art);int arc = cbm_artifact_export(db_path, p->repo_path, p->project_name, CBM_ARTIFACT_BEST);+ CBM_PROF_END("persist", "6_artifact_export", t_art);if (arc != 0) {const char *err = cbm_artifact_export_last_error();cbm_log_error("pipeline.err", "phase", "artifact_export", "err", err ? err : "unknown");diff --git a/tests/test_store_nodes.c b/tests/test_store_nodes.cindex 0d84f4c78..5b30ef9f5 100644--- a/tests/test_store_nodes.c+++ b/tests/test_store_nodes.c@@ -807,6 +807,62 @@ TEST(store_file_hash_batch) {PASS();}+/* Guard for the persist-tail switch (pipeline.c dump_and_persist_hashes) from a+ * per-file cbm_store_upsert_file_hash loop to a single cbm_store_upsert_file_hash_batch:+ * both paths must yield IDENTICAL file_hashes rows (same tuples, same upsert/replace+ * semantics — only the transaction boundary differs). Uses sha256="" exactly as the+ * persist path does. */+TEST(store_file_hash_batch_equals_loop) {+ cbm_file_hash_t rows[4] = {+ {.project = "p", .rel_path = "a.c", .sha256 = "", .mtime_ns = 111, .size = 10},+ {.project = "p", .rel_path = "b/c.c", .sha256 = "", .mtime_ns = 222, .size = 20},+ {.project = "p", .rel_path = "d.rs", .sha256 = "", .mtime_ns = 333, .size = 30},+ {.project = "p", .rel_path = "e.py", .sha256 = "", .mtime_ns = 444, .size = 40},+ };++ /* Store A: the original per-file loop. */+ cbm_store_t *a = cbm_store_open_memory();+ cbm_store_upsert_project(a, "p", "/tmp/p");+ for (int i = 0; i < 4; i++) {+ ASSERT_EQ(cbm_store_upsert_file_hash(a, rows[i].project, rows[i].rel_path, rows[i].sha256,+ rows[i].mtime_ns, rows[i].size),+ CBM_STORE_OK);+ }++ /* Store B: the batched path. */+ cbm_store_t *b = cbm_store_open_memory();+ cbm_store_upsert_project(b, "p", "/tmp/p");+ ASSERT_EQ(cbm_store_upsert_file_hash_batch(b, rows, 4), CBM_STORE_OK);++ cbm_file_hash_t *ha = NULL, *hb = NULL;+ int ca = 0, cb = 0;+ ASSERT_EQ(cbm_store_get_file_hashes(a, "p", &ha, &ca), CBM_STORE_OK);+ ASSERT_EQ(cbm_store_get_file_hashes(b, "p", &hb, &cb), CBM_STORE_OK);+ ASSERT_EQ(ca, 4);+ ASSERT_EQ(cb, 4);++ /* Compare as sets (get_file_hashes has no ORDER BY). */+ for (int i = 0; i < ca; i++) {+ int found = 0;+ for (int j = 0; j < cb; j++) {+ if (strcmp(ha[i].rel_path, hb[j].rel_path) == 0) {+ ASSERT_STR_EQ(ha[i].sha256, hb[j].sha256);+ ASSERT_EQ(ha[i].mtime_ns, hb[j].mtime_ns);+ ASSERT_EQ(ha[i].size, hb[j].size);+ found = 1;+ break;+ }+ }+ ASSERT_TRUE(found);+ }++ cbm_store_free_file_hashes(ha, ca);+ cbm_store_free_file_hashes(hb, cb);+ cbm_store_close(a);+ cbm_store_close(b);+ PASS();+}+/* ── Find edges by URL path ────────────────────────────────────── */TEST(store_find_edges_by_url_path) {@@ -1572,6 +1628,7 @@ SUITE(store_nodes) {RUN_TEST(store_find_by_qn_suffix_dot_boundary);RUN_TEST(store_node_degree);RUN_TEST(store_file_hash_batch);+ RUN_TEST(store_file_hash_batch_equals_loop);RUN_TEST(store_find_edges_by_url_path);RUN_TEST(store_restore_from);RUN_TEST(store_pragma_settings);