1 #![cfg_attr(feature = "specialize", feature(build_hasher_simple_hash_one))]
3 use ahash
::{AHasher, RandomState}
;
6 use std
::collections
::hash_map
::DefaultHasher
;
7 use std
::hash
::{BuildHasherDefault, Hash, Hasher}
;
10 all(any(target_arch
= "x86", target_arch
= "x86_64"), target_feature
= "aes", not(miri
)),
12 any(target_arch
= "arm", target_arch
= "aarch64"),
13 any(target_feature
= "aes", target_feature
= "crypto"),
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
)
23 all(any(target_arch
= "x86", target_arch
= "x86_64"), target_feature
= "aes", not(miri
)),
25 any(target_arch
= "arm", target_arch
= "aarch64"),
26 any(target_feature
= "aes", target_feature
= "crypto"),
31 fn aeshash
<H
: Hash
>(_b
: &H
) -> u64 {
32 panic
!("aes must be enabled")
36 all(any(target_arch
= "x86", target_arch
= "x86_64"), target_feature
= "aes", not(miri
)),
38 any(target_arch
= "arm", target_arch
= "aarch64"),
39 any(target_feature
= "aes", target_feature
= "crypto"),
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
)
49 all(any(target_arch
= "x86", target_arch
= "x86_64"), target_feature
= "aes", not(miri
)),
51 any(target_arch
= "arm", target_arch
= "aarch64"),
52 any(target_feature
= "aes", target_feature
= "crypto"),
57 fn fallbackhash
<H
: Hash
>(_b
: &H
) -> u64 {
58 panic
!("aes must be disabled")
61 fn fnvhash
<H
: Hash
>(b
: &H
) -> u64 {
62 let mut hasher
= fnv
::FnvHasher
::default();
67 fn siphash
<H
: Hash
>(b
: &H
) -> u64 {
68 let mut hasher
= DefaultHasher
::default();
73 fn fxhash
<H
: Hash
>(b
: &H
) -> u64 {
74 let mut hasher
= FxHasher
::default();
79 fn seahash
<H
: Hash
>(b
: &H
) -> u64 {
80 let mut hasher
= seahash
::SeaHasher
::default();
85 const STRING_LENGTHS
: [u32; 12] = [1, 3, 4, 7, 8, 15, 16, 24, 33, 68, 132, 1024];
87 fn gen_strings() -> Vec
<String
> {
91 let mut string
= String
::default();
93 let c
= (48 + (pos
% 10) as u8) as char;
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;
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
))));
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
))));
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
))));
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
))));
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
))));
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
))));
169 fn bench_map(c
: &mut Criterion
) {
170 #[cfg(feature = "std")]
172 let mut group
= c
.benchmark_group("map");
173 group
.bench_function("aHash-alias", |b
| {
175 let hm
: ahash
::HashMap
<i32, i32> = (0..1_000_000).map(|i
| (i
, i
)).collect();
177 for i
in 0..1_000_000 {
178 if let Some(x
) = hm
.get(&i
) {
184 group
.bench_function("aHash-hashBrown", |b
| {
186 let hm
: hashbrown
::HashMap
<i32, i32> = (0..1_000_000).map(|i
| (i
, i
)).collect();
188 for i
in 0..1_000_000 {
189 if let Some(x
) = hm
.get(&i
) {
195 group
.bench_function("aHash-hashBrown-explicit", |b
| {
197 let hm
: hashbrown
::HashMap
<i32, i32, RandomState
> = (0..1_000_000).map(|i
| (i
, i
)).collect();
199 for i
in 0..1_000_000 {
200 if let Some(x
) = hm
.get(&i
) {
206 group
.bench_function("aHash-wrapper", |b
| {
208 let hm
: ahash
::AHashMap
<i32, i32> = (0..1_000_000).map(|i
| (i
, i
)).collect();
210 for i
in 0..1_000_000 {
211 if let Some(x
) = hm
.get(&i
) {
217 group
.bench_function("aHash-rand", |b
| {
219 let hm
: std
::collections
::HashMap
<i32, i32, RandomState
> = (0..1_000_000).map(|i
| (i
, i
)).collect();
221 for i
in 0..1_000_000 {
222 if let Some(x
) = hm
.get(&i
) {
228 group
.bench_function("aHash-default", |b
| {
230 let hm
: std
::collections
::HashMap
<i32, i32, BuildHasherDefault
<AHasher
>> =
231 (0..1_000_000).map(|i
| (i
, i
)).collect();
233 for i
in 0..1_000_000 {
234 if let Some(x
) = hm
.get(&i
) {
243 criterion_main
!(benches
);
246 all(any(target_arch
= "x86", target_arch
= "x86_64"), target_feature
= "aes", not(miri
)),
248 any(target_arch
= "arm", target_arch
= "aarch64"),
249 any(target_feature
= "aes", target_feature
= "crypto"),
254 criterion_group
!(benches
, bench_ahash
, bench_fx
, bench_fnv
, bench_sea
, bench_sip
);
257 all(any(target_arch
= "x86", target_arch
= "x86_64"), target_feature
= "aes", not(miri
)),
259 any(target_arch
= "arm", target_arch
= "aarch64"),
260 any(target_feature
= "aes", target_feature
= "crypto"),