Skip to content

feat(rag): pilot a Rust port of the BM25 core with rust-anything - #2214

Draft
murdore wants to merge 1 commit into
juspay:releasefrom
murdore:feat/rust-bm25-pilot
Draft

murdore wants to merge 1 commit into
juspay:releasefrom
murdore:feat/rust-bm25-pilot

Conversation

@murdore

@murdore murdore commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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.

Version (100,000 docs, medians of 3 runs) Search Index build Process memory
TypeScript today (linear scan) 610 ms 1.2 s 567 MB
Rust port, same algorithm 399 ms (1.5x) 0.7 s 916 MB
TypeScript with an inverted index 3.7 ms (165x) 6.4 s 692 MB

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: InMemoryBM25Index moves out of hybridSearch.ts. It is now a thin async wrapper around a synchronous BM25Core (bm25Core.ts), because the bridge cannot port async methods. Public API and behaviour are unchanged.
  • rust/bm25-rs/: the Rust crate. src/port.rs is the port. src/fdlibm.rs is V8's Math.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.
  • .prettierignore and eslint.config.js skip each crate's target/ and .rust-anything/ folders. Without that, building the crate locally fails the pre-commit hook.

Findings that matter for the migration

  • Matching scores needs V8's math. Rust's f64::ln differs from Math.log in the last bit for about 2% of inputs. fdlibm.rs ports fdlibm's log and 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.
  • Async classes need a TypeScript refactor before they can be ported.
  • A straight port keeps the old data layout, so it does not save memory.
  • rust-anything analyze found 140 of 153 src/lib units in one import cycle.

Checks

  • Refactor parity: the old class from release and the new split were compared on 13,242 random cases with 0 mismatches. A deliberately broken copy failed the same test (2,167 mismatches).
  • Port parity: rust-anything diff matched 500/500 cases at 100% line coverage, and fuzz matched 3,260/3,260 fresh cases. gate passed all 13 checks (fmt, clippy, tests, no unsafe, no panics, coverage, behaviour).
  • Inverted-index parity: it matched the core on 12,864 random cases, including replaced documents. The broken-copy control failed.
  • Repo checks: 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.

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.
@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

This branch has not been deployed

No deployments
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.

1 participant