forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

bench.rs69 lines, 2.8 KiBHistoryRawDownload
1//! bench: zlib inflate rows for lanes/bench.md ("lane benchrest").
2//!
3//! usage:
4//! bench zlib <in.zlib> <out.bin> [reps]
5//! bench blocks <in.blocks> <out.bin> [reps]
6//!
7//! `zlib` inflates one zlib stream with `inflate_zlib`, `reps` times (default 5), and keeps the
8//! best wall time. `blocks` reads a sequence of `[u32 little-endian length][zlib stream]` records
9//! (one per subtitle-sized packet) and inflates every record with `inflate_zlib`, timing the whole
10//! loop. Both write the concatenated output of the last pass to `<out.bin>`, untimed, for the md5
11//! check against python's `zlib.decompress`.
12use std::hint::black_box;
13use std::time::Instant;
14
15/// Output ceiling passed to every call: far above the inputs used here, but not "no ceiling".
16const LIMIT: usize = 1 << 30;
17
18fn split_blocks(data: &[u8]) -> Vec<&[u8]> {
19 let mut out = Vec::new();
20 let mut at = 0usize;
21 while at < data.len() {
22 let len = u32::from_le_bytes(data[at..at + 4].try_into().expect("4 bytes")) as usize;
23 at += 4;
24 out.push(&data[at..at + len]);
25 at += len;
26 }
27 out
28}
29
30fn main() {
31 let args: Vec<String> = std::env::args().collect();
32 match args.get(1).map(String::as_str) {
33 Some(mode @ ("zlib" | "blocks")) if args.len() >= 4 => {
34 let reps: usize = args.get(4).map_or(5, |s| s.parse().expect("reps"));
35 assert!(reps >= 1, "reps must be at least 1");
36 let data = std::fs::read(&args[2]).expect("read input");
37 let blocks = if mode == "blocks" { split_blocks(&data) } else { Vec::new() };
38 let mut best = f64::MAX;
39 let mut out = Vec::new();
40 for _ in 0..reps {
41 let t = Instant::now();
42 out = Vec::new();
43 if mode == "zlib" {
44 out = ec_inflate::inflate_zlib(black_box(&data), LIMIT).expect("inflate");
45 } else {
46 for b in &blocks {
47 let v = ec_inflate::inflate_zlib(black_box(b), LIMIT).expect("inflate block");
48 out.extend_from_slice(&v);
49 }
50 }
51 best = best.min(t.elapsed().as_secs_f64());
52 }
53 std::fs::write(&args[3], &out).expect("write output");
54 let streams = if mode == "blocks" { blocks.len() } else { 1 };
55 println!(
56 "mode={mode} streams={streams} in_bytes={} out_bytes={} best_inflate_s={best:.4} mb_per_s={:.2} reps={reps}",
57 data.len(),
58 out.len(),
59 out.len() as f64 / best / 1e6
60 );
61 }
62 _ => {
63 eprintln!(
64 "usage: bench zlib <in.zlib> <out.bin> [reps]\n bench blocks <in.blocks> <out.bin> [reps]"
65 );
66 std::process::exit(2);
67 }
68 }
69}