Repository navigation
Complete merges on writer release and add a long-lived collection writer - #14
Merged
Merged
Conversation
tantivy kills its merge threads when an IndexWriter is dropped, and a commit schedules merges asynchronously, so every scoped helper (write/add/upsert/ delete: writer, commit, release) abandoned its merges and left one segment per call behind. 200 per-document adds produced a 200-segment index whose searches were ~10x slower than the batch-built equivalent. tantivy_writer_free now calls wait_merging_threads() instead of dropping the writer; the same loop leaves 4 segments and, on disk, runs faster because the index stays small. SearchCollection gains writer(heapSize:) returning a typed Writer (add, add(contentsOf:), upsert, remove, removeAll, commit, rollback, indexWriter) so callers whose models arrive one at a time can hold one writer and commit per batch instead of paying a writer + commit + reload per document. The single-document helpers on Index and SearchCollection, the IndexWriter type docs and the README now state the per-call cost plainly and point at the batch and long-lived alternatives. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
myluma
pushed a commit
that referenced
this pull request
Sep 19, 2026
Resolves the conflict in tantivy_writer_free: keep main's wait for background merges (#14) and run it inside catch_unwind, so a panic in the drop path cannot escape the extern "C" boundary and abort. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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.
Summary
Follow-up to the review note that the per-document helpers (
index.add(doc),upsert,delete(matching:)) are expensive in a loop. Measuring it turned up a second, worse problem in the same path, fixed here alongside the API and doc changes the note asked for.What changed
IndexWriteris dropped, and a commit schedules merges asynchronously, so every scoped helper (writer → commit → release) abandoned its merges and left one segment per call behind.tantivy_writer_freenow calls tantivy's publicwait_merging_threads()instead of dropping. tantivy itself is unchanged; this is shim-only.SearchCollection.writer(heapSize:)returns a new typedSearchCollection.Writerwithadd,add(contentsOf:),upsert,remove(idField:id:),remove(matching:),removeAll,commit(commits + reloads),rollback, and anindexWriterescape hatch.write {}is unchanged to avoid a source break.IndexandSearchCollectionnow says it opens a writer, commits and reloads per call and points atadd(contentsOf:),write {}or a long-lived writer.IndexWritertype docs and the README explain the cost and the wait-on-release behaviour..onCommitpolicy, persistence; and regression tests that scoped adds no longer leave one segment per call, in memory and on disk. 414 tests pass.Measurements (200 small docs, release build, this machine)
index.add(doc)in a loop, beforeindex.add(contentsOf:)onceindex.add(doc)in a loop, afterWriter creation itself is cheap (~0.1 ms). The per-call cost is the commit (segment write + fsync, ~65 ms on disk here); the segment growth was the hidden cost, making searches on the loop-built index ~10x slower.
Reviewer notes
release.shrebuilds the binary. Until then the docs describe behaviour the current binary lacks.IndexWriter; avoid releasing a busy writer on the main thread.tantivy_writer_free(deinit cannot report it; committed data is unaffected and the next commit retries).🤖 Generated with Claude Code