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};
6
7 #[cfg(any(
8 all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
9 all(any(target_arch = "arm", target_arch = "aarch64"), target_feature = "crypto", not(miri), feature = "stdsimd")
10 ))]
aeshash<H: Hash>(b: &H) -> u6411 fn aeshash<H: Hash>(b: &H) -> u64 {
12 let build_hasher = RandomState::with_seeds(1, 2, 3, 4);
13 H::get_hash(b, &build_hasher)
14 }
15 #[cfg(not(any(
16 all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
17 all(any(target_arch = "arm", target_arch = "aarch64"), target_feature = "crypto", not(miri), feature = "stdsimd")
18 )))]
aeshash<H: Hash>(_b: &H) -> u6419 fn aeshash<H: Hash>(_b: &H) -> u64 {
20 panic!("aes must be enabled")
21 }
22
23 #[cfg(not(any(
24 all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
25 all(any(target_arch = "arm", target_arch = "aarch64"), target_feature = "crypto", not(miri), feature = "stdsimd")
26 )))]
fallbackhash<H: Hash>(b: &H) -> u6427 fn fallbackhash<H: Hash>(b: &H) -> u64 {
28 let build_hasher = RandomState::with_seeds(1, 2, 3, 4);
29 H::get_hash(b, &build_hasher)
30 }
31 #[cfg(any(
32 all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
33 all(any(target_arch = "arm", target_arch = "aarch64"), target_feature = "crypto", not(miri), feature = "stdsimd")
34 ))]
fallbackhash<H: Hash>(_b: &H) -> u6435 fn fallbackhash<H: Hash>(_b: &H) -> u64 {
36 panic!("aes must be disabled")
37 }
38
fnvhash<H: Hash>(b: &H) -> u6439 fn fnvhash<H: Hash>(b: &H) -> u64 {
40 let mut hasher = fnv::FnvHasher::default();
41 b.hash(&mut hasher);
42 hasher.finish()
43 }
44
siphash<H: Hash>(b: &H) -> u6445 fn siphash<H: Hash>(b: &H) -> u64 {
46 let mut hasher = DefaultHasher::default();
47 b.hash(&mut hasher);
48 hasher.finish()
49 }
50
fxhash<H: Hash>(b: &H) -> u6451 fn fxhash<H: Hash>(b: &H) -> u64 {
52 let mut hasher = FxHasher::default();
53 b.hash(&mut hasher);
54 hasher.finish()
55 }
56
seahash<H: Hash>(b: &H) -> u6457 fn seahash<H: Hash>(b: &H) -> u64 {
58 let mut hasher = seahash::SeaHasher::default();
59 b.hash(&mut hasher);
60 hasher.finish()
61 }
62
63 const STRING_LENGTHS: [u32; 12] = [1, 3, 4, 7, 8, 15, 16, 24, 33, 68, 132, 1024];
64
gen_strings() -> Vec<String>65 fn gen_strings() -> Vec<String> {
66 STRING_LENGTHS
67 .iter()
68 .map(|len| {
69 let mut string = String::default();
70 for pos in 1..=*len {
71 let c = (48 + (pos % 10) as u8) as char;
72 string.push(c);
73 }
74 string
75 })
76 .collect()
77 }
78
79 const U8_VALUE: u8 = 123;
80 const U16_VALUE: u16 = 1234;
81 const U32_VALUE: u32 = 12345678;
82 const U64_VALUE: u64 = 1234567890123456;
83 const U128_VALUE: u128 = 12345678901234567890123456789012;
84
bench_ahash(c: &mut Criterion)85 fn bench_ahash(c: &mut Criterion) {
86 let mut group = c.benchmark_group("aeshash");
87 group.bench_with_input("u8", &U8_VALUE, |b, s| b.iter(|| black_box(aeshash(s))));
88 group.bench_with_input("u16", &U16_VALUE, |b, s| b.iter(|| black_box(aeshash(s))));
89 group.bench_with_input("u32", &U32_VALUE, |b, s| b.iter(|| black_box(aeshash(s))));
90 group.bench_with_input("u64", &U64_VALUE, |b, s| b.iter(|| black_box(aeshash(s))));
91 group.bench_with_input("u128", &U128_VALUE, |b, s| b.iter(|| black_box(aeshash(s))));
92 group.bench_with_input("string", &gen_strings(), |b, s| b.iter(|| black_box(aeshash(s))));
93 }
94
bench_fallback(c: &mut Criterion)95 fn bench_fallback(c: &mut Criterion) {
96 let mut group = c.benchmark_group("fallback");
97 group.bench_with_input("u8", &U8_VALUE, |b, s| b.iter(|| black_box(fallbackhash(s))));
98 group.bench_with_input("u16", &U16_VALUE, |b, s| b.iter(|| black_box(fallbackhash(s))));
99 group.bench_with_input("u32", &U32_VALUE, |b, s| b.iter(|| black_box(fallbackhash(s))));
100 group.bench_with_input("u64", &U64_VALUE, |b, s| b.iter(|| black_box(fallbackhash(s))));
101 group.bench_with_input("u128", &U128_VALUE, |b, s| b.iter(|| black_box(fallbackhash(s))));
102 group.bench_with_input("string", &gen_strings(), |b, s| b.iter(|| black_box(fallbackhash(s))));
103 }
104
bench_fx(c: &mut Criterion)105 fn bench_fx(c: &mut Criterion) {
106 let mut group = c.benchmark_group("fx");
107 group.bench_with_input("u8", &U8_VALUE, |b, s| b.iter(|| black_box(fxhash(s))));
108 group.bench_with_input("u16", &U16_VALUE, |b, s| b.iter(|| black_box(fxhash(s))));
109 group.bench_with_input("u32", &U32_VALUE, |b, s| b.iter(|| black_box(fxhash(s))));
110 group.bench_with_input("u64", &U64_VALUE, |b, s| b.iter(|| black_box(fxhash(s))));
111 group.bench_with_input("u128", &U128_VALUE, |b, s| b.iter(|| black_box(fxhash(s))));
112 group.bench_with_input("string", &gen_strings(), |b, s| b.iter(|| black_box(fxhash(s))));
113 }
114
bench_fnv(c: &mut Criterion)115 fn bench_fnv(c: &mut Criterion) {
116 let mut group = c.benchmark_group("fnv");
117 group.bench_with_input("u8", &U8_VALUE, |b, s| b.iter(|| black_box(fnvhash(s))));
118 group.bench_with_input("u16", &U16_VALUE, |b, s| b.iter(|| black_box(fnvhash(s))));
119 group.bench_with_input("u32", &U32_VALUE, |b, s| b.iter(|| black_box(fnvhash(s))));
120 group.bench_with_input("u64", &U64_VALUE, |b, s| b.iter(|| black_box(fnvhash(s))));
121 group.bench_with_input("u128", &U128_VALUE, |b, s| b.iter(|| black_box(fnvhash(s))));
122 group.bench_with_input("string", &gen_strings(), |b, s| b.iter(|| black_box(fnvhash(s))));
123 }
124
bench_sea(c: &mut Criterion)125 fn bench_sea(c: &mut Criterion) {
126 let mut group = c.benchmark_group("sea");
127 group.bench_with_input("u8", &U8_VALUE, |b, s| b.iter(|| black_box(seahash(s))));
128 group.bench_with_input("u16", &U16_VALUE, |b, s| b.iter(|| black_box(seahash(s))));
129 group.bench_with_input("u32", &U32_VALUE, |b, s| b.iter(|| black_box(seahash(s))));
130 group.bench_with_input("u64", &U64_VALUE, |b, s| b.iter(|| black_box(seahash(s))));
131 group.bench_with_input("u128", &U128_VALUE, |b, s| b.iter(|| black_box(seahash(s))));
132 group.bench_with_input("string", &gen_strings(), |b, s| b.iter(|| black_box(seahash(s))));
133 }
134
bench_sip(c: &mut Criterion)135 fn bench_sip(c: &mut Criterion) {
136 let mut group = c.benchmark_group("sip");
137 group.bench_with_input("u8", &U8_VALUE, |b, s| b.iter(|| black_box(siphash(s))));
138 group.bench_with_input("u16", &U16_VALUE, |b, s| b.iter(|| black_box(siphash(s))));
139 group.bench_with_input("u32", &U32_VALUE, |b, s| b.iter(|| black_box(siphash(s))));
140 group.bench_with_input("u64", &U64_VALUE, |b, s| b.iter(|| black_box(siphash(s))));
141 group.bench_with_input("u128", &U128_VALUE, |b, s| b.iter(|| black_box(siphash(s))));
142 group.bench_with_input("string", &gen_strings(), |b, s| b.iter(|| black_box(siphash(s))));
143 }
144
145 criterion_main!(benches);
146 criterion_group!(
147 benches,
148 bench_ahash,
149 bench_fallback,
150 bench_fx,
151 bench_fnv,
152 bench_sea,
153 bench_sip
154 );
155