]> git.proxmox.com Git - rustc.git/blob - vendor/ahash/tests/bench.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / vendor / ahash / tests / bench.rs
1 #![cfg_attr(feature = "specialize", feature(build_hasher_simple_hash_one))]
2
3 use ahash::{AHasher, RandomState};
4 use criterion::*;
5 use fxhash::FxHasher;
6 use std::collections::hash_map::DefaultHasher;
7 use std::hash::{BuildHasherDefault, Hash, Hasher};
8
9 #[cfg(any(
10 all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
11 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"),
27 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)),
37 all(
38 any(target_arch = "arm", target_arch = "aarch64"),
39 any(target_feature = "aes", target_feature = "crypto"),
40 not(miri),
41 feature = "stdsimd"
42 )
43 )))]
44 fn fallbackhash<H: Hash>(b: &H) -> u64 {
45 let build_hasher = RandomState::with_seeds(1, 2, 3, 4);
46 build_hasher.hash_one(b)
47 }
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
61 fn fnvhash<H: Hash>(b: &H) -> u64 {
62 let mut hasher = fnv::FnvHasher::default();
63 b.hash(&mut hasher);
64 hasher.finish()
65 }
66
67 fn siphash<H: Hash>(b: &H) -> u64 {
68 let mut hasher = DefaultHasher::default();
69 b.hash(&mut hasher);
70 hasher.finish()
71 }
72
73 fn fxhash<H: Hash>(b: &H) -> u64 {
74 let mut hasher = FxHasher::default();
75 b.hash(&mut hasher);
76 hasher.finish()
77 }
78
79 fn seahash<H: Hash>(b: &H) -> u64 {
80 let mut hasher = seahash::SeaHasher::default();
81 b.hash(&mut hasher);
82 hasher.finish()
83 }
84
85 const STRING_LENGTHS: [u32; 12] = [1, 3, 4, 7, 8, 15, 16, 24, 33, 68, 132, 1024];
86
87 fn gen_strings() -> Vec<String> {
88 STRING_LENGTHS
89 .iter()
90 .map(|len| {
91 let mut string = String::default();
92 for pos in 1..=*len {
93 let c = (48 + (pos % 10) as u8) as char;
94 string.push(c);
95 }
96 string
97 })
98 .collect()
99 }
100
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;
106
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))));
116 }
117
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))));
127 }
128
129 fn bench_fx(c: &mut Criterion) {
130 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))));
137 }
138
139 fn bench_fnv(c: &mut Criterion) {
140 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))));
147 }
148
149 fn bench_sea(c: &mut Criterion) {
150 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))));
157 }
158
159 fn bench_sip(c: &mut Criterion) {
160 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))));
167 }
168
169 fn bench_map(c: &mut Criterion) {
170 #[cfg(feature = "std")]
171 {
172 let mut group = c.benchmark_group("map");
173 group.bench_function("aHash-alias", |b| {
174 b.iter(|| {
175 let hm: ahash::HashMap<i32, i32> = (0..1_000_000).map(|i| (i, i)).collect();
176 let mut sum = 0;
177 for i in 0..1_000_000 {
178 if let Some(x) = hm.get(&i) {
179 sum += x;
180 }
181 }
182 })
183 });
184 group.bench_function("aHash-hashBrown", |b| {
185 b.iter(|| {
186 let hm: hashbrown::HashMap<i32, i32> = (0..1_000_000).map(|i| (i, i)).collect();
187 let mut sum = 0;
188 for i in 0..1_000_000 {
189 if let Some(x) = hm.get(&i) {
190 sum += x;
191 }
192 }
193 })
194 });
195 group.bench_function("aHash-hashBrown-explicit", |b| {
196 b.iter(|| {
197 let hm: hashbrown::HashMap<i32, i32, RandomState> = (0..1_000_000).map(|i| (i, i)).collect();
198 let mut sum = 0;
199 for i in 0..1_000_000 {
200 if let Some(x) = hm.get(&i) {
201 sum += x;
202 }
203 }
204 })
205 });
206 group.bench_function("aHash-wrapper", |b| {
207 b.iter(|| {
208 let hm: ahash::AHashMap<i32, i32> = (0..1_000_000).map(|i| (i, i)).collect();
209 let mut sum = 0;
210 for i in 0..1_000_000 {
211 if let Some(x) = hm.get(&i) {
212 sum += x;
213 }
214 }
215 })
216 });
217 group.bench_function("aHash-rand", |b| {
218 b.iter(|| {
219 let hm: std::collections::HashMap<i32, i32, RandomState> = (0..1_000_000).map(|i| (i, i)).collect();
220 let mut sum = 0;
221 for i in 0..1_000_000 {
222 if let Some(x) = hm.get(&i) {
223 sum += x;
224 }
225 }
226 })
227 });
228 group.bench_function("aHash-default", |b| {
229 b.iter(|| {
230 let hm: std::collections::HashMap<i32, i32, BuildHasherDefault<AHasher>> =
231 (0..1_000_000).map(|i| (i, i)).collect();
232 let mut sum = 0;
233 for i in 0..1_000_000 {
234 if let Some(x) = hm.get(&i) {
235 sum += x;
236 }
237 }
238 })
239 });
240 }
241 }
242
243 criterion_main!(benches);
244
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 criterion_group!(
266 benches,
267 bench_fallback,
268 bench_fx,
269 bench_fnv,
270 bench_sea,
271 bench_sip,
272 bench_map,
273 );