{"slug": "how-we-built-a-zero-alloc-cjk-thai-tokenizer-in-rust-241-mb-s-search-mode", "title": "How We Built a Zero-Alloc CJK & Thai Tokenizer in Rust (241 MB/s Search Mode)", "summary": "A developer released mcut 0.1.1 and lexf 0.1.0, a pair of safe Rust crates that tokenize Chinese, Japanese and Thai text with zero heap allocation on the hot path and no startup heap, borrowing &str slices directly from the input buffer. Benchmarked in isolated child processes on an Apple M2 Max, mcut reached 241.00 MB/s in search mode and 255.18 MB/s on Chinese documents, versus 159.87 MB/s and 206.29 MB/s for jieba-rs, which also allocates 33,273 KB of live heap at startup. The companion lexf crate emits up to six deduplicated normalized forms per token, reconciling Traditional/Simplified Han, English inflections, German umlauts and formatted numbers.", "body_md": "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`).\n\n`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.\n\n`mcut` borrows `&str` slices directly from the input buffer, and `lexf` writes normalized forms and stems into a caller-owned `Scratch` buffer:\n\n```\nuse lexf::{LexConfig, Scratch, detect_lang, expand_each};\nuse mcut::{Cut, CutMode, MCUT};\n\nfn main() {\n  let text = \"南京市长江大桥 · 數據庫 1,000.50 · Die Häuser\";\n  let cfg = LexConfig { lang: detect_lang(text), ..LexConfig::default() };\n  let mut scratch = Scratch::new();\n\n  MCUT.cut_each(text, CutMode::Search, |tok| {\n    print!(\"{} ->\", tok.text);\n    expand_each(tok.text, cfg, &mut scratch, |form| print!(\" {form}\"));\n    println!();\n  });\n}\n```\n\n`CutMode::Search` emits both compound words and 2/3-character dictionary subwords for inverted-index recall:\n\n```\n南京市长江大桥\n→ 南京 / 南京市 / 长江 / 大桥 / 长江大桥\n```\n\nMeanwhile `lexf` deterministically emits up to 6 deduplicated forms per token:\n\n``` php\n數據庫   -> 數據庫 数据库\n1,000.50 -> 1000.50 1000.5\nHäuser   -> häuser hauser haeuser haus\n```\n\nEvery 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](https://atomgit.com/i18n-site/rs/blob/main/mcut/i18n/en/bench/detail.md).\n\n| Engine | Cold start | Live heap | Max RSS | zh_doc | zht_doc | zh_search | mixed_doc | Allocs / call | \n|---|---|---|---|---|---|---|---|---|\n| `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 | \n| `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 | \n| `jieba-rs` | 52.59 ms | 33,273 KB | 39,376 KB | 206.29 MB/s | 171.57 MB/s | 159.87 MB/s | - | 1 | \n| `charabia` | 106.58 ms | 45,792 KB | 50,704 KB | 24.75 MB/s | 26.87 MB/s | - | 33.12 MB/s | 2,525 | \n\n`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`).\n\n`FusedTrie` with `u64` SWAR probes.`FusedTrie::from_slice`, eliminating repeated chunking divisions on the hot path.` fearless_simd` `i16x8` operations without `unsafe`.` Search` mode via `u128` bitmasks (108 → 241 MB/s).`zh_search` lagged behind `jieba-rs` (`108.81 MB/s` for `mcut-stream` vs `161.53 MB/s`) because `222.16 MB/s` (`241.00 MB/s` (`mcut-sm`) with zero `.mcut` format changes.`mcut` + `lexf`).` Mode::NormAndStem` for indexing and regular search queries, and `Mode::Norm` for prefix queries or exact phrases where stemming alters prefixes.`#![no_std]` model crates`lg` (`zh` + `ja` + `th`) is `8.23 MiB` raw / `4.15 MiB` compressed download, scoring held-out word F1 of `0.9804` (`0.9591` (`zht`), `0.9843` (`0.9214` (`sm` drops to `3.54 MiB` raw / `1.76 MiB` compressed. Individual languages can be compiled alone or loaded externally via `Model::from_slice` on `mmap` pages.`Ultra-FineWeb-zh`, `FineWeb-2`) and live Bluesky posts, handling modern phrasing (`松弛感`, `打工人`, `小红书`, `智能体`) without hand-written regex rules.` C++`, `Rust语言`, `GPT-4o模型`) using binary `.mdict` dictionaries (`OwnedUserDict` / `UserDict`), or plug custom scripts into the mixed-language router (` UTF-8`, `GPT-4o`, `v1.20.3`) via `LangCut`.\nThe offline crawler, SimHash deduplication, unsupervised word miner, EM re-segmenter, and linear SVM exporter live in `mcut_train`.\n\n`mcut_train`:", "url": "https://wpnews.pro/news/how-we-built-a-zero-alloc-cjk-thai-tokenizer-in-rust-241-mb-s-search-mode", "canonical_source": "https://dev.to/webcsite/how-we-built-a-zero-alloc-cjk-thai-tokenizer-in-rust-241-mbs-search-mode-5f1a", "published_at": "2026-12-04 02:33:48+00:00", "updated_at": "2026-12-04 02:45:55.753785+00:00", "lang": "en", "topics": ["natural-language-processing", "developer-tools", "ai-search", "ai-infrastructure"], "entities": ["mcut", "lexf", "jieba-rs", "charabia", "Rust", "Apple M2 Max"], "also_reported_by": [], "takeaway": "mcut's Search mode emits compound words plus 2/3-character dictionary subwords from demand-paged static data, hitting 241.00 MB/s with zero per-call allocations versus jieba-rs at 159.87 MB/s and 33,273 KB of startup heap.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/how-we-built-a-zero-alloc-cjk-thai-tokenizer-in-rust-241-mb-s-search-mode", "markdown": "https://wpnews.pro/news/how-we-built-a-zero-alloc-cjk-thai-tokenizer-in-rust-241-mb-s-search-mode.md", "text": "https://wpnews.pro/news/how-we-built-a-zero-alloc-cjk-thai-tokenizer-in-rust-241-mb-s-search-mode.txt", "jsonld": "https://wpnews.pro/news/how-we-built-a-zero-alloc-cjk-thai-tokenizer-in-rust-241-mb-s-search-mode.jsonld"}}