Skip to content

perf(core): batch vector-index saves instead of rewriting per write - #1363

Merged
ajianaz merged 2 commits into
developfrom
perf/index-save-debounce
Oct 7, 2026
Merged

ajianaz merged 2 commits into
developfrom
perf/index-save-debounce

Conversation

@ajianaz

@ajianaz ajianaz commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

What

  • VectorIndex tracks unsaved_ops / last_saved and gets save_if_due(): it saves once 64 mutations or 2s have accumulated, otherwise does nothing.
  • index_sync gets persist_deferred and an IndexWriter::save_deferred (default = immediate save, so fakes/other adapters are unchanged). The per-operation paths upsert (remember) and remove_ids (forget) use it. Batch paths (import, re-embed, document chunks, repair) still save explicitly.
  • Pending changes are flushed by: shutdown() (now flush_index(), which peeks with a read lock so it never blocks recall when clean), Drop for VectorIndex (CLI/MCP processes that exit without shutdown), and a 1s flusher thread in uteke-serve (SIGTERM does not run the shutdown path, and a single write followed by idle time must still reach disk).
  • retry() no longer retries Error::Validation (deterministic), so the 200ms backoff cannot sleep under the index lock for input that can never succeed.

Why

#1322: every write serialized the whole index and rewrote the whole .keys sidecar, so bulk ingest was O(N^2) and each save held the index write lock.

Trade-off to review (the issue asked for it to be decided): after a hard kill (SIGKILL/power loss) the on-disk index can trail SQLite by up to 64 ops / ~2s. SQLite stays the source of truth (rows remain FTS5-searchable) and uteke verify / repair resync the vector index, the same recovery as before; the window is just wider than "last op". Clean exits, SIGINT and Drop lose nothing.

Not done here: the save still runs under the index write lock (it is now rare, not gone), and a true linear-time bulk path would need an append-only index format.

Closes #1322

Testing

  • New: save_if_due_batches_writes_and_drop_flushes_the_rest (130 ops -> 2 saves, tail flushed on drop, both engines), save_if_due_is_a_noop_when_clean, upsert_does_not_retry_validation_errors, and bulk_remember_batches_index_saves_without_losing_entries (100 remembers -> <=5 saves, all 100 entries after reopen).
  • cargo test --workspace all pass (core 619); cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --all -- --check clean; cargo run -p docgen no diff.
  • Builds on the sidecar-consistency work in fix(core): verify index/.keys sidecar consistency on load #1362 (independent; a merge of develop will be needed after it lands because both touch the end of vector.rs tests).

🤖 Generated with Claude Code

ajianaz and others added 2 commits October 7, 2026 21:32
…1322)

Every remember/forget rewrote the whole index and .keys sidecar, making bulk
ingest O(N^2) and holding the index write lock for each rewrite.

Per-operation paths (index_sync::upsert, remove_ids) now call persist_deferred,
which saves once 64 mutations or 2s have accumulated (VectorIndex::save_if_due).
Pending changes are still flushed by shutdown(), a new Uteke::flush_index, the
index's Drop, and a 1s flusher thread in uteke-serve (SIGTERM does not run the
shutdown path). Batch paths (import, re-embed, document chunks) keep saving
explicitly. Deterministic validation errors are no longer retried, so the
backoff sleep cannot run under the index lock for input that cannot succeed.

Trade-off: after a hard kill the on-disk index can trail SQLite by up to the
batch window; SQLite remains the source of truth and `uteke verify`/`repair`
resync it.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Keep both the #1325 sidecar tests and the #1322 deferred-save tests; reuse
the shared compiled_backends/saved_index helpers.
@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit 07f5328 into develop Oct 7, 2026
15 checks passed
@ajianaz
ajianaz deleted the perf/index-save-debounce branch October 7, 2026 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: debounce/batch vector index saves instead of full rewrite per write

1 participant