forked from rust-lang/hashbrown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.rs
200 lines (178 loc) · 5.13 KB
/
bench.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// This benchmark suite contains some benchmarks along a set of dimensions:
// Hasher: std default (SipHash) and crate default (FxHash).
// Int key distribution: low bit heavy, top bit heavy, and random.
// Task: basic functionality: insert, insert_erase, lookup, lookup_fail, iter
#![feature(test)]
extern crate test;
use test::{black_box, Bencher};
use hashbrown::hash_map::DefaultHashBuilder;
use hashbrown::HashMap;
use std::collections::hash_map::RandomState;
const SIZE: usize = 1000;
// The default hashmap when using this crate directly.
type FxHashMap<K, V> = HashMap<K, V, DefaultHashBuilder>;
// This uses the hashmap from this crate with the default hasher of the stdlib.
type StdHashMap<K, V> = HashMap<K, V, RandomState>;
// A random key iterator.
#[derive(Clone, Copy)]
struct RandomKeys {
state: usize,
}
impl RandomKeys {
fn new() -> Self {
RandomKeys { state: 0 }
}
}
impl Iterator for RandomKeys {
type Item = usize;
fn next(&mut self) -> Option<usize> {
// Add 1 then multiply by some 32 bit prime.
self.state = self.state.wrapping_add(1).wrapping_mul(3787392781);
Some(self.state)
}
}
macro_rules! bench_suite {
($bench_macro:ident, $bench_fx_serial:ident, $bench_std_serial:ident,
$bench_fx_highbits:ident, $bench_std_highbits:ident,
$bench_fx_random:ident, $bench_std_random:ident) => {
$bench_macro!($bench_fx_serial, FxHashMap, 0..);
$bench_macro!($bench_std_serial, StdHashMap, 0..);
$bench_macro!($bench_fx_highbits, FxHashMap, (0..).map(usize::swap_bytes));
$bench_macro!(
$bench_std_highbits,
StdHashMap,
(0..).map(usize::swap_bytes)
);
$bench_macro!($bench_fx_random, FxHashMap, RandomKeys::new());
$bench_macro!($bench_std_random, StdHashMap, RandomKeys::new());
};
}
macro_rules! bench_insert {
($name:ident, $maptype:ident, $keydist:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
b.iter(|| {
let mut m = $maptype::default();
for i in ($keydist).take(SIZE) {
m.insert(i, i);
}
black_box(m);
})
}
};
}
bench_suite!(
bench_insert,
insert_fx_serial,
insert_std_serial,
insert_fx_highbits,
insert_std_highbits,
insert_fx_random,
insert_std_random
);
macro_rules! bench_insert_erase {
($name:ident, $maptype:ident, $keydist:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let mut m = $maptype::default();
let mut add_iter = $keydist;
for i in (&mut add_iter).take(SIZE) {
m.insert(i, i);
}
let mut remove_iter = $keydist;
b.iter(|| {
// While keeping the size constant,
// replace the first keydist with the second.
for (add, remove) in (&mut add_iter).zip(&mut remove_iter).take(SIZE) {
m.insert(add, add);
black_box(m.remove(&remove));
}
})
}
};
}
bench_suite!(
bench_insert_erase,
insert_erase_fx_serial,
insert_erase_std_serial,
insert_erase_fx_highbits,
insert_erase_std_highbits,
insert_erase_fx_random,
insert_erase_std_random
);
macro_rules! bench_lookup {
($name:ident, $maptype:ident, $keydist:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let mut m = $maptype::default();
for i in $keydist.take(SIZE) {
m.insert(i, i);
}
b.iter(|| {
for i in $keydist.take(SIZE) {
black_box(m.get(&i));
}
})
}
};
}
bench_suite!(
bench_lookup,
lookup_fx_serial,
lookup_std_serial,
lookup_fx_highbits,
lookup_std_highbits,
lookup_fx_random,
lookup_std_random
);
macro_rules! bench_lookup_fail {
($name:ident, $maptype:ident, $keydist:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let mut m = $maptype::default();
let mut iter = $keydist;
for i in (&mut iter).take(SIZE) {
m.insert(i, i);
}
b.iter(|| {
for i in (&mut iter).take(SIZE) {
black_box(m.get(&i));
}
})
}
};
}
bench_suite!(
bench_lookup_fail,
lookup_fail_fx_serial,
lookup_fail_std_serial,
lookup_fail_fx_highbits,
lookup_fail_std_highbits,
lookup_fail_fx_random,
lookup_fail_std_random
);
macro_rules! bench_iter {
($name:ident, $maptype:ident, $keydist:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let mut m = $maptype::default();
for i in ($keydist).take(SIZE) {
m.insert(i, i);
}
b.iter(|| {
for i in &m {
black_box(i);
}
})
}
};
}
bench_suite!(
bench_iter,
iter_fx_serial,
iter_std_serial,
iter_fx_highbits,
iter_std_highbits,
iter_fx_random,
iter_std_random
);