Skip to content

Commit 2375c0e

Browse files
committed
benchmark on all zlib inputs from gix blame README.md
1 parent 3aa2cc2 commit 2375c0e

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use libz_sys as libz_ng_sys;
2+
3+
fn iter_files() -> impl Iterator<Item = &'static [u8]> {
4+
static DATA: &[u8] = include_bytes!("../src/test-data/gix-blame-readme.bin");
5+
6+
let mut cursor = DATA;
7+
8+
std::iter::from_fn(move || {
9+
let Some((length, rest)) = cursor.split_at_checked(4) else {
10+
return None;
11+
};
12+
13+
let length = u32::from_le_bytes(length.try_into().unwrap()) as usize;
14+
15+
let (input, rest) = rest.split_at(length);
16+
17+
cursor = rest;
18+
19+
Some(input)
20+
})
21+
}
22+
23+
fn main() {
24+
match std::env::args().nth(1).unwrap().as_str() {
25+
"ng" => {
26+
for _ in 0..100 {
27+
decompress_files_ng(iter_files());
28+
}
29+
}
30+
"rs" => {
31+
for _ in 0..100 {
32+
decompress_files_rs(iter_files());
33+
}
34+
}
35+
other => panic!("invalid variant: {other}"),
36+
}
37+
}
38+
39+
fn decompress_files_rs<'a>(inputs: impl Iterator<Item = &'a [u8]>) {
40+
use libz_rs_sys::*;
41+
42+
let mut outbuf = vec![0u8; 1 << 20];
43+
44+
unsafe {
45+
let mut stream = std::mem::MaybeUninit::zeroed();
46+
let ret = inflateInit2_(
47+
stream.as_mut_ptr(),
48+
15, // windowBits = 15 (default)
49+
zlibVersion(),
50+
std::mem::size_of::<z_stream>() as i32,
51+
);
52+
assert_eq!(ret, Z_OK);
53+
let stream = stream.assume_init_mut();
54+
55+
for input in inputs {
56+
stream.next_in = input.as_ptr() as *mut u8;
57+
stream.avail_in = input.len() as u32;
58+
stream.next_out = outbuf.as_mut_ptr();
59+
stream.avail_out = outbuf.len() as u32;
60+
61+
let ret = inflate(stream, Z_NO_FLUSH);
62+
assert!(ret == Z_STREAM_END || ret == Z_OK);
63+
64+
// Reset stream for next input
65+
let ret = inflateReset(stream);
66+
assert_eq!(ret, Z_OK);
67+
}
68+
69+
inflateEnd(stream);
70+
}
71+
}
72+
73+
fn decompress_files_ng<'a>(inputs: impl Iterator<Item = &'a [u8]>) {
74+
use libz_ng_sys::*;
75+
76+
let mut outbuf = vec![0u8; 1 << 20];
77+
78+
unsafe {
79+
let mut stream = std::mem::MaybeUninit::zeroed();
80+
let ret = inflateInit2_(
81+
stream.as_mut_ptr(),
82+
15, // windowBits = 15 (default)
83+
zlibVersion(),
84+
std::mem::size_of::<z_stream>() as i32,
85+
);
86+
assert_eq!(ret, Z_OK);
87+
let stream = stream.assume_init_mut();
88+
89+
for input in inputs {
90+
stream.next_in = input.as_ptr() as *mut u8;
91+
stream.avail_in = input.len() as u32;
92+
stream.next_out = outbuf.as_mut_ptr();
93+
stream.avail_out = outbuf.len() as u32;
94+
95+
let _ret = inflate(stream, Z_FINISH);
96+
// assert!(ret == Z_STREAM_END || ret == Z_OK,);
97+
98+
// Reset stream for next input
99+
let ret = inflateReset(stream);
100+
assert_eq!(ret, Z_OK);
101+
}
102+
103+
inflateEnd(stream);
104+
}
105+
}
2.26 MB
Binary file not shown.

0 commit comments

Comments
 (0)