use lzvolt::format;
use std::time::Instant;
const BASELINE: &str = "bench-results/quick.txt";
const CELL_MS: u128 = 300;
fn main() {
if std::env::args().any(|a| a == "--entry") {
entry_cost();
return;
}
let save = std::env::args().any(|a| a == "--save");
let shapes = corpora();
let old = load(BASELINE);
let mut now: Vec<Cell> = Vec::new();
println!(
"{} | {} | kernel: {}",
machine(),
std::env::consts::ARCH,
lzvolt::backend()
);
println!(
"{:<22}{:>10}{:>11}{:>12}{:>10}{:>9}",
"", "ns/call", "GB/s", "vs liblz4", "vs base", "res."
);
for (name, data) in &shapes {
let mut reference = f64::NAN;
if let Some((raw, len)) = lz4_block(data) {
let mut ar = Arena::new(raw.len(), len + 64);
ar.buf[ar.src..ar.src + raw.len()].copy_from_slice(&raw);
let (head, tail) = ar.buf.split_at_mut(ar.a);
let block: &[u8] = &head[ar.src..ar.src + raw.len()];
let (dst_a, dst_b) = tail.split_at_mut(ar.b - ar.a);
let (ours, theirs, spread) = time_pair(
CELL_MS,
|| {
assert!(lzvolt::raw::unpack_lz4_into_slice(
block,
&mut dst_a[..len + 64],
len
));
},
|| lz4_decompress(block, &mut dst_b[..len + 64], len),
);
reference = theirs;
report(
&format!("lz4/{name}"),
len,
ours,
theirs,
spread,
&old,
&mut now,
);
}
let mut packed = Vec::new();
if format::pack(data, &mut packed) {
let mut out = pinned_vec(data.len() + 4160);
let (ours, spread) = time(CELL_MS, || {
format::unpack(&packed, &mut out).unwrap();
});
report(
&format!("own/{name}"),
data.len(),
ours,
reference,
spread,
&old,
&mut now,
);
}
}
if save {
store(BASELINE, &now);
println!("\nBaseline written to {BASELINE} ({} cells)", now.len());
} else if old.is_empty() {
println!("\nNo baseline yet. `--save` writes one.");
} else {
verdict(&old, &now);
}
}
struct Arena {
buf: Vec<u8>,
src: usize,
a: usize,
b: usize,
}
impl Arena {
fn new(src_len: usize, dst_len: usize) -> Self {
let stride = (src_len.max(dst_len) + 4096 + 63) & !63;
let mut buf = vec![0u8; stride * 3 + 4096 + 128];
let base = buf.as_ptr().align_offset(4096);
let _ = &mut buf;
Self {
buf,
src: base,
a: base + stride + 1088,
b: base + stride * 2 + 2176,
}
}
}
fn time_pair(budget_ms: u128, mut a: impl FnMut(), mut b: impl FnMut()) -> (f64, f64, f64) {
for _ in 0..8 {
a();
b();
}
let probe = Instant::now();
for _ in 0..64 {
a();
}
let per_call = (probe.elapsed().as_nanos() as f64 / 64.0).max(0.25);
let rounds = ((budget_ms as f64 * 1_000_000.0 / 5.0) / per_call).max(1.0) as u64;
let mut pairs: Vec<(f64, f64)> = Vec::with_capacity(5);
for _ in 0..5 {
let t = Instant::now();
for _ in 0..rounds {
a();
}
let ta = t.elapsed().as_nanos() as f64 / rounds as f64;
let t = Instant::now();
for _ in 0..rounds {
b();
}
let tb = t.elapsed().as_nanos() as f64 / rounds as f64;
pairs.push((ta, tb));
}
pairs.sort_by(|x, y| (x.0 / x.1).partial_cmp(&(y.0 / y.1)).unwrap());
let lo = pairs[1].0 / pairs[1].1;
let hi = pairs[3].0 / pairs[3].1;
let spread = 100.0 * (hi / lo - 1.0);
(pairs[2].0, pairs[2].1, spread)
}
#[cfg(feature = "liblz4")]
fn entry_cost() {
let body = lzvolt::raw::lz4_body();
println!("Body: {body:?}\n");
let mut rows: Vec<(&str, f64, f64)> = Vec::new();
for (label, n) in [("512", 512usize), ("4096", 4096)] {
let data = noise(n);
let (raw, len) = lz4_block(&data).unwrap();
let mut ar = Arena::new(raw.len(), len + 64);
ar.buf[ar.src..ar.src + raw.len()].copy_from_slice(&raw);
let (head, tail) = ar.buf.split_at_mut(ar.a);
let block: &[u8] = &head[ar.src..ar.src + raw.len()];
let (dst, _) = tail.split_at_mut(ar.b - ar.a);
let with = {
let d = &mut *dst;
time(400, || {
assert!(lzvolt::raw::unpack_lz4_into_slice(block, d, len));
})
.0
};
let without = {
let d = &mut *dst;
time(400, || {
assert!(lzvolt::raw::unpack_lz4_into_slice_on(body, block, d, len));
})
.0
};
rows.push((label, with, without));
println!("{label:>6} B with dispatch {with:>8.2} ns without {without:>8.2} ns");
}
let fit = |a: f64, b: f64| {
let bw = 3584.0 / (b - a);
(a - 512.0 / bw, bw)
};
let (fw, bw) = fit(rows[0].1, rows[1].1);
let (fo, bo) = fit(rows[0].2, rows[1].2);
println!("\n{:<24}{:>12}{:>14}", "", "fixed cost", "rate");
println!("{:<24}{fw:>9.2} ns{bw:>11.1} GB/s", "with dispatch");
println!("{:<24}{fo:>9.2} ns{bo:>11.1} GB/s", "without dispatch");
println!("{:<24}{:>9.2} ns", "davon Rust-Dispatch", fw - fo);
}
#[cfg(not(feature = "liblz4"))]
fn entry_cost() {
println!("braucht --features liblz4");
}
fn pinned_vec(cap: usize) -> Vec<u8> {
let mut candidates: Vec<Vec<u8>> = Vec::new();
for _ in 0..48 {
let v: Vec<u8> = Vec::with_capacity(cap);
if v.as_ptr() as usize % 4096 == 1088 {
return v;
}
candidates.push(v);
}
candidates.swap_remove(0)
}
fn time(budget_ms: u128, mut f: impl FnMut()) -> (f64, f64) {
for _ in 0..8 {
f();
}
let probe = Instant::now();
for _ in 0..64 {
f();
}
let per_call = (probe.elapsed().as_nanos() as f64 / 64.0).max(0.25);
let rounds = ((budget_ms as f64 * 1_000_000.0 / 5.0) / per_call).max(1.0) as u64;
let mut v: Vec<f64> = Vec::with_capacity(5);
for _ in 0..5 {
let t = Instant::now();
for _ in 0..rounds {
f();
}
v.push(t.elapsed().as_nanos() as f64 / rounds as f64);
}
v.sort_by(|x, y| x.partial_cmp(y).unwrap());
(v[2], 100.0 * (v[3] / v[1] - 1.0))
}
type Cell = (String, f64, f64, f64);
fn report(
name: &str,
bytes: usize,
ours: f64,
theirs: f64,
spread: f64,
old: &[Cell],
now: &mut Vec<Cell>,
) {
let gbs = bytes as f64 / ours;
let vs_lib = if theirs.is_nan() {
String::from("--")
} else {
format!("{:+.1}%", 100.0 * (theirs / ours - 1.0))
};
let vs_base = match old.iter().find(|(n, _, _, _)| n == name) {
Some((_, prev, prev_ref, _)) if !theirs.is_nan() && !prev_ref.is_nan() => {
let was = prev / prev_ref;
let is = ours / theirs;
format!("{:+.1}%", 100.0 * (was / is - 1.0))
}
Some(_) => String::from("--"),
None => String::from("neu"),
};
println!(
"{name:<22}{ours:>10.1}{gbs:>11.2}{vs_lib:>12}{vs_base:>10}{:>9}",
format!("+-{spread:.1}%")
);
now.push((name.to_string(), ours, theirs, spread));
}
fn store(path: &str, cells: &[Cell]) {
let _ = std::fs::create_dir_all(std::path::Path::new(path).parent().unwrap());
use std::fmt::Write;
let mut body = String::new();
for (n, a, b, sp) in cells {
let _ = writeln!(body, "{n}\t{a}\t{b}\t{sp}");
}
let _ = std::fs::write(path, body);
}
fn load(path: &str) -> Vec<Cell> {
let Ok(text) = std::fs::read_to_string(path) else {
return Vec::new();
};
text.lines()
.filter_map(|l| {
let mut f = l.split('\t');
Some((
f.next()?.to_string(),
f.next()?.parse().ok()?,
f.next()?.parse().unwrap_or(f64::NAN),
f.next().and_then(|v| v.parse().ok()).unwrap_or(6.0),
))
})
.collect()
}
fn verdict(old: &[Cell], now: &[Cell]) {
let (mut up, mut down, mut blind) = (Vec::new(), Vec::new(), 0usize);
for (name, ns, reference, spread) in now {
let Some((_, prev, prev_ref, prev_spread)) = old.iter().find(|(n, _, _, _)| n == name)
else {
continue;
};
if reference.is_nan() || prev_ref.is_nan() {
blind += 1;
continue;
}
let d = 100.0 * ((prev / prev_ref) / (ns / reference) - 1.0);
let limit = spread.max(*prev_spread).max(6.0);
if d >= limit {
up.push((name.clone(), d, limit));
} else if d <= -limit {
down.push((name.clone(), d, limit));
}
}
println!();
match (up.is_empty(), down.is_empty()) {
(true, true) => println!("= UNCHANGED"),
(false, true) => {
println!("^ BETTER");
for (n, d, l) in &up {
println!(" {n:<22}{d:+.1}% (resolution {l:.1}%)");
}
}
(true, false) => {
println!("v WORSE");
for (n, d, l) in &down {
println!(" {n:<22}{d:+.1}% (resolution {l:.1}%)");
}
}
(false, false) => {
println!("~ MIXED");
for (n, d, l) in up.iter().chain(down.iter()) {
println!(" {n:<22}{d:+.1}% (resolution {l:.1}%)");
}
}
}
if blind > 0 {
println!(" ({blind} cells with no reference -- built without liblz4?)");
}
}
fn machine() -> String {
#[cfg(target_os = "macos")]
{
if let Ok(out) = std::process::Command::new("sysctl")
.args(["-n", "machdep.cpu.brand_string"])
.output()
{
let name = String::from_utf8_lossy(&out.stdout).trim().to_owned();
if !name.is_empty() {
return name;
}
}
}
#[cfg(target_os = "linux")]
{
if let Ok(text) = std::fs::read_to_string("/proc/cpuinfo") {
for key in ["model name", "Model"] {
if let Some(line) = text.lines().find(|l| l.starts_with(key)) {
if let Some((_, v)) = line.split_once(':') {
return v.trim().to_owned();
}
}
}
let part = text
.lines()
.find(|l| l.starts_with("CPU part"))
.and_then(|l| l.split_once(':'))
.map(|(_, v)| v.trim().to_owned());
if let Some(part) = part {
let named = if part == "0xd4f" {
" (Neoverse V2)"
} else {
""
};
return format!("ARM part {part}{named}");
}
}
}
std::env::consts::ARCH.to_owned()
}
fn corpora() -> Vec<(String, Vec<u8>)> {
let mut v = Vec::new();
for (kind, f) in [
("records", records as fn(usize) -> Vec<u8>),
("varied", varied),
("noise", noise),
] {
for (label, n) in [("512", 512usize), ("4k", 4096), ("64k", 65_536)] {
v.push((format!("{kind}_{label}"), f(n)));
}
}
v
}
fn noise(total: usize) -> Vec<u8> {
let mut st = 0x2545_F491_4F6C_DD1Du64;
(0..total)
.map(|_| {
st ^= st << 13;
st ^= st >> 7;
st ^= st << 17;
st as u8
})
.collect()
}
fn records(total: usize) -> Vec<u8> {
let (mut o, mut i) = (Vec::new(), 0u64);
while o.len() < total {
o.extend_from_slice(
format!(
"{{\"id\":{i},\"tenant\":\"tenant42\",\"active\":true,\"role\":\"member\",\
\"created\":\"2026-09-0{}T1{}:0{}:00Z\",\"score\":{},\"region\":\"eu-central-1\"}}",
i % 9,
i % 10,
i % 10,
i % 1000
)
.as_bytes(),
);
i += 1;
}
o.truncate(total);
o
}
fn varied(total: usize) -> Vec<u8> {
let mut state = 0x2545_F491_4F6C_DD1Du64;
let mut next = move || {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
state
};
let regions = ["eu-central-1", "us-east-1", "ap-south-1", "sa-east-1"];
let roles = ["member", "admin", "viewer", "owner", "billing"];
let mut out = Vec::with_capacity(total);
while out.len() < total {
let r = next();
out.extend_from_slice(
format!(
"{{\"id\":\"{:016x}\",\"tenant\":\"{:08x}\",\"role\":\"{}\",\
\"region\":\"{}\",\"score\":{},\"seen\":{},\"token\":\"{:016x}\"}}",
r,
next() as u32,
roles[(r % 5) as usize],
regions[(r % 4) as usize],
next() % 100_000,
next() % 1_700_000_000,
next()
)
.as_bytes(),
);
}
out.truncate(total);
out
}
#[cfg(feature = "liblz4")]
#[link(name = "lz4")]
extern "C" {
fn LZ4_compress_default(s: *const u8, d: *mut u8, n: i32, cap: i32) -> i32;
fn LZ4_decompress_safe(s: *const u8, d: *mut u8, n: i32, cap: i32) -> i32;
}
#[cfg(feature = "liblz4")]
fn lz4_block(data: &[u8]) -> Option<(Vec<u8>, usize)> {
let mut block = vec![0u8; data.len() * 2 + 64];
let n = unsafe {
LZ4_compress_default(
data.as_ptr(),
block.as_mut_ptr(),
data.len() as i32,
block.len() as i32,
)
};
block.truncate(n as usize);
Some((block, data.len()))
}
#[cfg(feature = "liblz4")]
fn lz4_decompress(block: &[u8], out: &mut [u8], len: usize) {
unsafe {
LZ4_decompress_safe(
block.as_ptr(),
out.as_mut_ptr(),
block.len() as i32,
len as i32,
);
}
}
#[cfg(not(feature = "liblz4"))]
fn lz4_block(_: &[u8]) -> Option<(Vec<u8>, usize)> {
None
}
#[cfg(not(feature = "liblz4"))]
fn lz4_decompress(_: &[u8], _: &mut [u8], _: usize) {}