1 use criterion::{criterion_group, criterion_main, Criterion};
2 
3 const DATA: [&str; 34] = [
4     "waifulandshigtgsqwetyuop[]asbnm,./",
5     "waifulandshigtgsqwetyuop[]asbnm,.",
6     "waifulandshigtgsqwetyuop[]asbnm,",
7     "waifulandshigtgsqwetyuop[]asbnm",
8     "waifulandshigtgsqwetyuop[]asbn",
9     "waifulandshigtgsqwetyuop[]asb",
10     "waifulandshigtgsqwetyuop[]as",
11     "waifulandshigtgsqwetyuop[]a",
12     "waifulandshigtgsqwetyuop[]",
13     "waifulandshigtgsqwetyuop[",
14     "waifulandshigtgsqwetyuop",
15     "waifulandshigtgsqwetyuo",
16     "waifulandshigtgsqwetyu",
17     "waifulandshigtgsqwety",
18     "waifulandshigtgsqwet",
19     "waifulandshigtgsqwe",
20     "waifulandshigtgsqw",
21     "waifulandshigtgsq",
22     "waifulandshigtgs",
23     "waifulandshigtg",
24     "waifulandshigt",
25     "waifulandshig",
26     "waifulandshi",
27     "waifulandsh",
28     "waifulands",
29     "waifuland",
30     "waifulan",
31     "waifula",
32     "waiful",
33     "lolka",
34     "lolk",
35     "lol",
36     "lo",
37     "l",
38 ];
39 
define(c: &mut Criterion)40 fn define(c: &mut Criterion) {
41     #[cfg(feature = "xxh32")]
42     c.bench_function("xxh32 Rust", |b| b.iter_batched(|| &DATA, |data| for input in data {
43         xxhash_rust::xxh32::xxh32(input.as_bytes(), 0);
44     }, criterion::BatchSize::SmallInput));
45 
46     #[cfg(feature = "const_xxh32")]
47     c.bench_function("const_xxh32 Rust", |b| b.iter_batched(|| &DATA, |data| for input in data {
48         xxhash_rust::const_xxh32::xxh32(input.as_bytes(), 0);
49     }, criterion::BatchSize::SmallInput));
50 
51     #[cfg(feature = "xxh32")]
52     c.bench_function("xxh32 Rust Stateful", |b| b.iter_batched(|| &DATA, |data| for input in data {
53         let mut hasher = xxhash_rust::xxh32::Xxh32::new(0);
54         hasher.update(input.as_bytes());
55         hasher.digest();
56     }, criterion::BatchSize::SmallInput));
57 
58     #[cfg(feature = "xxh64")]
59     c.bench_function("xxh64 Rust", |b| b.iter_batched(|| &DATA, |data| for input in data {
60         xxhash_rust::xxh64::xxh64(input.as_bytes(), 0);
61     }, criterion::BatchSize::SmallInput));
62 
63     #[cfg(feature = "xxh64")]
64     c.bench_function("xxh64 Rust Stateful", |b| b.iter_batched(|| &DATA, |data| for input in data {
65         let mut hasher = xxhash_rust::xxh64::Xxh64::new(0);
66         hasher.update(input.as_bytes());
67         hasher.digest();
68     }, criterion::BatchSize::SmallInput));
69 
70     #[cfg(feature = "const_xxh64")]
71     c.bench_function("const_xxh64 Rust", |b| b.iter_batched(|| &DATA, |data| for input in data {
72         xxhash_rust::const_xxh64::xxh64(input.as_bytes(), 0);
73     }, criterion::BatchSize::SmallInput));
74 
75     c.bench_function("twox-hash32 Rust", |b| b.iter_batched(|| &DATA, |data| for input in data {
76         use core::hash::Hasher;
77 
78         let mut hasher = twox_hash::XxHash32::with_seed(0);
79         hasher.write(input.as_bytes());
80         hasher.finish();
81     }, criterion::BatchSize::SmallInput));
82 
83     c.bench_function("twox-hash64 Rust", |b| b.iter_batched(|| &DATA, |data| for input in data {
84         use core::hash::Hasher;
85 
86         let mut hasher = twox_hash::XxHash64::with_seed(0);
87         hasher.write(input.as_bytes());
88         hasher.finish();
89     }, criterion::BatchSize::SmallInput));
90 
91     c.bench_function("xxh32 C", |b| b.iter_batched(|| &DATA, |data| for input in data {
92         unsafe {
93             xxhash_c_sys::XXH32(input.as_ptr() as _, input.len(), 0);
94         }
95     }, criterion::BatchSize::SmallInput));
96 
97     c.bench_function("xxh32 C Stateful", |b| b.iter_batched(|| &DATA, |data| for input in data {
98         use xxhash_c_sys as sys;
99 
100         let mut state = core::mem::MaybeUninit::<sys::XXH32_state_t>::uninit();
101 
102         unsafe {
103             sys::XXH32_reset(state.as_mut_ptr(), 0);
104             sys::XXH32_update(state.as_mut_ptr(), input.as_ptr() as _, input.len());
105             sys::XXH32_digest(state.as_mut_ptr());
106         }
107     }, criterion::BatchSize::SmallInput));
108 
109     c.bench_function("xxh64 C", |b| b.iter_batched(|| &DATA, |data| for input in data {
110         unsafe {
111             xxhash_c_sys::XXH64(input.as_ptr() as _, input.len(), 0);
112         }
113     }, criterion::BatchSize::SmallInput));
114 
115     c.bench_function("xxh64 C Stateful", |b| b.iter_batched(|| &DATA, |data| for input in data {
116         use xxhash_c_sys as sys;
117 
118         let mut state = core::mem::MaybeUninit::<sys::XXH64_state_t>::uninit();
119 
120         unsafe {
121             sys::XXH64_reset(state.as_mut_ptr(), 0);
122             sys::XXH64_update(state.as_mut_ptr(), input.as_ptr() as _, input.len());
123             sys::XXH64_digest(state.as_mut_ptr());
124         }
125     }, criterion::BatchSize::SmallInput));
126 
127 
128     #[cfg(feature = "xxh3")]
129     c.bench_function("xxh3_64 Rust", |b| b.iter_batched(|| &DATA, |data| for input in data {
130         xxhash_rust::xxh3::xxh3_64(input.as_bytes());
131     }, criterion::BatchSize::SmallInput));
132 
133     #[cfg(feature = "xxh3")]
134     c.bench_function("xxh3_64 Rust Stateful", move |b| b.iter_batched(move || &DATA, |data| for input in data {
135         let mut hasher = xxhash_rust::xxh3::Xxh3::new();
136         hasher.update(input.as_bytes());
137         hasher.digest();
138     }, criterion::BatchSize::SmallInput));
139 
140     #[cfg(feature = "xxh3")]
141     c.bench_function("xxh3_128 Rust", |b| b.iter_batched(|| &DATA, |data| for input in data {
142         xxhash_rust::xxh3::xxh3_128(input.as_bytes());
143     }, criterion::BatchSize::SmallInput));
144 
145     #[cfg(feature = "const_xxh3")]
146     c.bench_function("const_xxh3 64 Rust", |b| b.iter_batched(|| &DATA, |data| for input in data {
147         xxhash_rust::const_xxh3::xxh3_64(input.as_bytes());
148     }, criterion::BatchSize::SmallInput));
149 
150     #[cfg(feature = "const_xxh3")]
151     c.bench_function("const_xxh3 128 Rust", |b| b.iter_batched(|| &DATA, |data| for input in data {
152         xxhash_rust::const_xxh3::xxh3_128(input.as_bytes());
153     }, criterion::BatchSize::SmallInput));
154 
155     c.bench_function("xxh3_64 C Stateful", |b| b.iter_batched(|| &DATA, |data| for input in data {
156         use xxhash_c_sys as sys;
157 
158         let mut state = core::mem::MaybeUninit::<sys::XXH3_state_t>::uninit();
159 
160         unsafe {
161             sys::XXH3_64bits_reset(state.as_mut_ptr());
162             sys::XXH3_64bits_update(state.as_mut_ptr(), input.as_ptr() as _, input.len());
163             sys::XXH3_64bits_digest(state.as_mut_ptr());
164         }
165     }, criterion::BatchSize::SmallInput));
166 
167     c.bench_function("xxh3_64 C", |b| b.iter_batched(|| &DATA, |data| for input in data {
168         unsafe {
169             xxhash_c_sys::XXH3_64bits(input.as_ptr() as _, input.len());
170         }
171     }, criterion::BatchSize::SmallInput));
172 
173     c.bench_function("xxh3_128 C", |b| b.iter_batched(|| &DATA, |data| for input in data {
174         unsafe {
175             xxhash_c_sys::XXH3_128bits(input.as_ptr() as _, input.len());
176         }
177     }, criterion::BatchSize::SmallInput));
178 
179     let mut rand_230_bytes = [0u8; 260];
180     let _ = getrandom::getrandom(&mut rand_230_bytes);
181 
182     c.bench_function("xxh3_64 C 230b", |b| b.iter_batched(|| rand_230_bytes, |input| {
183         unsafe {
184             xxhash_c_sys::XXH3_64bits(input.as_ptr() as _, input.len());
185         }
186     }, criterion::BatchSize::SmallInput));
187 
188     #[cfg(feature = "xxh3")]
189     c.bench_function("xxh3_64 Rust 230b", |b| b.iter_batched(|| rand_230_bytes, |input| {
190         xxhash_rust::xxh3::xxh3_64(&input);
191     }, criterion::BatchSize::SmallInput));
192 
193     c.bench_function("xxh3_128 C 230b", |b| b.iter_batched(|| rand_230_bytes, |input| {
194         unsafe {
195             xxhash_c_sys::XXH3_128bits(input.as_ptr() as _, input.len());
196         }
197     }, criterion::BatchSize::SmallInput));
198 
199     #[cfg(feature = "xxh3")]
200     c.bench_function("xxh3_128 Rust 230b", |b| b.iter_batched(|| rand_230_bytes, |input| {
201         xxhash_rust::xxh3::xxh3_128(&input);
202     }, criterion::BatchSize::SmallInput));
203 
204     let mut rand_260_bytes = [0u8; 260];
205     let _ = getrandom::getrandom(&mut rand_260_bytes);
206 
207     c.bench_function("xxh3_64 C 260b", |b| b.iter_batched(|| rand_260_bytes, |input| {
208         unsafe {
209             xxhash_c_sys::XXH3_64bits(input.as_ptr() as _, input.len());
210         }
211     }, criterion::BatchSize::SmallInput));
212 
213     c.bench_function("xxh3_64 C Stateful 260b", |b| b.iter_batched(|| rand_260_bytes, |input| {
214         use xxhash_c_sys as sys;
215 
216         let mut state = core::mem::MaybeUninit::<sys::XXH3_state_t>::uninit();
217 
218         unsafe {
219             sys::XXH3_64bits_reset(state.as_mut_ptr());
220             sys::XXH3_64bits_update(state.as_mut_ptr(), input.as_ptr() as _, input.len());
221             sys::XXH3_64bits_digest(state.as_mut_ptr());
222         }
223     }, criterion::BatchSize::SmallInput));
224 
225     c.bench_function("xxh3_128 C 260b", |b| b.iter_batched(|| rand_260_bytes, |input| {
226         unsafe {
227             xxhash_c_sys::XXH3_128bits(input.as_ptr() as _, input.len());
228         }
229     }, criterion::BatchSize::SmallInput));
230 
231     #[cfg(feature = "xxh3")]
232     c.bench_function("xxh3_64 Rust 260b", |b| b.iter_batched(|| rand_260_bytes, |input| {
233         xxhash_rust::xxh3::xxh3_64(&input);
234     }, criterion::BatchSize::SmallInput));
235 
236     #[cfg(feature = "xxh3")]
237     c.bench_function("xxh3_64 Rust Stateful 260b", |b| b.iter_batched(|| rand_260_bytes, |input| {
238         let mut hasher = xxhash_rust::xxh3::Xxh3::new();
239         hasher.update(&input);
240         hasher.digest();
241     }, criterion::BatchSize::SmallInput));
242 
243     #[cfg(feature = "xxh3")]
244     c.bench_function("xxh3_128 Rust 260b", |b| b.iter_batched(|| rand_260_bytes, |input| {
245         xxhash_rust::xxh3::xxh3_128(&input);
246     }, criterion::BatchSize::SmallInput));
247 }
248 
249 criterion_group!(benches, define);
250 
251 criterion_main!(benches);
252