Skip to content

Commit fa13faf

Browse files
Fix benches (#142)
* Fix benches * Remove mod in bench file and add comment * increase num iterations per batch
1 parent 2ae75f6 commit fa13faf

File tree

3 files changed

+37
-114
lines changed

3 files changed

+37
-114
lines changed

src/hash_map.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Borrow;
2-
use std::collections::{hash_map, HashMap};
32
use std::collections::hash_map::{IntoKeys, IntoValues};
3+
use std::collections::{hash_map, HashMap};
44
use std::fmt::{self, Debug};
55
use std::hash::{BuildHasher, Hash};
66
use std::iter::FromIterator;
@@ -14,7 +14,6 @@ use serde::{
1414
};
1515

1616
use crate::RandomState;
17-
use crate::random_state::RandomSource;
1817

1918
/// A [`HashMap`](std::collections::HashMap) using [`RandomState`](crate::RandomState) to hash the items.
2019
/// (Requires the `std` feature to be enabled.)

src/hash_set.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::RandomState;
2-
use crate::random_state::RandomSource;
32
use std::collections::{hash_set, HashSet};
43
use std::fmt::{self, Debug};
54
use std::hash::{BuildHasher, Hash};

tests/bench.rs

+36-111
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,33 @@
33
use ahash::{AHasher, RandomState};
44
use criterion::*;
55
use fxhash::FxHasher;
6+
use rand::Rng;
67
use std::collections::hash_map::DefaultHasher;
78
use std::hash::{BuildHasherDefault, Hash, Hasher};
89

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(
1112
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",
2715
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+
),
3717
all(
3818
any(target_arch = "arm", target_arch = "aarch64"),
3919
any(target_feature = "aes", target_feature = "crypto"),
4020
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 {
4530
let build_hasher = RandomState::with_seeds(1, 2, 3, 4);
4631
build_hasher.hash_one(b)
4732
}
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-
}
6033

6134
fn fnvhash<H: Hash>(b: &H) -> u64 {
6235
let mut hasher = fnv::FnvHasher::default();
@@ -98,72 +71,44 @@ fn gen_strings() -> Vec<String> {
9871
.collect()
9972
}
10073

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);
10678

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+
};
11687
}
11788

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);
12792
}
12893

12994
fn bench_fx(c: &mut Criterion) {
13095
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);
13797
}
13898

13999
fn bench_fnv(c: &mut Criterion) {
140100
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);
147102
}
148103

149104
fn bench_sea(c: &mut Criterion) {
150105
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);
157107
}
158108

159109
fn bench_sip(c: &mut Criterion) {
160110
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);
167112
}
168113

169114
fn bench_map(c: &mut Criterion) {
@@ -242,32 +187,12 @@ fn bench_map(c: &mut Criterion) {
242187

243188
criterion_main!(benches);
244189

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-
)))]
265190
criterion_group!(
266191
benches,
267-
bench_fallback,
192+
bench_ahash,
268193
bench_fx,
269194
bench_fnv,
270195
bench_sea,
271196
bench_sip,
272-
bench_map,
197+
bench_map
273198
);

0 commit comments

Comments
 (0)