1 use ahash::{CallHasher, RandomState};
2 use criterion::*;
3 use fxhash::FxHasher;
4 use std::collections::hash_map::DefaultHasher;
5 use std::hash::{Hash, Hasher, BuildHasher};
6
7 #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes"))]
aeshash<H: Hash>(b: &H) -> u648 fn aeshash<H: Hash>(b: &H) -> u64 {
9 let hasher = RandomState::with_seeds(1, 2, 3, 4).build_hasher();
10 H::get_hash(b, hasher)
11 }
12 #[cfg(not(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes")))]
aeshash<H: Hash>(_b: &H) -> u6413 fn aeshash<H: Hash>(_b: &H) -> u64 {
14 panic!("aes must be enabled")
15 }
16
17 #[cfg(not(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes")))]
fallbackhash<H: Hash>(b: &H) -> u6418 fn fallbackhash<H: Hash>(b: &H) -> u64 {
19 let hasher = RandomState::with_seeds(1, 2, 3, 4).build_hasher();
20 H::get_hash(b, hasher)
21 }
22 #[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes"))]
fallbackhash<H: Hash>(_b: &H) -> u6423 fn fallbackhash<H: Hash>(_b: &H) -> u64 {
24 panic!("aes must be disabled")
25 }
26
fnvhash<H: Hash>(b: &H) -> u6427 fn fnvhash<H: Hash>(b: &H) -> u64 {
28 let mut hasher = fnv::FnvHasher::default();
29 b.hash(&mut hasher);
30 hasher.finish()
31 }
32
siphash<H: Hash>(b: &H) -> u6433 fn siphash<H: Hash>(b: &H) -> u64 {
34 let mut hasher = DefaultHasher::default();
35 b.hash(&mut hasher);
36 hasher.finish()
37 }
38
fxhash<H: Hash>(b: &H) -> u6439 fn fxhash<H: Hash>(b: &H) -> u64 {
40 let mut hasher = FxHasher::default();
41 b.hash(&mut hasher);
42 hasher.finish()
43 }
44
seahash<H: Hash>(b: &H) -> u6445 fn seahash<H: Hash>(b: &H) -> u64 {
46 let mut hasher = seahash::SeaHasher::default();
47 b.hash(&mut hasher);
48 hasher.finish()
49 }
50
51 const STRING_LENGTHS: [u32; 12] = [1, 3, 4, 7, 8, 15, 16, 24, 33, 68, 132, 1024];
52
gen_strings() -> Vec<String>53 fn gen_strings() -> Vec<String> {
54 STRING_LENGTHS
55 .iter()
56 .map(|len| {
57 let mut string = String::default();
58 for pos in 1..=*len {
59 let c = (48 + (pos % 10) as u8) as char;
60 string.push(c);
61 }
62 string
63 })
64 .collect()
65 }
66
67 const U8_VALUES: [u8; 1] = [123];
68 const U16_VALUES: [u16; 1] = [1234];
69 const U32_VALUES: [u32; 1] = [12345678];
70 const U64_VALUES: [u64; 1] = [1234567890123456];
71 const U128_VALUES: [u128; 1] = [12345678901234567890123456789012];
72
bench_ahash(c: &mut Criterion)73 fn bench_ahash(c: &mut Criterion) {
74 c.bench(
75 "aeshash",
76 ParameterizedBenchmark::new("u8", |b, &s| b.iter(|| black_box(aeshash(s))), &U8_VALUES),
77 );
78 c.bench(
79 "aeshash",
80 ParameterizedBenchmark::new("u16", |b, &s| b.iter(|| black_box(aeshash(s))), &U16_VALUES),
81 );
82 c.bench(
83 "aeshash",
84 ParameterizedBenchmark::new("u32", |b, &s| b.iter(|| black_box(aeshash(s))), &U32_VALUES),
85 );
86 c.bench(
87 "aeshash",
88 ParameterizedBenchmark::new("u64", |b, &s| b.iter(|| black_box(aeshash(s))), &U64_VALUES),
89 );
90 c.bench(
91 "aeshash",
92 ParameterizedBenchmark::new("u128", |b, &s| b.iter(|| black_box(aeshash(s))), &U128_VALUES),
93 );
94 c.bench(
95 "aeshash",
96 ParameterizedBenchmark::new("string", |b, s| b.iter(|| black_box(aeshash(s))), gen_strings()),
97 );
98 }
99
bench_fallback(c: &mut Criterion)100 fn bench_fallback(c: &mut Criterion) {
101 c.bench(
102 "fallback",
103 ParameterizedBenchmark::new("u8", |b, &s| b.iter(|| black_box(fallbackhash(s))), &U8_VALUES),
104 );
105 c.bench(
106 "fallback",
107 ParameterizedBenchmark::new("u16", |b, &s| b.iter(|| black_box(fallbackhash(s))), &U16_VALUES),
108 );
109 c.bench(
110 "fallback",
111 ParameterizedBenchmark::new("u32", |b, &s| b.iter(|| black_box(fallbackhash(s))), &U32_VALUES),
112 );
113 c.bench(
114 "fallback",
115 ParameterizedBenchmark::new("u64", |b, &s| b.iter(|| black_box(fallbackhash(s))), &U64_VALUES),
116 );
117 c.bench(
118 "fallback",
119 ParameterizedBenchmark::new("u128", |b, &s| b.iter(|| black_box(fallbackhash(s))), &U128_VALUES),
120 );
121 c.bench(
122 "fallback",
123 ParameterizedBenchmark::new("string", |b, s| b.iter(|| black_box(fallbackhash(s))), gen_strings()),
124 );
125 }
126
bench_fx(c: &mut Criterion)127 fn bench_fx(c: &mut Criterion) {
128 c.bench(
129 "fx",
130 ParameterizedBenchmark::new("u8", |b, &s| b.iter(|| black_box(fxhash(s))), &U8_VALUES),
131 );
132 c.bench(
133 "fx",
134 ParameterizedBenchmark::new("u16", |b, &s| b.iter(|| black_box(fxhash(s))), &U16_VALUES),
135 );
136 c.bench(
137 "fx",
138 ParameterizedBenchmark::new("u32", |b, &s| b.iter(|| black_box(fxhash(s))), &U32_VALUES),
139 );
140 c.bench(
141 "fx",
142 ParameterizedBenchmark::new("u64", |b, &s| b.iter(|| black_box(fxhash(s))), &U64_VALUES),
143 );
144 c.bench(
145 "fx",
146 ParameterizedBenchmark::new("u128", |b, &s| b.iter(|| black_box(fxhash(s))), &U128_VALUES),
147 );
148 c.bench(
149 "fx",
150 ParameterizedBenchmark::new("string", |b, s| b.iter(|| black_box(fxhash(s))), gen_strings()),
151 );
152 }
153
bench_fnv(c: &mut Criterion)154 fn bench_fnv(c: &mut Criterion) {
155 c.bench(
156 "fnv",
157 ParameterizedBenchmark::new("u8", |b, &s| b.iter(|| black_box(fnvhash(s))), &U8_VALUES),
158 );
159 c.bench(
160 "fnv",
161 ParameterizedBenchmark::new("u16", |b, &s| b.iter(|| black_box(fnvhash(s))), &U16_VALUES),
162 );
163 c.bench(
164 "fnv",
165 ParameterizedBenchmark::new("u32", |b, &s| b.iter(|| black_box(fnvhash(s))), &U32_VALUES),
166 );
167 c.bench(
168 "fnv",
169 ParameterizedBenchmark::new("u64", |b, &s| b.iter(|| black_box(fnvhash(s))), &U64_VALUES),
170 );
171 c.bench(
172 "fnv",
173 ParameterizedBenchmark::new("u128", |b, &s| b.iter(|| black_box(fnvhash(s))), &U128_VALUES),
174 );
175 c.bench(
176 "fnv",
177 ParameterizedBenchmark::new("string", |b, s| b.iter(|| black_box(fnvhash(s))), gen_strings()),
178 );
179 }
180
bench_sea(c: &mut Criterion)181 fn bench_sea(c: &mut Criterion) {
182 c.bench(
183 "sea",
184 ParameterizedBenchmark::new("string", |b, s| b.iter(|| black_box(seahash(s))), gen_strings()),
185 );
186 }
187
bench_sip(c: &mut Criterion)188 fn bench_sip(c: &mut Criterion) {
189 c.bench(
190 "sip",
191 ParameterizedBenchmark::new("u8", |b, &s| b.iter(|| black_box(siphash(s))), &U8_VALUES),
192 );
193 c.bench(
194 "sip",
195 ParameterizedBenchmark::new("u16", |b, &s| b.iter(|| black_box(siphash(s))), &U16_VALUES),
196 );
197 c.bench(
198 "sip",
199 ParameterizedBenchmark::new("u32", |b, &s| b.iter(|| black_box(siphash(s))), &U32_VALUES),
200 );
201 c.bench(
202 "sip",
203 ParameterizedBenchmark::new("u64", |b, &s| b.iter(|| black_box(siphash(s))), &U64_VALUES),
204 );
205 c.bench(
206 "sip",
207 ParameterizedBenchmark::new("u128", |b, &s| b.iter(|| black_box(siphash(s))), &U128_VALUES),
208 );
209 c.bench(
210 "sip",
211 ParameterizedBenchmark::new("string", |b, s| b.iter(|| black_box(siphash(s))), gen_strings()),
212 );
213 }
214
215 criterion_main!(benches);
216 criterion_group!(
217 benches,
218 bench_ahash,
219 bench_fallback,
220 bench_fx,
221 bench_fnv,
222 bench_sea,
223 bench_sip
224 );
225