Adding full-text search for Chinese, Japanese, and Thai to a Rust service usually comes with two quiet surprises: dictionary segmenters allocate 33–50 MB of live heap just to start up, and segmentation alone does not reconcile index terms with user queries across Traditional/Simplified Han (數據庫 vs 数据库), English inflections (cutting vs cut), German umlauts (Häuser vs haus), or formatted numbers (1,000.50 vs 1000.5).
mcut 0.1.1 and lexf 0.1.0 solve those two halves together in safe Rust (#![forbid(unsafe_code)]), with 0 KB startup heap and 0 heap allocations per call on the hot path.
Minimal runnable pipeline
mcut borrows &str slices directly from the input buffer, and lexf writes normalized forms and stems into a caller-owned Scratch buffer:
use lexf::{LexConfig, Scratch, detect_lang, expand_each}; use mcut::{Cut, CutMode, MCUT}; fn main() { let text = "南京市长江大桥 · 數據庫 1,000.50 · Die Häuser"; let cfg = LexConfig { lang: detect_lang(text), ..LexConfig::default() }; let mut scratch = Scratch::new(); MCUT.cut_each(text, CutMode::Search, |tok| { print!("{} ->", tok.text); expand_each(tok.text, cfg, &mut scratch, |form| print!(" {form}")); println!(); }); } CutMode::Search emits both compound words and 2/3-character dictionary subwords for inverted-index recall:
南京市长江大桥 → 南京 / 南京市 / 长江 / 大桥 / 长江大桥 Meanwhile lexf deterministically emits up to 6 deduplicated forms per token:
數據庫 -> 數據庫 数据库 1,000.50 -> 1000.50 1000.5 Häuser -> häuser hauser haeuser haus Benchmarks
Every engine runs in an isolated child process on Apple M2 Max (darwin / arm64, bench profile with lto = "fat"). Full methodology, corpus details, and unicode-seg / mcut-vec numbers live in mcut_bench.
| Engine | Cold start | Live heap | Max RSS | zh_doc | zht_doc | zh_search | mixed_doc | Allocs / call |
|---|---|---|---|---|---|---|---|---|
mcut-stream (lg) | 15.22 ms | 0 KB | 10,560 KB | 234.20 MB/s | 245.84 MB/s | 222.16 MB/s | 84.16 MB/s | 0 |
mcut-sm (sm) | 6.77 ms | 0 KB | 5,760 KB | 255.18 MB/s | 265.73 MB/s | 241.00 MB/s | 82.80 MB/s | 0 |
jieba-rs | 52.59 ms | 33,273 KB | 39,376 KB | 206.29 MB/s | 171.57 MB/s | 159.87 MB/s | - | 1 |
charabia | 106.58 ms | 45,792 KB | 50,704 KB | 24.75 MB/s | 26.87 MB/s | - | 33.12 MB/s | 2,525 |
jieba-rs and charabia construct hash maps or FSTs on the heap at startup (33,273 KB and 45,792 KB live heap). mcut reads directly from demand-paged &'static [u8] data, keeping resident memory at 10,560 KB (lg) and 5,760 KB (sm).
Four design choices that drove the numbers
- Demand-paged
FusedTriewithu64SWAR probes. Unigram weights, 2/3-gram weights, and prefix lookup share a single flat read-only table. Common Han codepoints resolve via a direct-index LUT; deeper transitions inspect 8-slot groups concurrently with a singleu64tag comparison. Slot-group slices are bounds-validated once inFusedTrie::from_slice, eliminating repeated chunking divisions on the hot path. - Pointwise linear classification without a Viterbi lattice. Inspired by Vaporetto, each character boundary is scored and emitted in one pass. Pure-Han spans up to 64 characters run on a 67-slot stack buffer using
fearless_simdi16x8operations withoutunsafe. - One-pass
Searchmode viau64/u128bitmasks (108 → 241 MB/s). Previouslyzh_searchlagged behindjieba-rs(108.81 MB/sformcut-streamvs161.53 MB/s) becauseSearchbypassed the pure-Han fast path and re-probed the trie inside every multi-char word. Recording depth-2 and depth-3 dictionary hits directly asu64/u128bitmasks during the scoring pass eliminated secondary probes, raisingzh_searchto222.16 MB/s(mcut-stream) and241.00 MB/s(mcut-sm) with zero.mcutformat changes. - Separating segmentation from normalization (
mcut+lexf). Documents and queries traverse the same boundary and normalization rules so terms align cleanly at the inverted index: useMode::NormAndStemfor indexing and regular search queries, andMode::Normfor prefix queries or exact phrases where stemming alters prefixes.
Modular models, fresh corpora, and user dictionaries
- Six
#![no_std]model crates: defaultlg(zh+ja+th) is8.23 MiBraw /4.15 MiBcompressed download, scoring held-out word F1 of0.9804(zh),0.9591(zht),0.9843(ja), and0.9214(th). Compactsmdrops to3.54 MiBraw /1.76 MiBcompressed. Individual languages can be compiled alone or loaded externally viaModel::from_sliceonmmappages. - Web and social vocabularies: models are trained on web corpora (
Ultra-FineWeb-zh,FineWeb-2) and live Bluesky posts, handling modern phrasing (松弛感,打工人,小红书,智能体) without hand-written regex rules. - Zero-copy domain overrides: lock product or technical terms (
C++,Rust语言,GPT-4o模型) using binary.mdictdictionaries (OwnedUserDict/UserDict), or plug custom scripts into the mixed-language router (UTF-8,GPT-4o,v1.20.3) viaLangCut.
The offline crawler, SimHash deduplication, unsupervised word miner, EM re-segmenter, and linear SVM exporter live in mcut_train.
Links
-
mcutcrate: https://crates.io/crates/mcut -
mcutdocs: https://docs.rs/mcut -
mcutsource: https://atomgit.com/i18n-site/rs/tree/main/mcut -
lexfcrate: https://crates.io/crates/lexf -
lexfdocs: https://docs.rs/lexf -
lexfsource: https://atomgit.com/i18n-site/rs/tree/main/lexf -
mcut_train: https://atomgit.com/i18n-site/mcut_train
