add wssplit bench
Browse files- benches/wssplit.rs +123 -0
benches/wssplit.rs
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//! WhitespaceSplit: SIMD boundary-detection (`whitespace_split_simd`) vs the scalar generic core
|
| 2 |
+
//! (`whitespace_split_scalar`), both checked byte-for-byte against a `char::is_whitespace` ground
|
| 3 |
+
//! truth. Whitespace-split is the simplest pre-tokenizer (one class, Removed) → the cleanest SIMD win.
|
| 4 |
+
//!
|
| 5 |
+
//! Run: cargo bench --bench wssplit
|
| 6 |
+
use fast_split::classify::{Atoms, classify};
|
| 7 |
+
use fast_split::fsm::{Span, whitespace_split_scalar, whitespace_split_simd};
|
| 8 |
+
use std::hint::black_box;
|
| 9 |
+
use std::time::Instant;
|
| 10 |
+
|
| 11 |
+
const CORPORA: &[(&str, &str)] = &[
|
| 12 |
+
("English", "../data/big.txt"),
|
| 13 |
+
("French", "benches/data/fr.txt"),
|
| 14 |
+
("Russian", "benches/data/ru.txt"),
|
| 15 |
+
("Greek", "benches/data/el.txt"),
|
| 16 |
+
("Arabic", "benches/data/ar.txt"),
|
| 17 |
+
("Hindi", "benches/data/hi.txt"),
|
| 18 |
+
("Thai", "benches/data/th.txt"),
|
| 19 |
+
("Chinese", "benches/data/zh.txt"),
|
| 20 |
+
("Japanese", "../data/unigram_wagahaiwa_nekodearu.txt"),
|
| 21 |
+
("Korean", "benches/data/ko.txt"),
|
| 22 |
+
];
|
| 23 |
+
|
| 24 |
+
// ground truth: WhitespaceSplit(Removed) = maximal runs of non-`char::is_whitespace`.
|
| 25 |
+
fn ws_ref(s: &str) -> Vec<Span> {
|
| 26 |
+
let mut out = Vec::new();
|
| 27 |
+
let mut start: Option<usize> = None;
|
| 28 |
+
for (i, c) in s.char_indices() {
|
| 29 |
+
if c.is_whitespace() {
|
| 30 |
+
if let Some(st) = start.take() {
|
| 31 |
+
out.push((st as u32, i as u32));
|
| 32 |
+
}
|
| 33 |
+
} else if start.is_none() {
|
| 34 |
+
start = Some(i);
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
if let Some(st) = start {
|
| 38 |
+
out.push((st as u32, s.len() as u32));
|
| 39 |
+
}
|
| 40 |
+
out
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
fn ns_per_byte<F: FnMut() -> usize>(len: usize, iters: u32, mut f: F) -> f64 {
|
| 44 |
+
for _ in 0..3 {
|
| 45 |
+
black_box(f());
|
| 46 |
+
}
|
| 47 |
+
let mut best = f64::INFINITY;
|
| 48 |
+
for _ in 0..7 {
|
| 49 |
+
let t = Instant::now();
|
| 50 |
+
let mut acc = 0usize;
|
| 51 |
+
for _ in 0..iters {
|
| 52 |
+
acc = acc.wrapping_add(f());
|
| 53 |
+
}
|
| 54 |
+
black_box(acc);
|
| 55 |
+
best = best.min(t.elapsed().as_nanos() as f64 / (iters as usize * len) as f64);
|
| 56 |
+
}
|
| 57 |
+
best
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
fn main() {
|
| 61 |
+
let manifest = env!("CARGO_MANIFEST_DIR");
|
| 62 |
+
println!(
|
| 63 |
+
"{:<10} {:>7} {:>5} {:>8} {:>8} | {:>7} {:>6}",
|
| 64 |
+
"lang", "bytes", "b/tok", "simd", "scalar", "speedup", "parity"
|
| 65 |
+
);
|
| 66 |
+
|
| 67 |
+
for (label, rel) in CORPORA {
|
| 68 |
+
let raw = match std::fs::read_to_string(format!("{manifest}/{rel}")) {
|
| 69 |
+
Ok(s) if !s.trim().is_empty() => s,
|
| 70 |
+
_ => {
|
| 71 |
+
println!("{label:<10} (skipped — {rel} missing)");
|
| 72 |
+
continue;
|
| 73 |
+
}
|
| 74 |
+
};
|
| 75 |
+
let mut c = raw.len().min(180_000);
|
| 76 |
+
while c > 0 && !raw.is_char_boundary(c) {
|
| 77 |
+
c -= 1;
|
| 78 |
+
}
|
| 79 |
+
let corpus = &raw[..c];
|
| 80 |
+
let text = corpus.as_bytes();
|
| 81 |
+
let n = text.len();
|
| 82 |
+
let iters = (4_000_000 / n).clamp(3, 150) as u32;
|
| 83 |
+
|
| 84 |
+
let mut tags = vec![0u8; n];
|
| 85 |
+
classify::<Atoms>(text, &mut tags);
|
| 86 |
+
|
| 87 |
+
// parity: simd == scalar == char::is_whitespace ground truth
|
| 88 |
+
let reference = ws_ref(corpus);
|
| 89 |
+
let (mut a, mut b) = (Vec::new(), Vec::new());
|
| 90 |
+
whitespace_split_scalar(text, &tags, &mut a);
|
| 91 |
+
whitespace_split_simd(text, &tags, &mut b);
|
| 92 |
+
let parity = if a == reference && b == reference {
|
| 93 |
+
"✓"
|
| 94 |
+
} else {
|
| 95 |
+
if a != reference {
|
| 96 |
+
eprintln!(" {label}: scalar != ref ({} vs {} tokens)", a.len(), reference.len());
|
| 97 |
+
}
|
| 98 |
+
if b != reference {
|
| 99 |
+
let k = a.iter().zip(&b).position(|(x, y)| x != y).unwrap_or(a.len().min(b.len()));
|
| 100 |
+
eprintln!(" {label}: simd != ref @tok {k}: simd={:?} scal={:?}", b.get(k), a.get(k));
|
| 101 |
+
}
|
| 102 |
+
"✗"
|
| 103 |
+
};
|
| 104 |
+
let btok = n as f64 / a.len().max(1) as f64;
|
| 105 |
+
|
| 106 |
+
let simd = ns_per_byte(n, iters, || {
|
| 107 |
+
b.clear();
|
| 108 |
+
whitespace_split_simd(text, &tags, &mut b);
|
| 109 |
+
b.len()
|
| 110 |
+
});
|
| 111 |
+
let scal = ns_per_byte(n, iters, || {
|
| 112 |
+
a.clear();
|
| 113 |
+
whitespace_split_scalar(text, &tags, &mut a);
|
| 114 |
+
a.len()
|
| 115 |
+
});
|
| 116 |
+
|
| 117 |
+
println!(
|
| 118 |
+
"{label:<10} {n:>7} {btok:>5.1} {simd:>8.3} {scal:>8.3} | {:>6.2}x {parity:>6}",
|
| 119 |
+
scal / simd
|
| 120 |
+
);
|
| 121 |
+
}
|
| 122 |
+
println!("\n(ns/byte, lower better. simd = whitespace_split_simd, scalar = generic fsm_split core.)");
|
| 123 |
+
}
|