|
3 | 3 | use ahash::{AHasher, RandomState};
|
4 | 4 | use criterion::*;
|
5 | 5 | use fxhash::FxHasher;
|
| 6 | +use rand::Rng; |
6 | 7 | use std::collections::hash_map::DefaultHasher;
|
7 | 8 | use std::hash::{BuildHasherDefault, Hash, Hasher};
|
8 | 9 |
|
9 |
| -#[cfg(any( |
10 |
| - all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)), |
| 10 | +// Needs to be in sync with `src/lib.rs` |
| 11 | +const AHASH_IMPL: &str = if cfg!(any( |
11 | 12 | all(
|
12 |
| - any(target_arch = "arm", target_arch = "aarch64"), |
13 |
| - any(target_feature = "aes", target_feature = "crypto"), |
14 |
| - not(miri), |
15 |
| - feature = "stdsimd" |
16 |
| - ) |
17 |
| -))] |
18 |
| -fn aeshash<H: Hash>(b: &H) -> u64 { |
19 |
| - let build_hasher = RandomState::with_seeds(1, 2, 3, 4); |
20 |
| - build_hasher.hash_one(b) |
21 |
| -} |
22 |
| -#[cfg(not(any( |
23 |
| - all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)), |
24 |
| - all( |
25 |
| - any(target_arch = "arm", target_arch = "aarch64"), |
26 |
| - any(target_feature = "aes", target_feature = "crypto"), |
| 13 | + any(target_arch = "x86", target_arch = "x86_64"), |
| 14 | + target_feature = "aes", |
27 | 15 | not(miri),
|
28 |
| - feature = "stdsimd" |
29 |
| - ) |
30 |
| -)))] |
31 |
| -fn aeshash<H: Hash>(_b: &H) -> u64 { |
32 |
| - panic!("aes must be enabled") |
33 |
| -} |
34 |
| - |
35 |
| -#[cfg(not(any( |
36 |
| - all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)), |
| 16 | + ), |
37 | 17 | all(
|
38 | 18 | any(target_arch = "arm", target_arch = "aarch64"),
|
39 | 19 | any(target_feature = "aes", target_feature = "crypto"),
|
40 | 20 | not(miri),
|
41 |
| - feature = "stdsimd" |
42 |
| - ) |
43 |
| -)))] |
44 |
| -fn fallbackhash<H: Hash>(b: &H) -> u64 { |
| 21 | + feature = "stdsimd", |
| 22 | + ), |
| 23 | +)) { |
| 24 | + "aeshash" |
| 25 | +} else { |
| 26 | + "fallbackhash" |
| 27 | +}; |
| 28 | + |
| 29 | +fn ahash<H: Hash>(b: &H) -> u64 { |
45 | 30 | let build_hasher = RandomState::with_seeds(1, 2, 3, 4);
|
46 | 31 | build_hasher.hash_one(b)
|
47 | 32 | }
|
48 |
| -#[cfg(any( |
49 |
| - all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)), |
50 |
| - all( |
51 |
| - any(target_arch = "arm", target_arch = "aarch64"), |
52 |
| - any(target_feature = "aes", target_feature = "crypto"), |
53 |
| - not(miri), |
54 |
| - feature = "stdsimd" |
55 |
| - ) |
56 |
| -))] |
57 |
| -fn fallbackhash<H: Hash>(_b: &H) -> u64 { |
58 |
| - panic!("aes must be disabled") |
59 |
| -} |
60 | 33 |
|
61 | 34 | fn fnvhash<H: Hash>(b: &H) -> u64 {
|
62 | 35 | let mut hasher = fnv::FnvHasher::default();
|
@@ -98,72 +71,44 @@ fn gen_strings() -> Vec<String> {
|
98 | 71 | .collect()
|
99 | 72 | }
|
100 | 73 |
|
101 |
| -const U8_VALUE: u8 = 123; |
102 |
| -const U16_VALUE: u16 = 1234; |
103 |
| -const U32_VALUE: u32 = 12345678; |
104 |
| -const U64_VALUE: u64 = 1234567890123456; |
105 |
| -const U128_VALUE: u128 = 12345678901234567890123456789012; |
| 74 | +macro_rules! bench_inputs { |
| 75 | + ($group:ident, $hash:ident) => { |
| 76 | + // Number of iterations per batch should be high enough to hide timing overhead. |
| 77 | + let size = BatchSize::NumIterations(2_000); |
106 | 78 |
|
107 |
| -#[cfg(target_feature = "aes")] |
108 |
| -fn bench_ahash(c: &mut Criterion) { |
109 |
| - let mut group = c.benchmark_group("aeshash"); |
110 |
| - group.bench_with_input("u8", &U8_VALUE, |b, s| b.iter(|| black_box(aeshash(s)))); |
111 |
| - group.bench_with_input("u16", &U16_VALUE, |b, s| b.iter(|| black_box(aeshash(s)))); |
112 |
| - group.bench_with_input("u32", &U32_VALUE, |b, s| b.iter(|| black_box(aeshash(s)))); |
113 |
| - group.bench_with_input("u64", &U64_VALUE, |b, s| b.iter(|| black_box(aeshash(s)))); |
114 |
| - group.bench_with_input("u128", &U128_VALUE, |b, s| b.iter(|| black_box(aeshash(s)))); |
115 |
| - group.bench_with_input("string", &gen_strings(), |b, s| b.iter(|| black_box(aeshash(s)))); |
| 79 | + let mut rng = rand::thread_rng(); |
| 80 | + $group.bench_function("u8", |b| b.iter_batched(|| rng.gen::<u8>(), |v| $hash(&v), size)); |
| 81 | + $group.bench_function("u16", |b| b.iter_batched(|| rng.gen::<u16>(), |v| $hash(&v), size)); |
| 82 | + $group.bench_function("u32", |b| b.iter_batched(|| rng.gen::<u32>(), |v| $hash(&v), size)); |
| 83 | + $group.bench_function("u64", |b| b.iter_batched(|| rng.gen::<u64>(), |v| $hash(&v), size)); |
| 84 | + $group.bench_function("u128", |b| b.iter_batched(|| rng.gen::<u128>(), |v| $hash(&v), size)); |
| 85 | + $group.bench_with_input("strings", &gen_strings(), |b, s| b.iter(|| $hash(black_box(s)))); |
| 86 | + }; |
116 | 87 | }
|
117 | 88 |
|
118 |
| -#[cfg(not(target_feature = "aes"))] |
119 |
| -fn bench_fallback(c: &mut Criterion) { |
120 |
| - let mut group = c.benchmark_group("fallback"); |
121 |
| - group.bench_with_input("u8", &U8_VALUE, |b, s| b.iter(|| black_box(fallbackhash(s)))); |
122 |
| - group.bench_with_input("u16", &U16_VALUE, |b, s| b.iter(|| black_box(fallbackhash(s)))); |
123 |
| - group.bench_with_input("u32", &U32_VALUE, |b, s| b.iter(|| black_box(fallbackhash(s)))); |
124 |
| - group.bench_with_input("u64", &U64_VALUE, |b, s| b.iter(|| black_box(fallbackhash(s)))); |
125 |
| - group.bench_with_input("u128", &U128_VALUE, |b, s| b.iter(|| black_box(fallbackhash(s)))); |
126 |
| - group.bench_with_input("string", &gen_strings(), |b, s| b.iter(|| black_box(fallbackhash(s)))); |
| 89 | +fn bench_ahash(c: &mut Criterion) { |
| 90 | + let mut group = c.benchmark_group(AHASH_IMPL); |
| 91 | + bench_inputs!(group, ahash); |
127 | 92 | }
|
128 | 93 |
|
129 | 94 | fn bench_fx(c: &mut Criterion) {
|
130 | 95 | let mut group = c.benchmark_group("fx");
|
131 |
| - group.bench_with_input("u8", &U8_VALUE, |b, s| b.iter(|| black_box(fxhash(s)))); |
132 |
| - group.bench_with_input("u16", &U16_VALUE, |b, s| b.iter(|| black_box(fxhash(s)))); |
133 |
| - group.bench_with_input("u32", &U32_VALUE, |b, s| b.iter(|| black_box(fxhash(s)))); |
134 |
| - group.bench_with_input("u64", &U64_VALUE, |b, s| b.iter(|| black_box(fxhash(s)))); |
135 |
| - group.bench_with_input("u128", &U128_VALUE, |b, s| b.iter(|| black_box(fxhash(s)))); |
136 |
| - group.bench_with_input("string", &gen_strings(), |b, s| b.iter(|| black_box(fxhash(s)))); |
| 96 | + bench_inputs!(group, fxhash); |
137 | 97 | }
|
138 | 98 |
|
139 | 99 | fn bench_fnv(c: &mut Criterion) {
|
140 | 100 | let mut group = c.benchmark_group("fnv");
|
141 |
| - group.bench_with_input("u8", &U8_VALUE, |b, s| b.iter(|| black_box(fnvhash(s)))); |
142 |
| - group.bench_with_input("u16", &U16_VALUE, |b, s| b.iter(|| black_box(fnvhash(s)))); |
143 |
| - group.bench_with_input("u32", &U32_VALUE, |b, s| b.iter(|| black_box(fnvhash(s)))); |
144 |
| - group.bench_with_input("u64", &U64_VALUE, |b, s| b.iter(|| black_box(fnvhash(s)))); |
145 |
| - group.bench_with_input("u128", &U128_VALUE, |b, s| b.iter(|| black_box(fnvhash(s)))); |
146 |
| - group.bench_with_input("string", &gen_strings(), |b, s| b.iter(|| black_box(fnvhash(s)))); |
| 101 | + bench_inputs!(group, fnvhash); |
147 | 102 | }
|
148 | 103 |
|
149 | 104 | fn bench_sea(c: &mut Criterion) {
|
150 | 105 | let mut group = c.benchmark_group("sea");
|
151 |
| - group.bench_with_input("u8", &U8_VALUE, |b, s| b.iter(|| black_box(seahash(s)))); |
152 |
| - group.bench_with_input("u16", &U16_VALUE, |b, s| b.iter(|| black_box(seahash(s)))); |
153 |
| - group.bench_with_input("u32", &U32_VALUE, |b, s| b.iter(|| black_box(seahash(s)))); |
154 |
| - group.bench_with_input("u64", &U64_VALUE, |b, s| b.iter(|| black_box(seahash(s)))); |
155 |
| - group.bench_with_input("u128", &U128_VALUE, |b, s| b.iter(|| black_box(seahash(s)))); |
156 |
| - group.bench_with_input("string", &gen_strings(), |b, s| b.iter(|| black_box(seahash(s)))); |
| 106 | + bench_inputs!(group, seahash); |
157 | 107 | }
|
158 | 108 |
|
159 | 109 | fn bench_sip(c: &mut Criterion) {
|
160 | 110 | let mut group = c.benchmark_group("sip");
|
161 |
| - group.bench_with_input("u8", &U8_VALUE, |b, s| b.iter(|| black_box(siphash(s)))); |
162 |
| - group.bench_with_input("u16", &U16_VALUE, |b, s| b.iter(|| black_box(siphash(s)))); |
163 |
| - group.bench_with_input("u32", &U32_VALUE, |b, s| b.iter(|| black_box(siphash(s)))); |
164 |
| - group.bench_with_input("u64", &U64_VALUE, |b, s| b.iter(|| black_box(siphash(s)))); |
165 |
| - group.bench_with_input("u128", &U128_VALUE, |b, s| b.iter(|| black_box(siphash(s)))); |
166 |
| - group.bench_with_input("string", &gen_strings(), |b, s| b.iter(|| black_box(siphash(s)))); |
| 111 | + bench_inputs!(group, siphash); |
167 | 112 | }
|
168 | 113 |
|
169 | 114 | fn bench_map(c: &mut Criterion) {
|
@@ -242,32 +187,12 @@ fn bench_map(c: &mut Criterion) {
|
242 | 187 |
|
243 | 188 | criterion_main!(benches);
|
244 | 189 |
|
245 |
| -#[cfg(any( |
246 |
| - all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)), |
247 |
| - all( |
248 |
| - any(target_arch = "arm", target_arch = "aarch64"), |
249 |
| - any(target_feature = "aes", target_feature = "crypto"), |
250 |
| - not(miri), |
251 |
| - feature = "stdsimd" |
252 |
| - ) |
253 |
| -))] |
254 |
| -criterion_group!(benches, bench_ahash, bench_fx, bench_fnv, bench_sea, bench_sip); |
255 |
| - |
256 |
| -#[cfg(not(any( |
257 |
| - all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)), |
258 |
| - all( |
259 |
| - any(target_arch = "arm", target_arch = "aarch64"), |
260 |
| - any(target_feature = "aes", target_feature = "crypto"), |
261 |
| - not(miri), |
262 |
| - feature = "stdsimd" |
263 |
| - ) |
264 |
| -)))] |
265 | 190 | criterion_group!(
|
266 | 191 | benches,
|
267 |
| - bench_fallback, |
| 192 | + bench_ahash, |
268 | 193 | bench_fx,
|
269 | 194 | bench_fnv,
|
270 | 195 | bench_sea,
|
271 | 196 | bench_sip,
|
272 |
| - bench_map, |
| 197 | + bench_map |
273 | 198 | );
|
0 commit comments