Skip to content

perf(core): compact the vecq index when dead rows pass 25% - #1364

Merged
ajianaz merged 1 commit into
developfrom
perf/vecq-compaction
Oct 8, 2026
Merged

ajianaz merged 1 commit into
developfrom
perf/vecq-compaction

Conversation

@ajianaz

@ajianaz ajianaz commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

What

  • VectorIndex::needs_compaction(): true for a vecq index of at least 128 rows with more than 25% dead (tombstoned) rows. New dead_rows() / physical_rows(). usearch removes in place, so it never has dead rows.
  • Uteke::compact_index_if_needed(dry_run) -> Option<CompactionReport>: rebuilds the index from SQLite (original embeddings, document chunks included via the same loader repair now shares) and persists. The index write lock is held across the SQLite read and the rebuild: a writer that committed before the read is rebuilt in, one that commits after blocks on the lock and inserts into the compacted index, so nothing is lost. Vectors of the wrong width are skipped before build, which resets the index before it validates.
  • Triggers: the dream compact phase (dry-run reports "would drop N dead rows") and uteke-serve's 1s flusher thread (read-lock check, rebuild only once the threshold is crossed).
  • New CompactionReport type exported from uteke-core.

Why

#1324: vecq is append-only, so remove/re-insert leave dead rows. Search over-fetches k + dead from an O(N) scan, and the index and .keys files only ever grow on update-heavy workloads. Nothing compacted them.

Note: while a compaction runs in the server, requests wait (global Mutex<Uteke> plus the index write lock). It only runs after the 25% threshold is crossed, so it is amortized, and only for vecq.

Testing

  • vecq_compaction_shrinks_file_and_drops_dead_rows (acceptance): 200 memories, 150 forgotten -> 200 rows / 150 dead; dry run changes nothing; compaction -> 50 rows, 0 dead, file smaller, a surviving memory still found, state kept after reopen, and a second call is a no-op.
  • Unit tests for the threshold (exactly 25% is not enough, 51/200 is), small indexes never compact, usearch has no dead rows.
  • cargo test --workspace pass, cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --all -- --check clean; onnx,usearch builds and onnx,vecq core tests pass (626); cargo run -p docgen no diff.

Closes #1324

🤖 Generated with Claude Code

vecq rows are append-only: updates and deletes leave tombstoned rows that grow
the index and .keys files and make every search over-fetch k + dead from an O(N)
scan. Nothing ever compacted them.

VectorIndex::needs_compaction() flags a vecq index of >=128 rows with more than
a quarter of them dead. Uteke::compact_index_if_needed(dry_run) rebuilds it from
SQLite (original embeddings, chunks included) while holding the index write
lock across the read and the build so no concurrent write is lost. It runs in
the `dream` compact phase and in uteke-serve's 1s flusher thread (read-lock
check only; the rebuild happens once the threshold is crossed).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@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 c0b713d into develop Oct 8, 2026
15 checks passed
@ajianaz
ajianaz deleted the perf/vecq-compaction branch October 13, 2026 00:57
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: compact vecq index when dead rows accumulate

1 participant