Skip to content

Batches and hot paths

sh
rust-anything bridge <repo> <unit> --batch fn:param    # or: rust-anything refresh <crate> --batch fn:param

--batch generates a class that takes the param values once and keeps them in Rust: new FnBatch(items) (TS, Python, Java), Mod::FnBatch.new(items) (Ruby), NewFnBatch(items) (Go). Per query, run(<other args>) returns every result as one array, and rank(<other args>, min) returns the item indexes, best first. It runs on all cores, and errors are the original's. Every language except C gets batches.

A port called once per item pays the bridge once per item, and loses to a JIT. What wins is moving the loop: profile finds it, and --batch generates a class that takes the items once and runs the function over all of them per call, on all cores, with typed-array results.

sh
rust-anything profile ../x18n -- node --experimental-strip-types $PWD/demo/x18n-search/search.ts .
# -> fuzzyScore: a quarter of the run, called per key by `matched`: batch it
rust-anything bridge ../x18n packages/core/src/fuzzy.ts --batch fuzzyScore:text
js
const search = new FuzzyScoreBatch(keys)          // once
const order = search.rank(query, 0)               // per keystroke: indexes, best first

A FuzzyScoreIndex struct in port.rs replaces the default loop with one that prepares the items once, and can rank by itself (rank): it keeps the keys lowercased with a character mask each, a table of each key's first position of every common character (a one-letter query is one column), and the last query's matches (typing se after s rescores only what s matched). On x18n's sidebar search over 50,000 keys, typed letter by letter, with identical results:

Per query vs TS
TS: map(fuzzyScore), filter, sort 10 ms 1x
TS with the same narrowing as the index 4.1-5.4 ms 2x
The same port, called per key - 0.2-0.4x
FuzzyScoreBatch, default index (4 cores) 5.1 ms 1.7x
FuzzyScoreBatch with FuzzyScoreIndex (napi, 4 cores) 0.45-0.56 ms 18-22x
The same with wasm (one thread) 0.95 ms 11.5x

Against TS doing the same narrowing, Rust is still 7-12x faster (wasm 5x). Turn the returned indexes into keys with a plain loop: Array.from(order, i => keys[i]) cost 20x more, 0.4 ms.

Every language gets batches. Below, the Rust batch runs on 4 cores and the original on one thread, as the original code is written. Identical results and errors (benchmarks in each demo folder):

Language Workload Original (1 thread) Per-call port vs original Batch (4 cores) vs original
TS x18n search, 50,000 keys 10 ms 0.2-0.4x 20x
Python slugify, 20,000 strings 98 ms - 17x
Ruby slugify, 20,000 strings 110 ms - 12.7x
Ruby shellescape (stdlib), 50,000 mixed values 88 ms - 11.8x

Go and Java are left out: they can run the original on 4 threads just as easily, and measured that way the Java original beats the Rust batch (#110). Batch crates allocate with mimalloc: with the system allocator the Go batch was slower than one thread. bridge --allocator rusty_alloc opts into a pure-Rust allocator instead. Demo: demo/x18n-search.

Writing a fast index

  • Make the caller build the batch once, not per query. Map returned indexes back to items with a plain loop.
  • To prepare items once (lowercase, masks, position tables), define pub struct FnIndex in port.rs with new(items) and run(<other args>). It must give the same result as the function, item by item.
  • When ranking can skip work, give the index its own rank(<other args>, min). It must match ranking the function's results: score high to low, ties in item order.
  • A batch multiplies the port's per-item speed by the cores. Against compiled originals (Go, Java) the port itself must be fast: handle ASCII without Unicode tables, build each result in one pre-sized String, avoid intermediate copies.
  • Close the batch when done in Go and Java (finalizers are only a fallback).
Edit this page on GitHub →