Repository navigation
Conversation
A pilot for the Rust migration: one module ported end to end and measured against the TypeScript it replaces and against a better algorithm. InMemoryBM25Index moves to its own file and becomes a thin async wrapper around a synchronous BM25Core, because the napi bridge cannot port async methods. Behaviour is unchanged: a differential test against the release version matched on 13,242 random cases, and a deliberately broken copy failed it. rust/bm25-rs is the Rust port, generated and checked with rust-anything 4.1.0: 500/500 recorded cases and 3,260 fuzzed cases match, and its gate passes. Matching scores bit for bit needed V8's fdlibm log with clang's arm64 fused multiply-adds (src/fdlibm.rs), checked on 2,000,016 values. benchmarks/rust-pilot compares the three versions. The Rust port searches 1.5x faster at 100,000 documents but is 3x slower on small calls and uses more memory; an inverted index in TypeScript searches 165x faster. The native module is not loaded by the shipped package. Prettier and ESLint skip each crate's target/ and .rust-anything/ folders, which the crate's own .gitignore already keeps out of git.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
This branch has not been deployed
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
A pilot for the Rust migration. It ports one module, the in-memory BM25 search index, to Rust with rust-anything 4.1.0 and napi-rs. It then measures the port against the TypeScript it replaces and against a better algorithm in TypeScript.
Result: the Rust port should not ship. Opened as a draft so the findings and the tooling can be reviewed. Nothing in the published package loads the native module.
On rust-anything's small recorded cases, the Rust port ran at 0.34x: crossing from JS into Rust costs more than the work. All three versions return identical ids and scores, bit for bit, at every size.
Changes
src/lib/rag/retrieval/bm25Index.ts:InMemoryBM25Indexmoves out ofhybridSearch.ts. It is now a thin async wrapper around a synchronousBM25Core(bm25Core.ts), because the bridge cannot portasyncmethods. Public API and behaviour are unchanged.rust/bm25-rs/: the Rust crate.src/port.rsis the port.src/fdlibm.rsis V8'sMath.log. The rest is generated bindings, plus 500 recorded cases.benchmarks/rust-pilot/: the inverted-index variant and the three-way benchmark, with a README..prettierignoreandeslint.config.jsskip each crate'starget/and.rust-anything/folders. Without that, building the crate locally fails the pre-commit hook.Findings that matter for the migration
f64::lndiffers fromMath.login the last bit for about 2% of inputs.fdlibm.rsports fdlibm'slogand fuses the multiply-adds that clang fuses for arm64. It matches Node 26.12.1 on 2,000,016 values. x86-64 is not checked.rust-anything analyzefound 140 of 153src/libunits in one import cycle.Checks
releaseand the new split were compared on 13,242 random cases with 0 mismatches. A deliberately broken copy failed the same test (2,167 mismatches).rust-anything diffmatched 500/500 cases at 100% line coverage, andfuzzmatched 3,260/3,260 fresh cases.gatepassed all 13 checks (fmt, clippy, tests, no unsafe, no panics, coverage, behaviour).tsc --noEmit, Prettier and ESLint are clean on the whole tree. The pre-commit and pre-push hooks passed.The full migration analysis is in a separate document; this PR holds only the pilot.