Repository navigation
perf(core): batch vector-index saves instead of rewriting per write - #1363
Merged
Merged
Conversation
…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]>
🔍 Cora AI Code Review✅ No issues found. Code looks good! Review powered by cora-code · BYOK · MIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
VectorIndextracksunsaved_ops/last_savedand getssave_if_due(): it saves once 64 mutations or 2s have accumulated, otherwise does nothing.index_syncgetspersist_deferredand anIndexWriter::save_deferred(default = immediate save, so fakes/other adapters are unchanged). The per-operation pathsupsert(remember) andremove_ids(forget) use it. Batch paths (import, re-embed, document chunks, repair) still save explicitly.shutdown()(nowflush_index(), which peeks with a read lock so it never blocks recall when clean),Drop for VectorIndex(CLI/MCP processes that exit withoutshutdown), and a 1s flusher thread inuteke-serve(SIGTERM does not run the shutdown path, and a single write followed by idle time must still reach disk).retry()no longer retriesError::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
.keyssidecar, 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/repairresync the vector index, the same recovery as before; the window is just wider than "last op". Clean exits, SIGINT andDroplose 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
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, andbulk_remember_batches_index_saves_without_losing_entries(100 remembers -> <=5 saves, all 100 entries after reopen).cargo test --workspaceall pass (core 619);cargo clippy --workspace --all-targets -- -D warnings,cargo fmt --all -- --checkclean;cargo run -p docgenno diff.developwill be needed after it lands because both touch the end ofvector.rstests).🤖 Generated with Claude Code