1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//! C-side counterpart of `encode_loop_z000033`, for instruction-count and
//! cycle comparisons under `perf stat`.
//!
//! Same shape as the Rust loop: read the corpus once, allocate the output
//! buffer once, then compress it N times at the given level through the
//! upstream `ZSTD_compress` one-shot API. Keeping the two binaries
//! structurally identical is the point — the difference in retired
//! instructions is then attributable to the encoders, not to harness
//! overhead.
//!
//! Build: `cargo build --release -p ffi-bench --example ffi_encode_loop_z000033`
//! Run: `perf stat -e cycles,instructions ./ffi_encode_loop_z000033 <level> <iters> <corpus> [ldm]`
//!
//! With `ldm` it times the benchmark's `level_<N>_*_ldm` C arm: a fresh
//! context per frame through `ZSTD_compress2`, long-distance matching on,
//! content checksum and content size on.
use std::env;
use std::fs;
use zstd::zstd_safe::zstd_sys;
fn main() {
if env::args().nth(4).as_deref() == Some("ldm") {
return ldm_loop();
}
let level: i32 = env::args().nth(1).and_then(|s| s.parse().ok()).unwrap_or(3);
let iters: usize = env::args()
.nth(2)
.and_then(|s| s.parse().ok())
.unwrap_or(40);
let corpus_path = env::args()
.nth(3)
.unwrap_or_else(|| "zstd/decodecorpus_files/z000033".to_string());
let bytes = fs::read(&corpus_path).expect("read corpus");
let bound = unsafe { zstd_sys::ZSTD_compressBound(bytes.len()) };
let mut out = vec![0u8; bound];
// Summed so the optimiser cannot drop the calls, mirroring the Rust loop.
let mut sum = 0usize;
for _ in 0..iters {
let written = unsafe {
zstd_sys::ZSTD_compress(
out.as_mut_ptr().cast(),
out.len(),
bytes.as_ptr().cast(),
bytes.len(),
level,
)
};
assert!(
unsafe { zstd_sys::ZSTD_isError(written) } == 0,
"ZSTD_compress failed"
);
sum += written;
}
println!(
"encoded {} bytes x {} iters at level {}; last-out-sum={}",
bytes.len(),
iters,
level,
sum
);
}
fn ldm_loop() {
use zstd_sys::ZSTD_cParameter as P;
let level: i32 = env::args().nth(1).and_then(|s| s.parse().ok()).unwrap_or(1);
let iters: usize = env::args()
.nth(2)
.and_then(|s| s.parse().ok())
.unwrap_or(40);
let corpus_path = env::args().nth(3).expect("corpus path");
let bytes = fs::read(&corpus_path).expect("read corpus");
let bound = unsafe { zstd_sys::ZSTD_compressBound(bytes.len()) };
let mut out = vec![0u8; bound];
let mut sum = 0usize;
for _ in 0..iters {
// SAFETY: every call operates on the context created here, freed at
// the end of the iteration; errors are asserted.
unsafe {
let cctx = zstd_sys::ZSTD_createCCtx();
assert!(!cctx.is_null(), "ZSTD_createCCtx returned null");
for (param, value) in [
(P::ZSTD_c_compressionLevel, level),
(P::ZSTD_c_checksumFlag, 1),
(P::ZSTD_c_contentSizeFlag, 1),
(P::ZSTD_c_enableLongDistanceMatching, 1),
] {
let rc = zstd_sys::ZSTD_CCtx_setParameter(cctx, param, value);
assert!(zstd_sys::ZSTD_isError(rc) == 0, "setParameter failed");
}
let written = zstd_sys::ZSTD_compress2(
cctx,
out.as_mut_ptr().cast(),
out.len(),
bytes.as_ptr().cast(),
bytes.len(),
);
assert!(
zstd_sys::ZSTD_isError(written) == 0,
"ZSTD_compress2 failed"
);
sum += written;
zstd_sys::ZSTD_freeCCtx(cctx);
}
}
println!(
"encoded {} bytes x {} iters at level {} with ldm; last-out-sum={}",
bytes.len(),
iters,
level,
sum
);
}