Skip to content

perf(scratch): reuse Scratch buffers across encode() calls - #2261

Open
SBrandeis wants to merge 2 commits into
feat/train_encode_splitfrom
feat/scratch-reuse
Open

perf(scratch): reuse Scratch buffers across encode() calls#2261
SBrandeis wants to merge 2 commits into
feat/train_encode_splitfrom
feat/scratch-reuse

Conversation

@SBrandeis

@SBrandeis SBrandeis commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

PipelineTokenizer benchmark

9 / 10 models supported — PipelineTokenizer vs tokenizers v0.23.1 (latest release) · ~10 kB inputs · add_special_tokens on · single thread + 1/2/4/8/max-thread sweep

cb8ff8b28 · 2026-07-29 17:16 UTC · Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz · 8 cores

Per-model encode throughput vs latest release

vs base branch (28b2b9633) — per-model geomean ×speedup of this PR's PipelineTokenizer against the base branch's; regressions in red.

Per-model encode throughput vs base branch

Per-model memory footprint

Minimal encode binary size

Decode

Round-trip: v0.23.1 encode_fast produces the id streams (same fixtures, add_special_tokens=true); both implementations decode those SAME ids with skip_special_tokens=false. MB/s counts decoded text bytes.

Per-model decode throughput vs latest release

Per-model decode memory footprint

bert-base-uncased — normalizer-heavy WordPiece · ×4.96 vs v0.23.1 · ×1.02 vs base · decode pending bert-base-uncased speedup bert-base-uncased stage decomposition bert-base-uncased thread scaling bert-base-uncased decode speedup bert-base-uncased decode thread scaling

Memory (RSS MB, load+encode): v0.23.1 12+0 (peak 11) · Pipeline 8+0 (peak 17)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model post Ids
amh_Ethi lang 7.8 27.6 ×3.53 ×1.01 3% (1.2) 76% (27.3) 13% (4.8) 7% (2.7) 0% (0.0) match
arb_Arab lang 4.2 24.9 ×5.92 ×1.02 3% (1.2) 69% (27.4) 8% (3.3) 20% (8.0) 0% (0.0) match
ben_Beng lang 6.0 34.8 ×5.82 ×1.02 4% (1.2) 68% (19.3) 10% (2.9) 18% (5.1) 0% (0.0) match
cmn_Hani lang 3.8 18.7 ×4.90 ×1.03 2% (1.2) 71% (37.6) 11% (5.8) 16% (8.5) 0% (0.0) match
ell_Grek lang 3.8 23.7 ×6.29 ×1.02 3% (1.2) 68% (28.5) 8% (3.4) 21% (8.8) 0% (0.0) match
eng_Latn lang 4.4 18.1 ×4.11 ×1.03 5% (2.7) 70% (38.7) 8% (4.5) 16% (8.9) 0% (0.2) match
heb_Hebr lang 4.2 19.9 ×4.75 ×1.01 2% (1.2) 75% (37.3) 7% (3.7) 15% (7.6) 0% (0.1) match
hin_Deva lang 6.4 27.6 ×4.29 ×1.02 3% (1.2) 75% (26.8) 9% (3.2) 13% (4.8) 0% (0.0) match
jpn_Jpan lang 4.3 27.2 ×6.40 ×1.01 3% (1.2) 64% (23.3) 13% (4.7) 20% (7.3) 0% (0.0) match
kat_Geor lang 6.1 28.2 ×4.60 ×1.02 3% (1.2) 75% (26.5) 9% (3.1) 13% (4.5) 0% (0.0) match
kor_Hang lang 2.4 17.6 ×7.38 ×1.02 2% (1.3) 62% (35.2) 13% (7.5) 23% (12.7) 0% (0.0) match
rus_Cyrl lang 3.7 23.7 ×6.47 ×1.02 3% (1.2) 65% (27.3) 8% (3.2) 25% (10.4) 0% (0.0) match
tam_Taml lang 6.9 38.4 ×5.54 ×1.02 5% (1.2) 72% (18.6) 10% (2.6) 13% (3.3) 0% (0.0) match
tha_Thai lang 8.2 33.2 ×4.04 ×1.02 4% (1.3) 83% (24.9) 7% (2.1) 5% (1.6) 0% (0.0) match
added_normalized_dense modalities 6.7 19.7 ×2.96 ×1.05 3% (1.6) 80% (39.9) 15% (7.2) 2% (0.9) 0% (0.0) match
added_normalized_sparse modalities 5.2 18.4 ×3.52 ×1.04 4% (2.1) 75% (39.8) 13% (6.9) 9% (4.7) 0% (0.0) match
added_special_dense modalities 5.3 38.8 ×7.30 ×0.99 21% (5.1) 37% (9.1) 36% (9.0) 5% (1.3) 1% (0.2) match
added_special_sparse modalities 4.0 21.3 ×5.37 ×1.01 12% (5.3) 58% (26.7) 18% (8.2) 12% (5.7) 0% (0.0) match
agentic-traces modalities 3.8 18.0 ×4.73 ×1.03 5% (2.7) 69% (38.2) 9% (5.0) 17% (9.2) 0% (0.3) match
agentic_swe modalities 4.1 19.4 ×4.77 ×1.04 4% (2.2) 75% (38.5) 7% (3.7) 14% (7.0) 0% (0.1) match
code_mixed modalities 3.9 18.9 ×4.85 ×1.04 5% (2.4) 73% (38.4) 8% (4.3) 14% (7.6) 0% (0.0) match
math_latex modalities 4.0 18.0 ×4.55 ×1.03 5% (2.6) 70% (38.5) 9% (4.9) 17% (9.1) 0% (0.1) match
deepseek-v4 — deepseek 3-regex split-heavy byte-level BPE · ×4.03 vs v0.23.1 · ×0.98 vs base · decode pending deepseek-v4 speedup deepseek-v4 stage decomposition deepseek-v4 thread scaling deepseek-v4 decode speedup deepseek-v4 decode thread scaling

Memory (RSS MB, load+encode): v0.23.1 62+0 (peak 68) · Pipeline 83+0 (peak 82)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model post Ids
amh_Ethi lang 4.4 33.8 ×7.72 ×1.02 2% (0.7) 0% (0.0) 16% (4.8) 80% (23.4) 2% (0.5) match
arb_Arab lang 4.2 16.2 ×3.82 ×0.97 1% (0.6) 0% (0.0) 6% (3.5) 93% (57.1) 0% (0.1) match
ben_Beng lang 6.3 17.4 ×2.75 ×0.98 1% (0.6) 0% (0.0) 5% (3.1) 91% (52.6) 2% (1.3) match
cmn_Hani lang 3.7 16.2 ×4.34 ×0.99 1% (0.8) 0% (0.0) 6% (3.3) 93% (55.4) 0% (0.0) match
ell_Grek lang 4.8 17.9 ×3.76 ×1.00 1% (0.6) 0% (0.0) 6% (3.5) 92% (51.3) 0% (0.1) match
eng_Latn lang 3.2 12.7 ×4.01 ×0.98 3% (2.2) 0% (0.0) 6% (5.2) 89% (71.4) 2% (1.7) match
heb_Hebr lang 4.0 13.0 ×3.25 ×1.01 1% (0.6) 0% (0.0) 5% (3.7) 93% (72.4) 1% (1.2) match
hin_Deva lang 5.9 21.4 ×3.64 ×0.99 1% (0.6) 0% (0.0) 7% (3.3) 89% (42.2) 3% (1.2) match
jpn_Jpan lang 4.2 17.6 ×4.17 ×0.99 1% (0.7) 0% (0.0) 5% (3.0) 93% (52.0) 0% (0.0) match
kat_Geor lang 6.1 17.4 ×2.87 ×0.96 1% (0.6) 0% (0.0) 5% (3.0) 94% (53.4) 0% (0.0) match
kor_Hang lang 3.6 19.4 ×5.36 ×0.98 1% (0.6) 0% (0.0) 7% (3.7) 91% (46.1) 0% (0.0) match
rus_Cyrl lang 4.4 14.2 ×3.25 ×1.00 1% (0.6) 0% (0.0) 5% (3.3) 93% (65.8) 2% (1.1) match
tam_Taml lang 6.2 16.9 ×2.74 ×0.95 1% (0.6) 0% (0.0) 5% (2.8) 93% (55.1) 1% (0.9) match
tha_Thai lang 6.9 14.1 ×2.04 ×0.98 1% (0.6) 0% (0.0) 3% (2.3) 96% (66.6) 0% (0.0) match
added_normalized_dense modalities 6.1 22.5 ×3.70 ×0.96 2% (0.8) 0% (0.0) 6% (2.8) 92% (40.2) 0% (0.0) match
added_normalized_sparse modalities 5.4 18.3 ×3.41 ×0.94 3% (1.3) 0% (0.0) 7% (3.7) 91% (48.1) 0% (0.0) match
added_special_dense modalities 3.7 34.7 ×9.42 ×0.95 23% (6.3) 2% (0.6) 24% (6.4) 54% (14.4) 0% (0.0) match
added_special_sparse modalities 4.0 20.0 ×5.00 ×0.97 8% (4.0) 0% (0.0) 13% (7.0) 75% (39.5) 4% (2.2) match
agentic-traces modalities 2.9 14.0 ×4.81 ×0.99 3% (1.9) 0% (0.0) 8% (5.6) 89% (62.5) 0% (0.0) match
agentic_swe modalities 3.1 14.8 ×4.86 ×0.99 2% (1.4) 0% (0.0) 6% (3.9) 92% (62.5) 0% (0.0) match
code_mixed modalities 3.6 15.2 ×4.25 ×0.98 2% (1.6) 0% (0.0) 7% (4.7) 87% (59.7) 4% (2.8) match
math_latex modalities 2.9 13.7 ×4.80 ×0.99 3% (2.1) 0% (0.0) 8% (5.6) 89% (64.3) 0% (0.0) match

Pre-tokenize: classify + fsm vs regex engines — ns/byte, lower better. The fsm is the scalar jump-table in both pipe columns; SIMD / scalar is the classify pass (regex pre-tokenizers have no SIMD fsm). ×vs = engine ÷ our pipeline (SIMD / scalar classify); onig & pcre2 (JIT) are C, fancy is pure-Rust fancy-regex, logos is a compile-time DFA lexer (approximate grammar; n/a for deepseek).

Fixture classify SIMD classify scalar pipe (SIMD cls + fsm) pipe (scalar cls + fsm) onig fancy pcre2 logos ×vs onig ×vs fancy ×vs pcre2 ×vs logos
amh_Ethi 2.75 3.09 4.81 5.16 42.1 19.9 9.2 8.8× / 8.2× 4.1× / 3.9× 1.9× / 1.8×
arb_Arab 1.15 2.78 3.50 5.12 49.6 23.1 10.3 14.2× / 9.7× 6.6× / 4.5× 2.9× / 2.0×
ben_Beng 1.60 2.61 3.13 4.14 36.1 15.8 7.6 11.5× / 8.7× 5.0× / 3.8× 2.4× / 1.8×
cmn_Hani 1.12 2.01 3.28 4.16 61.6 33.7 14.9 18.8× / 14.8× 10.3× / 8.1× 4.5× / 3.6×
ell_Grek 0.59 2.81 3.47 5.69 47.5 21.1 9.8 13.7× / 8.4× 6.1× / 3.7× 2.8× / 1.7×
eng_Latn 0.09 1.17 5.17 6.25 66.3 40.1 16.4 12.8× / 10.6× 7.8× / 6.4× 3.2× / 2.6×
heb_Hebr 1.14 2.91 3.70 5.46 51.5 23.6 10.7 13.9× / 9.4× 6.4× / 4.3× 2.9× / 2.0×
hin_Deva 1.51 2.79 3.34 4.62 37.8 18.3 8.6 11.3× / 8.2× 5.5× / 4.0× 2.6× / 1.9×
jpn_Jpan 1.71 3.10 3.00 4.39 53.8 26.8 12.2 17.9× / 12.3× 8.9× / 6.1× 4.1× / 2.8×
kat_Geor 1.54 2.21 3.04 3.70 32.5 15.2 7.3 10.7× / 8.8× 5.0× / 4.1× 2.4× / 2.0×
kor_Hang 1.22 2.48 3.70 4.96 50.5 26.8 12.0 13.6× / 10.2× 7.2× / 5.4× 3.2× / 2.4×
rus_Cyrl 1.17 2.77 3.34 4.94 47.4 20.4 9.6 14.2× / 9.6× 6.1× / 4.1× 2.9× / 1.9×
tam_Taml 0.92 2.61 2.78 4.47 31.9 13.7 6.6 11.5× / 7.1× 4.9× / 3.1× 2.4× / 1.5×
tha_Thai 1.81 2.21 2.32 2.72 26.7 10.2 5.3 11.5× / 9.8× 4.4× / 3.7× 2.3× / 1.9×
added_normalized_dense 0.06 1.18 2.78 3.90 41.9 19.8 9.3 15.1× / 10.7× 7.1× / 5.1× 3.3× / 2.4×
added_normalized_sparse 0.06 1.18 3.67 4.79 48.7 25.6 11.6 13.3× / 10.2× 7.0× / 5.3× 3.2× / 2.4×
added_special_dense 0.06 1.18 6.44 7.56 176.4 98.5 39.6 27.4× / 23.3× 15.3× / 13.0× 6.1× / 5.2×
added_special_sparse 0.06 1.18 6.96 8.08 103.9 58.4 24.2 14.9× / 12.9× 8.4× / 7.2× 3.5× / 3.0×
agentic-traces 0.72 1.18 5.61 6.06 81.5 52.4 20.4 14.5× / 13.4× 9.3× / 8.6× 3.6× / 3.4×
agentic_swe 0.65 1.16 3.95 4.45 97.3 65.9 24.1 24.7× / 21.9× 16.7× / 14.8× 6.1× / 5.4×
code_mixed 0.07 1.16 4.74 5.83 74.5 53.8 19.2 15.7× / 12.8× 11.4× / 9.2× 4.1× / 3.3×
math_latex 0.78 1.18 5.60 6.00 79.3 48.5 19.6 14.2× / 13.2× 8.7× / 8.1× 3.5× / 3.3×
gemma-4 — byte-fallback BPE, Metaspace-style split (gemma-4) · ×1.87 vs v0.23.1 · ×1.09 vs base · decode pending gemma-4 speedup gemma-4 stage decomposition gemma-4 thread scaling gemma-4 decode speedup gemma-4 decode thread scaling

Memory (RSS MB, load+encode): v0.23.1 304+0 (peak 371) · Pipeline 275+0 (peak 371)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model post Ids
amh_Ethi lang 11.3 29.6 ×2.63 ×1.12 2% (0.7) 6% (2.1) 0% (0.0) 92% (31.0) 0% (0.0) match
arb_Arab lang 7.6 13.9 ×1.82 ×1.08 1% (0.6) 4% (2.6) 0% (0.0) 96% (69.6) 0% (0.0) match
ben_Beng lang 11.0 19.2 ×1.76 ×1.11 1% (0.6) 3% (1.7) 0% (0.0) 96% (49.5) 0% (0.0) match
cmn_Hani lang 14.2 38.8 ×2.73 ×1.17 2% (0.6) 1% (0.3) 0% (0.0) 97% (24.6) 0% (0.0) match
ell_Grek lang 8.2 15.7 ×1.92 ×1.08 1% (0.6) 4% (2.5) 0% (0.0) 96% (61.6) 0% (0.0) match
eng_Latn lang 4.2 5.8 ×1.40 ×1.07 1% (2.1) 3% (4.9) 0% (0.0) 96% (164.3) 0% (0.0) match
heb_Hebr lang 8.5 17.7 ×2.07 ×1.08 1% (0.6) 5% (2.6) 0% (0.0) 94% (53.6) 0% (0.0) match
hin_Deva lang 10.5 19.3 ×1.84 ×1.12 1% (0.6) 4% (2.2) 0% (0.0) 95% (49.4) 0% (0.0) match
jpn_Jpan lang 14.3 32.5 ×2.27 ×1.16 2% (0.6) 1% (0.2) 0% (0.0) 97% (30.4) 0% (0.0) match
kat_Geor lang 12.5 26.1 ×2.08 ×1.09 2% (0.6) 4% (1.4) 0% (0.0) 95% (36.8) 0% (0.0) match
kor_Hang lang 10.4 28.2 ×2.70 ×1.14 2% (0.7) 7% (2.6) 0% (0.0) 91% (32.4) 0% (0.0) match
rus_Cyrl lang 7.7 11.6 ×1.50 ×1.12 1% (0.6) 3% (2.3) 0% (0.0) 97% (84.2) 0% (0.0) match
tam_Taml lang 12.0 20.9 ×1.74 ×1.10 1% (0.6) 3% (1.3) 0% (0.0) 96% (46.8) 0% (0.0) match
tha_Thai lang 14.1 25.6 ×1.82 ×1.10 2% (0.6) 2% (0.6) 0% (0.0) 97% (38.2) 0% (0.0) match
added_normalized_dense modalities 5.3 7.7 ×1.46 ×1.07 1% (0.8) 2% (2.8) 0% (0.0) 97% (127.4) 0% (0.0) match
added_normalized_sparse modalities 4.8 6.8 ×1.41 ×1.06 1% (1.4) 3% (4.4) 0% (0.0) 96% (141.9) 0% (0.0) match
added_special_dense modalities 4.8 20.2 ×4.17 ×1.04 19% (8.9) 18% (8.5) 15% (7.3) 48% (22.9) 1% (0.3) match
added_special_sparse modalities 7.2 10.0 ×1.37 ×1.04 5% (5.1) 8% (8.3) 5% (4.8) 82% (81.8) 0% (0.0) match
agentic-traces modalities 4.4 6.6 ×1.49 ×1.08 1% (1.9) 3% (4.5) 0% (0.0) 96% (144.5) 0% (0.0) match
agentic_swe modalities 4.2 7.3 ×1.73 ×1.08 1% (1.3) 5% (7.4) 0% (0.0) 94% (128.5) 0% (0.0) match
code_mixed modalities 4.3 6.9 ×1.59 ×1.08 1% (1.7) 4% (6.0) 0% (0.1) 94% (137.2) 1% (1.6) match
math_latex modalities 4.3 6.2 ×1.46 ×1.11 1% (2.0) 3% (4.6) 0% (0.0) 96% (154.1) 0% (0.0) match
gpt2 — gpt2 ByteLevel regex · ×7.65 vs v0.23.1 · ×1.00 vs base · decode pending gpt2 speedup gpt2 stage decomposition gpt2 thread scaling gpt2 decode speedup gpt2 decode thread scaling

Memory (RSS MB, load+encode): v0.23.1 25+2 (peak 27) · Pipeline 28+0 (peak 28)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model post Ids
amh_Ethi lang 3.9 47.8 ×12.36 ×1.02 3% (0.7) 0% (0.0) 19% (3.6) 78% (15.0) 0% (0.1) match
arb_Arab lang 3.8 25.4 ×6.62 ×0.99 2% (0.6) 0% (0.0) 6% (2.3) 93% (35.6) 0% (0.0) match
ben_Beng lang 2.9 44.6 ×15.12 ×1.03 3% (0.6) 0% (0.0) 14% (3.0) 83% (17.6) 0% (0.0) match
cmn_Hani lang 3.8 28.9 ×7.62 ×1.02 2% (0.6) 0% (0.0) 7% (2.3) 91% (30.7) 0% (0.0) match
ell_Grek lang 4.4 28.2 ×6.47 ×1.00 2% (0.6) 0% (0.0) 6% (2.2) 92% (31.8) 0% (0.0) match
eng_Latn lang 3.6 13.9 ×3.89 ×0.99 3% (2.2) 0% (0.0) 4% (3.0) 93% (66.5) 0% (0.0) match
heb_Hebr lang 4.0 30.2 ×7.59 ×1.05 2% (0.6) 0% (0.0) 7% (2.3) 90% (29.6) 1% (0.2) match
hin_Deva lang 3.1 40.6 ×13.01 ×1.00 3% (0.6) 0% (0.0) 13% (3.0) 86% (20.7) 0% (0.0) match
jpn_Jpan lang 4.6 21.6 ×4.73 ×1.00 1% (0.6) 0% (0.1) 5% (2.3) 102% (47.7) 0% (0.0) match
kat_Geor lang 4.8 67.0 ×13.92 ×0.94 4% (0.7) 0% (0.0) 13% (2.2) 84% (13.7) 0% (0.0) match
kor_Hang lang 3.4 44.1 ×12.86 ×1.01 3% (0.7) 0% (0.0) 11% (2.4) 93% (20.7) 0% (0.0) match
rus_Cyrl lang 4.3 26.7 ×6.24 ×1.00 2% (0.7) 0% (0.2) 5% (2.0) 96% (40.3) 0% (0.0) match
tam_Taml lang 2.7 72.5 ×26.45 ×1.02 5% (0.7) 0% (0.0) 21% (3.0) 72% (10.3) 1% (0.2) match
tha_Thai lang 3.7 37.0 ×10.04 ×1.00 2% (0.6) 0% (0.0) 8% (2.6) 74% (23.4) 15% (4.8) match
added_normalized_dense modalities 6.1 24.5 ×4.05 ×0.98 2% (0.8) 0% (0.0) 3% (1.1) 95% (38.4) 0% (0.0) match
added_normalized_sparse modalities 5.2 21.1 ×4.06 ×0.98 3% (1.4) 0% (0.0) 3% (1.8) 86% (44.0) 8% (4.1) match
added_special_dense modalities 4.2 46.1 ×10.89 ×1.02 25% (5.2) 0% (0.1) 20% (4.2) 53% (11.3) 2% (0.4) match
added_special_sparse modalities 4.3 22.7 ×5.24 ×0.97 7% (3.5) 0% (0.0) 9% (4.7) 71% (35.8) 13% (6.7) match
agentic-traces modalities 3.2 16.3 ×5.05 ×0.99 3% (1.9) 0% (0.0) 7% (4.1) 92% (57.6) 0% (0.0) match
agentic_swe modalities 3.5 24.7 ×7.03 ×1.00 3% (1.4) 0% (0.0) 6% (2.4) 97% (38.7) 0% (0.0) match
code_mixed modalities 3.7 20.4 ×5.57 ×0.98 3% (1.7) 0% (0.0) 6% (2.9) 92% (44.3) 0% (0.0) match
math_latex modalities 3.4 15.2 ×4.51 ×1.00 3% (2.1) 0% (0.0) 5% (3.2) 92% (61.9) 0% (0.0) match

Pre-tokenize: classify + fsm vs regex engines — ns/byte, lower better. The fsm is the scalar jump-table in both pipe columns; SIMD / scalar is the classify pass (regex pre-tokenizers have no SIMD fsm). ×vs = engine ÷ our pipeline (SIMD / scalar classify); onig & pcre2 (JIT) are C, fancy is pure-Rust fancy-regex, logos is a compile-time DFA lexer (approximate grammar; n/a for deepseek).

Fixture classify SIMD classify scalar pipe (SIMD cls + fsm) pipe (scalar cls + fsm) onig fancy pcre2 logos ×vs onig ×vs fancy ×vs pcre2 ×vs logos
amh_Ethi 2.75 3.10 3.62 3.97 28.3 21.1 5.8 4.8 7.8× / 7.1× 5.8× / 5.3× 1.6× / 1.5× 1.3× / 1.2×
arb_Arab 1.15 2.77 2.28 3.91 33.0 26.1 6.8 5.1 14.5× / 8.4× 11.4× / 6.7× 3.0× / 1.7× 2.2× / 1.3×
ben_Beng 1.60 2.62 2.98 4.00 68.2 55.8 13.7 4.0 22.9× / 17.1× 18.8× / 14.0× 4.6× / 3.4× 1.4× / 1.0×
cmn_Hani 1.11 2.01 2.32 3.21 27.1 21.4 5.9 2.4 11.7× / 8.4× 9.3× / 6.7× 2.6× / 1.8× 1.0× / 0.7×
ell_Grek 0.58 2.86 2.16 4.44 29.1 22.1 6.0 4.7 13.5× / 6.6× 10.2× / 5.0× 2.8× / 1.3× 2.2× / 1.1×
eng_Latn 0.11 1.20 2.96 4.06 47.3 52.6 12.3 3.9 16.0× / 11.7× 17.8× / 13.0× 4.1× / 3.0× 1.3× / 1.0×
heb_Hebr 1.15 2.86 2.32 4.03 32.6 28.7 7.0 3.1 14.1× / 8.1× 12.4× / 7.1× 3.0× / 1.7× 1.4× / 0.8×
hin_Deva 1.52 2.82 3.03 4.34 65.8 59.9 13.5 4.7 21.7× / 15.2× 19.7× / 13.8× 4.4× / 3.1× 1.6× / 1.1×
jpn_Jpan 1.70 3.15 2.30 3.75 24.5 21.0 5.4 4.1 10.7× / 6.5× 9.1× / 5.6× 2.4× / 1.4× 1.8× / 1.1×
kat_Geor 1.84 2.41 2.16 2.72 18.4 15.7 4.3 2.1 8.5× / 6.8× 7.3× / 5.8× 2.0× / 1.6× 1.0× / 0.8×
kor_Hang 1.22 2.49 2.41 3.68 35.4 34.2 8.8 3.8 14.7× / 9.6× 14.2× / 9.3× 3.7× / 2.4× 1.6× / 1.0×
rus_Cyrl 1.18 2.76 2.01 3.60 29.9 28.1 6.5 2.7 14.9× / 8.3× 14.0× / 7.8× 3.2× / 1.8× 1.4× / 0.8×
tam_Taml 0.98 2.71 3.01 4.74 73.4 70.6 15.1 4.0 24.4× / 15.5× 23.5× / 14.9× 5.0× / 3.2× 1.3× / 0.8×
tha_Thai 1.76 2.35 2.60 3.19 46.8 35.2 9.0 3.3 18.0× / 14.7× 13.5× / 11.0× 3.5× / 2.8× 1.3× / 1.0×
added_normalized_dense 0.06 1.28 1.13 2.35 30.7 28.4 9.0 2.0 27.2× / 13.1× 25.2× / 12.1× 8.0× / 3.8× 1.8× / 0.9×
added_normalized_sparse 0.09 1.18 1.77 2.86 32.6 31.3 8.7 2.6 18.5× / 11.4× 17.7× / 11.0× 4.9× / 3.1× 1.5× / 0.9×
added_special_dense 0.09 1.18 4.19 5.28 99.0 114.5 20.0 3.1 23.6× / 18.8× 27.3× / 21.7× 4.8× / 3.8× 0.7× / 0.6×
added_special_sparse 0.09 1.18 4.66 5.75 69.7 69.8 17.3 3.8 14.9× / 12.1× 15.0× / 12.1× 3.7× / 3.0× 0.8× / 0.7×
agentic-traces 0.75 1.18 4.11 4.55 61.4 62.1 15.7 4.6 14.9× / 13.5× 15.1× / 13.6× 3.8× / 3.4× 1.1× / 1.0×
agentic_swe 0.66 1.16 2.45 2.95 63.0 69.0 15.2 3.5 25.7× / 21.4× 28.2× / 23.4× 6.2× / 5.2× 1.4× / 1.2×
code_mixed 0.08 1.16 2.95 4.02 61.2 69.8 15.6 4.0 20.7× / 15.2× 23.7× / 17.4× 5.3× / 3.9× 1.4× / 1.0×
math_latex 0.98 1.19 3.25 3.45 54.5 57.2 14.0 4.4 16.8× / 15.8× 17.6× / 16.6× 4.3× / 4.0× 1.4× / 1.3×
gpt-oss — o200k-regex byte-level BPE (gpt-oss) · ×4.83 vs v0.23.1 · ×1.02 vs base · decode pending gpt-oss speedup gpt-oss stage decomposition gpt-oss thread scaling gpt-oss decode speedup gpt-oss decode thread scaling

Memory (RSS MB, load+encode): v0.23.1 241+0 (peak 315) · Pipeline 234+0 (peak 316)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model post Ids
amh_Ethi lang 3.8 23.8 ×6.21 ×1.02 2% (0.7) 0% (0.0) 13% (4.9) 88% (34.1) 0% (0.0) match
arb_Arab lang 4.5 18.1 ×3.99 ×1.03 1% (0.6) 0% (0.0) 6% (3.3) 93% (50.0) 0% (0.0) match
ben_Beng lang 6.6 17.5 ×2.67 ×1.00 1% (0.6) 0% (0.0) 7% (3.7) 95% (51.7) 0% (0.0) match
cmn_Hani lang 4.6 12.3 ×2.67 ×1.07 1% (0.6) 0% (0.0) 5% (3.6) 95% (74.7) 0% (0.0) match
ell_Grek lang 5.1 15.9 ×3.10 ×1.04 1% (0.7) 0% (0.0) 5% (3.2) 93% (57.3) 0% (0.2) match
eng_Latn lang 4.0 42.3 ×10.49 ×1.10 10% (2.1) 0% (0.0) 23% (4.8) 70% (14.3) 0% (0.0) match
heb_Hebr lang 4.8 16.6 ×3.45 ×0.99 1% (0.6) 0% (0.0) 6% (3.4) 97% (58.2) 0% (0.0) match
hin_Deva lang 7.1 27.4 ×3.86 ×1.05 2% (0.6) 0% (0.0) 11% (3.8) 90% (31.7) 0% (0.0) match
jpn_Jpan lang 5.4 12.8 ×2.35 ×1.04 1% (0.6) 0% (0.0) 4% (3.4) 96% (75.6) 0% (0.0) match
kat_Geor lang 6.8 13.9 ×2.05 ×1.01 1% (0.6) 0% (0.0) 4% (2.9) 99% (68.8) 0% (0.0) match
kor_Hang lang 4.1 15.0 ×3.69 ×1.03 1% (0.6) 0% (0.0) 6% (3.7) 93% (59.1) 1% (0.4) match
rus_Cyrl lang 5.0 15.3 ×3.05 ×1.01 1% (0.6) 0% (0.0) 5% (3.2) 97% (62.7) 0% (0.0) match
tam_Taml lang 6.9 13.0 ×1.88 ×1.00 1% (0.6) 0% (0.0) 4% (3.2) 93% (72.2) 2% (1.6) match
tha_Thai lang 7.8 11.4 ×1.46 ×1.00 1% (0.6) 0% (0.0) 4% (3.3) 96% (81.6) 0% (0.0) match
added_normalized_dense modalities 5.5 24.5 ×4.47 ×0.98 2% (0.8) 0% (0.0) 5% (2.2) 93% (40.3) 0% (0.0) match
added_normalized_sparse modalities 5.5 40.7 ×7.40 ×0.99 5% (1.4) 0% (0.0) 12% (3.0) 81% (21.0) 2% (0.6) match
added_special_dense modalities 4.3 74.3 ×17.11 ×0.97 39% (5.2) 0% (0.0) 41% (5.4) 20% (2.6) 0% (0.0) match
added_special_sparse modalities 4.4 72.1 ×16.29 ×0.94 22% (4.8) 0% (0.0) 30% (6.6) 15% (3.4) 40% (8.9) match
agentic-traces modalities 3.7 37.6 ×10.28 ×1.02 8% (1.9) 0% (0.0) 22% (5.1) 70% (16.6) 0% (0.1) match
agentic_swe modalities 3.8 27.7 ×7.23 ×1.02 4% (1.3) 0% (0.0) 10% (3.8) 87% (32.5) 0% (0.0) match
code_mixed modalities 3.8 49.4 ×12.84 ×1.01 9% (1.7) 0% (0.0) 25% (4.6) 66% (12.0) 1% (0.1) match
math_latex modalities 3.5 38.3 ×10.80 ×1.06 9% (2.1) 0% (0.0) 22% (5.1) 72% (16.8) 0% (0.0) match

Pre-tokenize: classify + fsm vs regex engines — ns/byte, lower better. The fsm is the scalar jump-table in both pipe columns; SIMD / scalar is the classify pass (regex pre-tokenizers have no SIMD fsm). ×vs = engine ÷ our pipeline (SIMD / scalar classify); onig & pcre2 (JIT) are C, fancy is pure-Rust fancy-regex, logos is a compile-time DFA lexer (approximate grammar; n/a for deepseek).

Fixture classify SIMD classify scalar pipe (SIMD cls + fsm) pipe (scalar cls + fsm) onig fancy pcre2 logos ×vs onig ×vs fancy ×vs pcre2 ×vs logos
amh_Ethi 2.78 3.11 4.87 5.20 28.6 14.0 7.0 4.8 5.9× / 5.5× 2.9× / 2.7× 1.4× / 1.4× 1.0× / 0.9×
arb_Arab 1.15 2.78 3.30 4.93 32.5 15.9 7.5 5.0 9.9× / 6.6× 4.8× / 3.2× 2.3× / 1.5× 1.5× / 1.0×
ben_Beng 1.64 2.61 3.67 4.64 22.8 10.9 5.4 2.8 6.2× / 4.9× 3.0× / 2.4× 1.5× / 1.2× 0.8× / 0.6×
cmn_Hani 1.13 2.05 3.63 4.56 21.0 11.0 5.5 2.6 5.8× / 4.6× 3.0× / 2.4× 1.5× / 1.2× 0.7× / 0.6×
ell_Grek 0.59 2.82 3.22 5.45 29.5 15.3 7.3 5.1 9.1× / 5.4× 4.8× / 2.8× 2.3× / 1.3× 1.6× / 0.9×
eng_Latn 0.10 1.18 4.77 5.86 40.7 32.1 15.1 4.3 8.5× / 6.9× 6.7× / 5.5× 3.2× / 2.6× 0.9× / 0.7×
heb_Hebr 1.16 2.86 3.39 5.10 32.3 17.2 8.0 2.9 9.5× / 6.3× 5.1× / 3.4× 2.4× / 1.6× 0.9× / 0.6×
hin_Deva 1.52 2.78 3.78 5.05 24.9 13.3 6.3 3.1 6.6× / 4.9× 3.5× / 2.6× 1.7× / 1.3× 0.8× / 0.6×
jpn_Jpan 1.69 3.13 3.38 4.81 20.0 9.3 4.8 3.8 5.9× / 4.2× 2.7× / 1.9× 1.4× / 1.0× 1.1× / 0.8×
kat_Geor 1.54 2.19 2.88 3.53 18.5 10.7 4.5 2.1 6.4× / 5.2× 3.7× / 3.0× 1.6× / 1.3× 0.7× / 0.6×
kor_Hang 1.22 2.47 3.74 4.99 32.0 19.1 8.8 3.7 8.5× / 6.4× 5.1× / 3.8× 2.4× / 1.8× 1.0× / 0.7×
rus_Cyrl 1.16 2.75 3.15 4.74 27.6 15.0 6.6 4.7 8.7× / 5.8× 4.8× / 3.2× 2.1× / 1.4× 1.5× / 1.0×
tam_Taml 0.93 2.63 3.21 4.91 18.5 8.8 4.2 2.9 5.8× / 3.8× 2.7× / 1.8× 1.3× / 0.9× 0.9× / 0.6×
tha_Thai 1.52 2.25 3.26 3.99 12.9 5.9 2.8 2.4 4.0× / 3.2× 1.8× / 1.5× 0.9× / 0.7× 0.7× / 0.6×
added_normalized_dense 0.06 1.18 2.17 3.29 31.0 17.6 11.3 2.2 14.3× / 9.4× 8.1× / 5.3× 5.2× / 3.4× 1.0× / 0.7×
added_normalized_sparse 0.06 1.18 3.04 4.16 33.0 20.9 11.5 3.1 10.9× / 7.9× 6.9× / 5.0× 3.8× / 2.8× 1.0× / 0.7×
added_special_dense 0.06 1.18 5.40 6.52 80.2 67.8 24.1 3.3 14.9× / 12.3× 12.6× / 10.4× 4.5× / 3.7× 0.6× / 0.5×
added_special_sparse 0.06 1.19 6.58 7.72 54.3 41.5 16.8 3.8 8.3× / 7.0× 6.3× / 5.4× 2.6× / 2.2× 0.6× / 0.5×
agentic-traces 0.78 1.18 5.11 5.51 49.2 43.0 17.6 5.0 9.6× / 8.9× 8.4× / 7.8× 3.4× / 3.2× 1.0× / 0.9×
agentic_swe 0.68 1.16 3.81 4.29 49.3 49.6 17.3 3.6 12.9× / 11.5× 13.0× / 11.6× 4.5× / 4.0× 1.0× / 0.8×
code_mixed 0.09 1.15 4.56 5.61 49.0 48.0 17.2 4.3 10.7× / 8.7× 10.5× / 8.6× 3.8× / 3.1× 0.9× / 0.8×
math_latex 0.73 1.19 5.08 5.55 46.5 36.8 15.8 4.6 9.1× / 8.4× 7.2× / 6.6× 3.1× / 2.9× 0.9× / 0.8×
glm-5.2 — cl100k-variant regex byte-level BPE (glm-5.2) · ×6.23 vs v0.23.1 · ×1.01 vs base · decode pending glm-5.2 speedup glm-5.2 stage decomposition glm-5.2 thread scaling glm-5.2 decode speedup glm-5.2 decode thread scaling

Memory (RSS MB, load+encode): v0.23.1 169+0 (peak 231) · Pipeline 170+0 (peak 232)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model post Ids
amh_Ethi lang 4.2 44.9 ×10.80 ×1.00 6% (1.2) 0% (0.0) 18% (3.7) 77% (15.8) 0% (0.0) match
arb_Arab lang 4.6 17.4 ×3.77 ×1.02 2% (1.2) 0% (0.0) 4% (2.4) 94% (53.5) 0% (0.0) match
ben_Beng lang 4.3 27.2 ×6.26 ×1.00 3% (1.2) 0% (0.0) 9% (3.2) 88% (32.2) 0% (0.0) match
cmn_Hani lang 4.9 14.2 ×2.89 ×1.04 2% (1.2) 0% (0.0) 4% (2.4) 94% (64.0) 0% (0.3) match
ell_Grek lang 5.1 19.6 ×3.85 ×1.03 2% (1.2) 0% (0.0) 5% (2.2) 93% (46.4) 0% (0.1) match
eng_Latn lang 4.0 43.8 ×10.97 ×1.04 14% (2.7) 0% (0.0) 16% (3.1) 77% (14.8) 0% (0.0) match
heb_Hebr lang 4.4 25.1 ×5.76 ×1.01 3% (1.2) 0% (0.1) 6% (2.3) 91% (34.8) 0% (0.0) match
hin_Deva lang 4.0 29.3 ×7.37 ×0.96 4% (1.2) 0% (0.0) 10% (3.3) 87% (28.5) 0% (0.0) match
jpn_Jpan lang 5.7 15.1 ×2.65 ×0.99 2% (1.2) 0% (0.0) 4% (2.2) 95% (59.0) 0% (0.0) match
kat_Geor lang 6.5 22.3 ×3.41 ×1.06 3% (1.2) 0% (0.0) 5% (2.1) 92% (40.2) 0% (0.2) match
kor_Hang lang 4.1 18.0 ×4.41 ×0.99 2% (1.2) 0% (0.0) 5% (2.6) 93% (49.4) 0% (0.0) match
rus_Cyrl lang 5.2 18.3 ×3.53 ×1.00 2% (1.2) 0% (0.0) 4% (2.2) 93% (49.3) 0% (0.2) match
tam_Taml lang 3.9 36.2 ×9.38 ×1.03 4% (1.2) 0% (0.0) 10% (2.7) 86% (23.7) 0% (0.0) match
tha_Thai lang 5.0 20.9 ×4.18 ×0.97 3% (1.2) 0% (0.0) 6% (2.6) 92% (42.6) 0% (0.0) match
added_normalized_dense modalities 6.2 25.9 ×4.19 ×0.99 3% (1.3) 0% (0.0) 3% (1.1) 94% (35.8) 0% (0.0) match
added_normalized_sparse modalities 5.4 40.3 ×7.44 ×0.99 8% (2.0) 0% (0.0) 8% (1.9) 84% (20.0) 0% (0.1) match
added_special_dense modalities 4.2 48.9 ×11.77 ×1.05 60% (11.9) 0% (0.0) 27% (5.3) 13% (2.6) 0% (0.0) match
added_special_sparse modalities 4.4 58.1 ×13.35 ×1.02 41% (6.7) 0% (0.0) 32% (5.2) 30% (4.8) 0% (0.0) match
agentic-traces modalities 3.5 35.2 ×10.15 ×0.99 10% (2.6) 0% (0.0) 15% (3.8) 76% (19.3) 0% (0.0) match
agentic_swe modalities 3.8 27.3 ×7.16 ×1.00 6% (2.0) 0% (0.0) 8% (2.9) 87% (30.5) 0% (0.0) match
code_mixed modalities 4.0 44.7 ×11.20 ×1.01 11% (2.3) 0% (0.0) 17% (3.4) 71% (14.3) 0% (0.1) match
math_latex modalities 3.8 38.6 ×10.26 ×1.03 11% (2.6) 0% (0.1) 15% (3.4) 74% (17.2) 0% (0.0) match

Pre-tokenize: classify + fsm vs regex engines — ns/byte, lower better. The fsm is the scalar jump-table in both pipe columns; SIMD / scalar is the classify pass (regex pre-tokenizers have no SIMD fsm). ×vs = engine ÷ our pipeline (SIMD / scalar classify); onig & pcre2 (JIT) are C, fancy is pure-Rust fancy-regex, logos is a compile-time DFA lexer (approximate grammar; n/a for deepseek).

Fixture classify SIMD classify scalar pipe (SIMD cls + fsm) pipe (scalar cls + fsm) onig fancy pcre2 logos ×vs onig ×vs fancy ×vs pcre2 ×vs logos
amh_Ethi 2.75 3.15 3.69 4.09 27.3 16.2 5.9 4.8 7.4× / 6.7× 4.4× / 4.0× 1.6× / 1.4× 1.3× / 1.2×
arb_Arab 1.15 2.70 2.37 3.92 30.9 19.1 6.9 5.2 13.0× / 7.9× 8.1× / 4.9× 2.9× / 1.8× 2.2× / 1.3×
ben_Beng 1.60 2.62 3.18 4.20 45.5 28.5 10.3 3.7 14.3× / 10.8× 9.0× / 6.8× 3.2× / 2.4× 1.2× / 0.9×
cmn_Hani 1.12 2.05 2.42 3.34 19.8 11.6 4.7 2.4 8.2× / 5.9× 4.8× / 3.5× 1.9× / 1.4× 1.0× / 0.7×
ell_Grek 0.59 2.81 2.25 4.47 28.4 17.5 6.2 4.8 12.7× / 6.4× 7.8× / 3.9× 2.7× / 1.4× 2.1× / 1.1×
eng_Latn 0.10 1.19 3.11 4.19 44.1 33.3 12.6 3.8 14.2× / 10.5× 10.7× / 7.9× 4.1× / 3.0× 1.2× / 0.9×
heb_Hebr 1.15 2.86 2.35 4.06 30.5 19.9 7.0 3.1 13.0× / 7.5× 8.5× / 4.9× 3.0× / 1.7× 1.3× / 0.8×
hin_Deva 1.51 2.78 3.27 4.54 48.0 30.2 11.4 4.0 14.7× / 10.6× 9.2× / 6.6× 3.5× / 2.5× 1.2× / 0.9×
jpn_Jpan 1.70 3.10 2.21 3.61 18.4 10.0 4.0 3.8 8.3× / 5.1× 4.5× / 2.8× 1.8× / 1.1× 1.7× / 1.1×
kat_Geor 1.54 2.21 2.13 2.80 16.9 11.1 4.2 2.0 7.9× / 6.0× 5.2× / 4.0× 1.9× / 1.5× 0.9× / 0.7×
kor_Hang 1.22 2.51 2.55 3.83 31.1 21.4 7.5 3.7 12.2× / 8.1× 8.4× / 5.6× 2.9× / 2.0× 1.5× / 1.0×
rus_Cyrl 1.17 2.77 2.20 3.79 27.0 16.2 5.9 2.4 12.3× / 7.1× 7.4× / 4.3× 2.7× / 1.6× 1.1× / 0.6×
tam_Taml 0.92 2.63 2.74 4.45 44.7 26.8 10.1 3.4 16.3× / 10.0× 9.8× / 6.0× 3.7× / 2.3× 1.2× / 0.8×
tha_Thai 1.52 2.32 2.61 3.41 28.1 16.0 6.7 3.0 10.8× / 8.2× 6.1× / 4.7× 2.6× / 2.0× 1.1× / 0.9×
added_normalized_dense 0.07 1.18 1.11 2.21 23.2 17.4 6.4 2.0 21.0× / 10.5× 15.7× / 7.9× 5.8× / 2.9× 1.8× / 0.9×
added_normalized_sparse 0.06 1.18 1.93 3.04 31.0 22.2 8.7 2.7 16.1× / 10.2× 11.5× / 7.3× 4.5× / 2.9× 1.4× / 0.9×
added_special_dense 0.07 1.18 5.27 6.38 94.1 72.9 21.3 3.3 17.9× / 14.8× 13.9× / 11.4× 4.0× / 3.3× 0.6× / 0.5×
added_special_sparse 0.07 1.18 5.18 6.29 60.5 45.9 15.3 3.7 11.7× / 9.6× 8.9× / 7.3× 2.9× / 2.4× 0.7× / 0.6×
agentic-traces 0.74 1.18 3.80 4.25 53.0 45.8 15.1 4.6 13.9× / 12.5× 12.0× / 10.8× 4.0× / 3.6× 1.2× / 1.1×
agentic_swe 0.67 1.18 2.94 3.45 54.9 52.1 15.7 3.5 18.7× / 15.9× 17.7× / 15.1× 5.4× / 4.6× 1.2× / 1.0×
code_mixed 0.07 1.17 3.39 4.49 52.5 51.1 15.5 4.0 15.5× / 11.7× 15.1× / 11.4× 4.6× / 3.5× 1.2× / 0.9×
math_latex 0.71 1.21 3.43 3.93 51.1 39.3 14.4 4.2 14.9× / 13.0× 11.5× / 10.0× 4.2× / 3.7× 1.2× / 1.1×
llama-2 — model-bounded BPE, no pre-tokenizer · ×3.82 vs v0.23.1 · ×1.05 vs base · decode pending llama-2 speedup llama-2 stage decomposition llama-2 thread scaling llama-2 decode speedup llama-2 decode thread scaling

Memory (RSS MB, load+encode): v0.23.1 18+0 (peak 23) · Pipeline 24+0 (peak 24)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model post Ids
amh_Ethi lang 4.6 44.7 ×9.70 ×1.11 0% (0.0) 12% (2.8) 0% (0.0) 87% (20.0) 0% (0.1) match
arb_Arab lang 10.2 50.8 ×5.00 ×1.08 0% (0.0) 16% (3.2) 0% (0.0) 84% (16.8) 0% (0.1) match
ben_Beng lang 10.9 86.9 ×7.97 ×1.12 0% (0.0) 18% (2.2) 0% (0.0) 81% (9.6) 0% (0.0) match
cmn_Hani lang 9.0 66.7 ×7.39 ×1.11 0% (0.1) 3% (0.4) 0% (0.0) 96% (14.1) 0% (0.0) match
ell_Grek lang 10.2 59.1 ×5.79 ×1.07 0% (0.0) 18% (3.2) 0% (0.0) 82% (14.0) 0% (0.0) match
eng_Latn lang 4.0 6.1 ×1.51 ×0.98 0% (0.0) 4% (6.9) 0% (0.0) 96% (158.6) 0% (0.0) match
heb_Hebr lang 10.0 63.1 ×6.33 ×1.08 0% (0.0) 20% (3.3) 0% (0.0) 80% (13.0) 0% (0.0) match
hin_Deva lang 12.0 82.2 ×6.84 ×1.14 0% (0.0) 23% (2.8) 0% (0.0) 77% (9.5) 0% (0.0) match
jpn_Jpan lang 12.7 87.8 ×6.92 ×1.13 0% (0.0) 3% (0.4) 0% (0.0) 97% (10.7) 0% (0.0) match
kat_Geor lang 14.2 94.5 ×6.66 ×1.13 0% (0.0) 18% (1.9) 0% (0.0) 82% (8.7) 0% (0.0) match
kor_Hang lang 7.2 53.0 ×7.39 ×1.10 0% (0.1) 17% (3.3) 0% (0.0) 83% (15.6) 0% (0.0) match
rus_Cyrl lang 7.5 16.3 ×2.17 ×1.03 0% (0.0) 5% (2.8) 0% (0.0) 96% (58.4) 0% (0.0) match
tam_Taml lang 12.4 93.7 ×7.53 ×1.12 0% (0.0) 16% (1.7) 0% (0.0) 84% (8.9) 0% (0.0) match
tha_Thai lang 14.8 88.5 ×5.97 ×1.09 0% (0.0) 9% (1.0) 0% (0.0) 92% (10.2) 0% (0.0) match
added_normalized_dense modalities 4.8 8.4 ×1.76 ×0.98 0% (0.1) 3% (3.8) 0% (0.0) 96% (113.7) 1% (1.1) match
added_normalized_sparse modalities 4.6 7.1 ×1.56 ×0.97 0% (0.0) 4% (6.1) 0% (0.0) 96% (136.2) 0% (0.0) match
added_special_dense modalities 3.8 19.8 ×5.22 ×1.00 10% (5.0) 32% (15.2) 4% (1.7) 54% (25.6) 0% (0.2) match
added_special_sparse modalities 5.2 10.2 ×1.94 ×1.01 2% (2.2) 14% (13.9) 1% (0.5) 83% (79.6) 0% (0.2) match
agentic-traces modalities 4.4 6.7 ×1.53 ×0.97 0% (0.1) 4% (6.2) 0% (0.0) 95% (141.6) 1% (0.9) match
agentic_swe modalities 3.9 6.6 ×1.69 ×0.96 0% (0.0) 6% (9.7) 0% (0.0) 95% (144.1) 0% (0.0) match
code_mixed modalities 4.1 6.7 ×1.64 ×0.97 0% (0.0) 5% (7.9) 0% (0.0) 95% (144.3) 0% (0.0) match
math_latex modalities 4.3 6.4 ×1.50 ×0.99 0% (0.1) 4% (6.4) 0% (0.0) 96% (149.7) 0% (0.0) match
llama-3 — cl100k-regex byte-level BPE (llama-3), single regex · ×6.87 vs v0.23.1 · ×1.00 vs base · decode pending llama-3 speedup llama-3 stage decomposition llama-3 thread scaling llama-3 decode speedup llama-3 decode thread scaling

Memory (RSS MB, load+encode): v0.23.1 73+0 (peak 95) · Pipeline 93+0 (peak 95)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model post Ids
amh_Ethi lang 4.3 50.0 ×11.69 ×1.05 4% (0.7) 0% (0.0) 21% (3.7) 75% (13.1) 0% (0.0) match
arb_Arab lang 4.6 17.5 ×3.83 ×1.00 1% (0.6) 0% (0.0) 4% (2.4) 94% (52.2) 1% (0.3) match
ben_Beng lang 4.1 31.8 ×7.82 ×1.00 2% (0.6) 0% (0.0) 10% (3.1) 87% (27.1) 1% (0.2) match
cmn_Hani lang 5.2 16.5 ×3.18 ×1.00 1% (0.6) 0% (0.0) 4% (2.4) 96% (56.1) 0% (0.0) match
ell_Grek lang 5.3 19.0 ×3.60 ×0.98 1% (0.6) 0% (0.0) 4% (2.2) 94% (47.9) 1% (0.4) match
eng_Latn lang 4.3 45.9 ×10.78 ×1.07 11% (2.1) 0% (0.0) 16% (3.1) 74% (14.0) 0% (0.0) match
heb_Hebr lang 4.4 25.5 ×5.79 ×1.02 2% (0.6) 0% (0.0) 6% (2.3) 94% (35.1) 0% (0.0) match
hin_Deva lang 4.5 77.3 ×17.11 ×1.02 5% (0.6) 0% (0.0) 28% (3.3) 67% (7.8) 0% (0.0) match
jpn_Jpan lang 5.8 16.1 ×2.76 ×1.01 1% (0.6) 0% (0.0) 4% (2.2) 95% (56.9) 1% (0.3) match
kat_Geor lang 5.8 37.6 ×6.49 ×1.08 2% (0.6) 0% (0.0) 8% (2.1) 89% (22.4) 0% (0.0) match
kor_Hang lang 4.2 18.8 ×4.52 ×1.05 1% (0.6) 0% (0.0) 5% (2.5) 94% (48.1) 0% (0.1) match
rus_Cyrl lang 5.0 16.2 ×3.24 ×1.03 1% (0.6) 0% (0.0) 4% (2.2) 97% (57.0) 0% (0.0) match
tam_Taml lang 4.1 35.1 ×8.65 ×1.01 2% (0.6) 0% (0.0) 10% (2.7) 88% (24.5) 0% (0.0) match
tha_Thai lang 5.2 20.0 ×3.83 ×0.99 1% (0.6) 0% (0.0) 5% (2.6) 94% (44.9) 0% (0.0) match
added_normalized_dense modalities 6.2 26.2 ×4.26 ×0.97 2% (0.8) 0% (0.0) 3% (1.0) 96% (36.0) 0% (0.0) match
added_normalized_sparse modalities 5.5 41.3 ×7.52 ×0.96 6% (1.4) 0% (0.0) 8% (2.0) 86% (20.4) 0% (0.0) match
added_special_dense modalities 4.1 69.4 ×16.78 ×0.92 39% (5.0) 1% (0.1) 40% (5.1) 22% (2.8) 0% (0.0) match
added_special_sparse modalities 4.4 70.1 ×15.80 ×0.96 25% (3.3) 0% (0.0) 39% (5.1) 37% (4.8) 0% (0.0) match
agentic-traces modalities 3.6 37.0 ×10.35 ×1.00 8% (1.9) 0% (0.0) 16% (3.8) 77% (18.8) 0% (0.0) match
agentic_swe modalities 4.0 27.2 ×6.72 ×0.96 4% (1.3) 0% (0.0) 9% (2.9) 87% (30.0) 0% (0.0) match
code_mixed modalities 4.2 46.5 ×11.07 ×1.01 8% (1.7) 0% (0.0) 17% (3.4) 72% (14.2) 2% (0.5) match
math_latex modalities 3.7 39.6 ×10.64 ×0.98 9% (2.0) 0% (0.1) 16% (3.4) 76% (16.9) 0% (0.0) match

Pre-tokenize: classify + fsm vs regex engines — ns/byte, lower better. The fsm is the scalar jump-table in both pipe columns; SIMD / scalar is the classify pass (regex pre-tokenizers have no SIMD fsm). ×vs = engine ÷ our pipeline (SIMD / scalar classify); onig & pcre2 (JIT) are C, fancy is pure-Rust fancy-regex, logos is a compile-time DFA lexer (approximate grammar; n/a for deepseek).

Fixture classify SIMD classify scalar pipe (SIMD cls + fsm) pipe (scalar cls + fsm) onig fancy pcre2 logos ×vs onig ×vs fancy ×vs pcre2 ×vs logos
amh_Ethi 2.75 3.10 3.68 4.02 26.0 16.1 5.9 4.8 7.1× / 6.5× 4.4× / 4.0× 1.6× / 1.5× 1.3× / 1.2×
arb_Arab 1.15 2.78 2.36 3.99 29.3 20.3 6.8 5.2 12.4× / 7.4× 8.6× / 5.1× 2.9× / 1.7× 2.2× / 1.3×
ben_Beng 1.60 2.61 3.12 4.13 42.9 29.7 10.2 3.7 13.8× / 10.4× 9.5× / 7.2× 3.3× / 2.5× 1.2× / 0.9×
cmn_Hani 1.12 2.03 2.39 3.31 19.0 11.7 4.7 2.5 8.0× / 5.7× 4.9× / 3.5× 2.0× / 1.4× 1.0× / 0.7×
ell_Grek 0.58 2.76 2.23 4.40 26.8 17.5 6.1 4.8 12.0× / 6.1× 7.8× / 4.0× 2.7× / 1.4× 2.1× / 1.1×
eng_Latn 0.10 1.16 3.14 4.21 41.6 34.4 12.3 3.8 13.2× / 9.9× 11.0× / 8.2× 3.9× / 2.9× 1.2× / 0.9×
heb_Hebr 1.15 2.86 2.34 4.06 28.8 19.6 6.9 3.1 12.3× / 7.1× 8.4× / 4.8× 2.9× / 1.7× 1.3× / 0.8×
hin_Deva 1.53 2.80 3.27 4.54 45.1 30.3 11.1 4.0 13.8× / 9.9× 9.3× / 6.7× 3.4× / 2.5× 1.2× / 0.9×
jpn_Jpan 1.70 3.18 2.19 3.66 17.8 10.0 4.0 3.8 8.1× / 4.9× 4.5× / 2.7× 1.9× / 1.1× 1.7× / 1.0×
kat_Geor 1.54 2.18 2.10 2.74 16.0 11.3 4.2 2.0 7.6× / 5.8× 5.4× / 4.1× 2.0× / 1.5× 1.0× / 0.7×
kor_Hang 1.23 2.49 2.53 3.79 29.4 21.1 7.4 3.7 11.6× / 7.8× 8.3× / 5.6× 2.9× / 1.9× 1.5× / 1.0×
rus_Cyrl 1.17 2.69 2.18 3.71 25.6 16.7 5.8 2.6 11.7× / 6.9× 7.7× / 4.5× 2.7× / 1.6× 1.2× / 0.7×
tam_Taml 0.92 2.65 2.72 4.45 42.1 27.0 9.9 3.3 15.5× / 9.5× 9.9× / 6.1× 3.7× / 2.2× 1.2× / 0.8×
tha_Thai 1.53 2.21 2.58 3.27 26.7 16.4 6.7 3.0 10.3× / 8.2× 6.4× / 5.0× 2.6× / 2.0× 1.2× / 0.9×
added_normalized_dense 0.06 1.18 1.04 2.16 22.0 16.7 6.5 2.0 21.0× / 10.2× 16.0× / 7.7× 6.2× / 3.0× 1.9× / 0.9×
added_normalized_sparse 0.06 1.18 1.99 3.11 29.4 22.4 8.7 2.7 14.8× / 9.5× 11.3× / 7.2× 4.4× / 2.8× 1.3× / 0.9×
added_special_dense 0.06 1.18 5.07 6.19 89.2 72.2 21.5 3.3 17.6× / 14.4× 14.2× / 11.7× 4.2× / 3.5× 0.7× / 0.5×
added_special_sparse 0.06 1.18 5.07 6.19 57.3 45.6 15.0 3.5 11.3× / 9.3× 9.0× / 7.4× 3.0× / 2.4× 0.7× / 0.6×
agentic-traces 0.73 1.20 3.83 4.30 49.9 44.5 15.0 4.6 13.1× / 11.6× 11.6× / 10.4× 3.9× / 3.5× 1.2× / 1.1×
agentic_swe 0.66 1.18 2.95 3.47 52.1 52.7 15.6 3.4 17.7× / 15.0× 17.9× / 15.2× 5.3× / 4.5× 1.2× / 1.0×
code_mixed 0.07 1.17 3.38 4.48 50.2 51.7 15.3 4.0 14.9× / 11.2× 15.3× / 11.5× 4.5× / 3.4× 1.2× / 0.9×
math_latex 0.93 1.19 3.45 3.70 48.1 39.9 14.1 4.2 13.9× / 13.0× 11.6× / 10.8× 4.1× / 3.8× 1.2× / 1.1×
mistral-small-4 — tekken byte-level BPE, 1k added specials (mistral-small-4) · ×5.40 vs v0.23.1 · ×1.01 vs base · decode pending mistral-small-4 speedup mistral-small-4 stage decomposition mistral-small-4 thread scaling mistral-small-4 decode speedup mistral-small-4 decode thread scaling

Memory (RSS MB, load+encode): v0.23.1 152+0 (peak 194) · Pipeline 109+0 (peak 195)

Fixture Group v0.23.1 MB/s Pipeline MB/s Speedup Δ base added-token normalize pre-tokenize model post Ids
amh_Ethi lang 3.6 44.8 ×12.36 ×1.06 6% (1.2) 0% (0.0) 24% (4.6) 70% (13.5) 0% (0.0) match
arb_Arab lang 4.6 21.7 ×4.73 ×1.04 3% (1.2) 0% (0.0) 7% (3.2) 90% (39.1) 0% (0.0) match
ben_Beng lang 6.6 19.0 ×2.86 ×1.01 2% (1.2) 0% (0.0) 7% (3.6) 91% (46.7) 0% (0.0) match
cmn_Hani lang 4.8 17.4 ×3.61 ×1.02 2% (1.2) 0% (0.0) 6% (3.4) 92% (49.9) 0% (0.0) match
ell_Grek lang 5.1 18.4 ×3.61 ×0.99 2% (1.2) 0% (0.0) 6% (3.2) 92% (48.2) 0% (0.0) match
eng_Latn lang 3.8 38.7 ×10.15 ×1.03 12% (2.8) 0% (0.0) 20% (4.6) 69% (15.5) 0% (0.0) match
heb_Hebr lang 4.2 16.5 ×3.92 ×0.98 2% (1.2) 0% (0.0) 6% (3.3) 92% (52.5) 0% (0.1) match
hin_Deva lang 6.5 26.0 ×3.99 ×1.00 3% (1.2) 0% (0.0) 10% (3.7) 87% (31.8) 0% (0.0) match
jpn_Jpan lang 5.4 16.3 ×2.99 ×0.99 2% (1.2) 0% (0.0) 5% (3.2) 93% (54.8) 0% (0.0) match
kat_Geor lang 6.4 14.6 ×2.30 ×0.97 2% (1.2) 0% (0.0) 4% (2.9) 94% (63.0) 0% (0.0) match
kor_Hang lang 4.0 19.2 ×4.83 ×1.06 2% (1.2) 0% (0.0) 7% (3.6) 90% (44.3) 0% (0.1) match
rus_Cyrl lang 4.7 16.0 ×3.37 ×1.01 2% (1.2) 0% (0.0) 5% (3.1) 94% (55.8) 0% (0.0) match
tam_Taml lang 6.5 15.0 ×2.31 ×1.02 2% (1.2) 0% (0.0) 5% (3.2) 94% (61.4) 0% (0.0) match
tha_Thai lang 7.9 14.6 ×1.84 ×0.99 2% (1.2) 0% (0.0) 5% (3.2) 93% (62.0) 0% (0.0) match
added_normalized_dense modalities 5.8 27.0 ×4.70 ×0.93 4% (1.3) 0% (0.0) 6% (2.1) 91% (31.5) 0% (0.0) match
added_normalized_sparse modalities 5.4 42.6 ×7.91 ×0.99 8% (1.9) 0% (0.0) 14% (3.1) 78% (18.2) 0% (0.0) match
added_special_dense modalities 4.4 71.4 ×16.18 ×1.00 38% (5.1) 0% (0.0) 41% (5.3) 20% (2.7) 0% (0.1) match
added_special_sparse modalities 4.6 63.9 ×13.91 ×0.98 26% (3.7) 0% (0.0) 40% (5.7) 35% (5.0) 0% (0.0) match
agentic-traces modalities 3.4 34.7 ×10.17 ×1.04 9% (2.5) 0% (0.0) 19% (5.0) 72% (19.3) 0% (0.1) match
agentic_swe modalities 3.5 25.0 ×7.22 ×1.00 5% (1.9) 0% (0.0) 10% (3.8) 85% (32.8) 0% (0.0) match
code_mixed modalities 3.7 43.4 ×11.79 ×1.02 10% (2.2) 0% (0.0) 21% (4.4) 70% (14.8) 0% (0.0) match
math_latex modalities 3.6 40.0 ×11.06 ×1.02 12% (2.6) 0% (0.0) 22% (4.8) 68% (15.2) 0% (0.0) match

Pre-tokenize: classify + fsm vs regex engines — ns/byte, lower better. The fsm is the scalar jump-table in both pipe columns; SIMD / scalar is the classify pass (regex pre-tokenizers have no SIMD fsm). ×vs = engine ÷ our pipeline (SIMD / scalar classify); onig & pcre2 (JIT) are C, fancy is pure-Rust fancy-regex, logos is a compile-time DFA lexer (approximate grammar; n/a for deepseek).

Fixture classify SIMD classify scalar pipe (SIMD cls + fsm) pipe (scalar cls + fsm) onig fancy pcre2 logos ×vs onig ×vs fancy ×vs pcre2 ×vs logos
amh_Ethi 2.81 3.10 4.64 4.93 27.4 14.5 6.8 5.9× / 5.5× 3.1× / 2.9× 1.5× / 1.4×
arb_Arab 1.15 2.80 3.21 4.86 31.6 16.4 7.3 9.8× / 6.5× 5.1× / 3.4× 2.3× / 1.5×
ben_Beng 1.60 2.65 3.63 4.68 22.1 10.7 5.3 6.1× / 4.7× 2.9× / 2.3× 1.5× / 1.1×
cmn_Hani 1.13 2.02 3.39 4.29 20.9 11.5 5.5 6.2× / 4.9× 3.4× / 2.7× 1.6× / 1.3×
ell_Grek 0.59 2.82 3.18 5.41 27.6 15.2 6.8 8.7× / 5.1× 4.8× / 2.8× 2.1× / 1.3×
eng_Latn 0.16 1.46 4.57 5.87 38.9 30.9 13.8 8.5× / 6.6× 6.8× / 5.3× 3.0× / 2.3×
heb_Hebr 1.15 2.86 3.27 4.98 30.5 17.5 7.7 9.3× / 6.1× 5.4× / 3.5× 2.4× / 1.6×
hin_Deva 1.51 2.87 3.71 5.07 23.7 13.1 6.1 6.4× / 4.7× 3.5× / 2.6× 1.6× / 1.2×
jpn_Jpan 1.71 3.17 3.16 4.62 19.7 9.4 4.8 6.2× / 4.3× 3.0× / 2.0× 1.5× / 1.0×
kat_Geor 1.54 2.23 2.94 3.63 17.9 10.1 4.5 6.1× / 4.9× 3.4× / 2.8× 1.5× / 1.2×
kor_Hang 1.22 2.49 3.60 4.87 30.8 19.2 8.7 8.6× / 6.3× 5.3× / 3.9× 2.4× / 1.8×
rus_Cyrl 1.17 2.76 3.07 4.67 26.9 14.9 6.5 8.7× / 5.8× 4.8× / 3.2× 2.1× / 1.4×
tam_Taml 0.93 2.60 3.18 4.85 17.9 8.8 4.2 5.6× / 3.7× 2.8× / 1.8× 1.3× / 0.9×
tha_Thai 1.51 2.21 3.22 3.91 12.8 5.9 2.8 4.0× / 3.3× 1.8× / 1.5× 0.9× / 0.7×
added_normalized_dense 0.06 1.18 2.09 3.21 28.8 17.2 11.6 13.8× / 9.0× 8.2× / 5.4× 5.5× / 3.6×
added_normalized_sparse 0.06 1.18 3.14 4.26 30.4 20.3 11.5 9.7× / 7.1× 6.5× / 4.8× 3.7× / 2.7×
added_special_dense 0.06 1.18 5.34 6.46 76.8 67.7 23.4 14.4× / 11.9× 12.7× / 10.5× 4.4× / 3.6×
added_special_sparse 0.06 1.18 5.68 6.80 49.5 41.3 16.3 8.7× / 7.3× 7.3× / 6.1× 2.9× / 2.4×
agentic-traces 0.80 1.18 5.03 5.40 49.2 44.1 17.7 9.8× / 9.1× 8.8× / 8.2× 3.5× / 3.3×
agentic_swe 0.66 1.17 3.81 4.31 53.6 55.2 18.6 14.1× / 12.4× 14.5× / 12.8× 4.9× / 4.3×
code_mixed 0.07 1.17 4.39 5.50 48.9 48.1 17.3 11.1× / 8.9× 11.0× / 8.8× 3.9× / 3.2×
math_latex 0.72 1.19 4.83 5.30 45.9 36.8 16.1 9.5× / 8.7× 7.6× / 6.9× 3.3× / 3.0×
Not yet supported: t5-base
t5-base not supported

`PipelineTokenizer::encode` built a fresh scratch every call, so every encode
started by asking the allocator for buffers the last one had just freed. They are
now borrowed from a pool held by the tokenizer and handed back on drop.

A tokenizer encodes through `&self`, so the pool has to cope with several threads
being in it at once: it keeps a pile of scratches behind a mutex, taken and
returned one at a time. The lock is only held while moving a scratch in or out,
never while encoding. One thread encoding in a loop gets the same scratch every
time; N threads encoding at once leave N behind for later calls to use.

Nothing needs clearing between calls: every model empties the buffers it uses
where it uses them (`merge_all` drains the merge queue, `merge_word` clears the
word, WordPiece clears its candidate), which is already what makes one scratch
safe across the many words of a single encode.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@SBrandeis SBrandeis changed the title perf(scratch): reuse Scracth buffers across encode() calls perf(scratch): reuse Scratch buffers across encode() calls Jul 29, 2026
@SBrandeis
SBrandeis marked this pull request as ready for review July 29, 2026 14:14
SBrandeis added a commit that referenced this pull request Jul 31, 2026
A set-associative cache keyed on the word bytes, plus a scratch pile
reused across encode calls, so BPE, WordPiece and Unigram all stop
re-tokenizing words they have already seen.

Squashed from feat/bpe-cache-min, which also carries #2261
(perf(scratch): reuse Scratch buffers across encode() calls).

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm and clean ty!

for _ in 0..1000 {
pipeline.encode("hello", false).unwrap();
}
assert_eq!(pipeline.scratch_pool.len(), after_burst);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should check they are not empty? / properly modified?

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.

3 participants