@@ -10,7 +10,10 @@ use test::{black_box, Bencher};
10
10
11
11
use hashbrown:: hash_map:: DefaultHashBuilder ;
12
12
use hashbrown:: HashMap ;
13
- use std:: collections:: hash_map:: RandomState ;
13
+ use std:: {
14
+ collections:: hash_map:: RandomState ,
15
+ sync:: atomic:: { self , AtomicUsize } ,
16
+ } ;
14
17
15
18
const SIZE : usize = 1000 ;
16
19
@@ -40,6 +43,20 @@ impl Iterator for RandomKeys {
40
43
}
41
44
}
42
45
46
+ // Just an arbitrary side effect to make the maps not shortcircuit to the non-dropping path
47
+ // when dropping maps/entries (most real world usages likely have drop in the key or value)
48
+ lazy_static:: lazy_static! {
49
+ static ref SIDE_EFFECT : AtomicUsize = AtomicUsize :: new( 0 ) ;
50
+ }
51
+
52
+ #[ derive( Clone ) ]
53
+ struct DropType ( usize ) ;
54
+ impl Drop for DropType {
55
+ fn drop ( & mut self ) {
56
+ SIDE_EFFECT . fetch_add ( self . 0 , atomic:: Ordering :: SeqCst ) ;
57
+ }
58
+ }
59
+
43
60
macro_rules! bench_suite {
44
61
( $bench_macro: ident, $bench_ahash_serial: ident, $bench_std_serial: ident,
45
62
$bench_ahash_highbits: ident, $bench_std_highbits: ident,
@@ -69,10 +86,11 @@ macro_rules! bench_insert {
69
86
b. iter( || {
70
87
m. clear( ) ;
71
88
for i in ( $keydist) . take( SIZE ) {
72
- m. insert( i, i ) ;
89
+ m. insert( i, DropType ( i ) ) ;
73
90
}
74
91
black_box( & mut m) ;
75
- } )
92
+ } ) ;
93
+ eprintln!( "{}" , SIDE_EFFECT . load( atomic:: Ordering :: SeqCst ) ) ;
76
94
}
77
95
} ;
78
96
}
@@ -93,7 +111,7 @@ macro_rules! bench_insert_erase {
93
111
fn $name( b: & mut Bencher ) {
94
112
let mut base = $maptype:: default ( ) ;
95
113
for i in ( $keydist) . take( SIZE ) {
96
- base. insert( i, i ) ;
114
+ base. insert( i, DropType ( i ) ) ;
97
115
}
98
116
let skip = $keydist. skip( SIZE ) ;
99
117
b. iter( || {
@@ -103,11 +121,12 @@ macro_rules! bench_insert_erase {
103
121
// While keeping the size constant,
104
122
// replace the first keydist with the second.
105
123
for ( add, remove) in ( & mut add_iter) . zip( & mut remove_iter) . take( SIZE ) {
106
- m. insert( add, add) ;
124
+ m. insert( add, DropType ( add) ) ;
107
125
black_box( m. remove( & remove) ) ;
108
126
}
109
127
black_box( m) ;
110
- } )
128
+ } ) ;
129
+ eprintln!( "{}" , SIDE_EFFECT . load( atomic:: Ordering :: SeqCst ) ) ;
111
130
}
112
131
} ;
113
132
}
@@ -128,14 +147,15 @@ macro_rules! bench_lookup {
128
147
fn $name( b: & mut Bencher ) {
129
148
let mut m = $maptype:: default ( ) ;
130
149
for i in $keydist. take( SIZE ) {
131
- m. insert( i, i ) ;
150
+ m. insert( i, DropType ( i ) ) ;
132
151
}
133
152
134
153
b. iter( || {
135
154
for i in $keydist. take( SIZE ) {
136
155
black_box( m. get( & i) ) ;
137
156
}
138
- } )
157
+ } ) ;
158
+ eprintln!( "{}" , SIDE_EFFECT . load( atomic:: Ordering :: SeqCst ) ) ;
139
159
}
140
160
} ;
141
161
}
@@ -157,7 +177,7 @@ macro_rules! bench_lookup_fail {
157
177
let mut m = $maptype:: default ( ) ;
158
178
let mut iter = $keydist;
159
179
for i in ( & mut iter) . take( SIZE ) {
160
- m. insert( i, i ) ;
180
+ m. insert( i, DropType ( i ) ) ;
161
181
}
162
182
163
183
b. iter( || {
@@ -185,7 +205,7 @@ macro_rules! bench_iter {
185
205
fn $name( b: & mut Bencher ) {
186
206
let mut m = $maptype:: default ( ) ;
187
207
for i in ( $keydist) . take( SIZE ) {
188
- m. insert( i, i ) ;
208
+ m. insert( i, DropType ( i ) ) ;
189
209
}
190
210
191
211
b. iter( || {
@@ -211,7 +231,7 @@ bench_suite!(
211
231
fn clone_small ( b : & mut Bencher ) {
212
232
let mut m = HashMap :: new ( ) ;
213
233
for i in 0 ..10 {
214
- m. insert ( i, i ) ;
234
+ m. insert ( i, DropType ( i ) ) ;
215
235
}
216
236
217
237
b. iter ( || {
@@ -224,7 +244,7 @@ fn clone_from_small(b: &mut Bencher) {
224
244
let mut m = HashMap :: new ( ) ;
225
245
let mut m2 = HashMap :: new ( ) ;
226
246
for i in 0 ..10 {
227
- m. insert ( i, i ) ;
247
+ m. insert ( i, DropType ( i ) ) ;
228
248
}
229
249
230
250
b. iter ( || {
@@ -237,7 +257,7 @@ fn clone_from_small(b: &mut Bencher) {
237
257
fn clone_large ( b : & mut Bencher ) {
238
258
let mut m = HashMap :: new ( ) ;
239
259
for i in 0 ..1000 {
240
- m. insert ( i, i ) ;
260
+ m. insert ( i, DropType ( i ) ) ;
241
261
}
242
262
243
263
b. iter ( || {
@@ -250,7 +270,7 @@ fn clone_from_large(b: &mut Bencher) {
250
270
let mut m = HashMap :: new ( ) ;
251
271
let mut m2 = HashMap :: new ( ) ;
252
272
for i in 0 ..1000 {
253
- m. insert ( i, i ) ;
273
+ m. insert ( i, DropType ( i ) ) ;
254
274
}
255
275
256
276
b. iter ( || {
0 commit comments