Repository navigation
Add an in-memory, single-threaded WebAssembly build and TypeScript package - #15
Merged
Merged
Conversation
…ckage tantivy's IndexWriter spawns indexing, merge and doc-store threads, which WebAssembly without threads cannot start. A new `single-threaded` Cargo feature swaps in a writer (rust/src/single_threaded.rs) that does the same work on the calling thread from tantivy's public pieces, keeping IndexWriter's opstamps, delete ordering and merge policy. The feature is off by default: the Swift build keeps tantivy's threaded writer and on-disk indexes. scripts/build-wasm.sh compiles the existing C ABI for wasm32-wasip1 with that feature and without mmap/zstd. js/ wraps it in a dependency-free TypeScript package mirroring the Swift API, one WebAssembly instance per index. CI runs the Swift suite against both writers and adds a WASM + JS test job. Co-Authored-By: Claude Opus 5.5 <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.
What
rust/src/single_threaded.rsis a thread-free replacement for tantivy'sIndexWriter, behind a newsingle-threadedCargo feature. It is built from tantivy's public pieces (SegmentWriter,write_alive_bitset,merge_filtered_segments,LogMergePolicy) and keepsIndexWriter's behaviour:commitandrollback;delete_alland rollback work the same way;scripts/build-wasm.shcompiles the existing C ABI towasm32-wasip1with that feature, withoutmmapor zstd, as acdylib. JavaScript calls the sametantivy_*functions Swift does. The only WASM-specific code isrust/src/wasm.rs, which addstantivy_alloc/tantivy_dealloc.js/is a TypeScript package,@carbon/tantivy, markedprivate: truefor now so it can't be published by accident. It mirrors the Swift API:SchemaBuilder,Index,IndexWriter,Query,SearchHitandSearchCollection. The mapping table is injs/README.md.engine.ts.close()returns its memory and a panic retires only that index.scripts/build-host.shforwards its arguments to cargo, e.g.--features single-threaded.Why
We want to run TantivySwift's engine in the browser without
SharedArrayBufferor cross-origin-isolation headers, which the stock threaded build would need. Keeping the JS package on the same C ABI keeps its API in step with Swift.Reviewer notes
nativedefault feature re-enablesmmapandcolumnar-zstd-compressionexactly as tantivy's defaults did, andIndexWriteris still used. The only other native change is that indexes are now created throughIndex::builder(), which is equivalent.commit()in the single-threaded writer, instead of in the background.merge_filtered_segmentswrites into its own index, so the merge output is copied back into the main index.IndexMergerisn't public.opt-level=ssaves about 280 KB gzipped but makes search about 1.6× slower, so the build stays at 3.Testing
swift testpasses 420/420 with the threaded writer and 420/420 with--features single-threaded, including the on-disk tests.npm testpasses 40/40 under Node 24.js/example/index.htmlran in Chromium: it loaded, indexed and searched, with no console errors.cargo testpasses under both feature sets.🤖 Generated with Claude Code