Skip to content

Commit 83ac6fd

Browse files
committed
Auto merge of #233 - Marwes:rehash, r=Amanieu
test: Add a test for rehash_in_place No tests ran the `rehash_in_place` method before and I could not come up with a way to trigger the rehash from the higher level structures so explicitly calling the method is the best I could do.
2 parents 3fcb5fd + 4b21bc6 commit 83ac6fd

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

benches/bench.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extern crate test;
99
use test::{black_box, Bencher};
1010

1111
use hashbrown::hash_map::DefaultHashBuilder;
12-
use hashbrown::HashMap;
12+
use hashbrown::{HashMap, HashSet};
1313
use std::{
1414
collections::hash_map::RandomState,
1515
sync::atomic::{self, AtomicUsize},
@@ -303,3 +303,29 @@ fn clone_from_large(b: &mut Bencher) {
303303
black_box(&mut m2);
304304
})
305305
}
306+
307+
#[bench]
308+
fn rehash_in_place(b: &mut Bencher) {
309+
b.iter(|| {
310+
let mut set = HashSet::new();
311+
312+
// Each loop triggers one rehash
313+
for _ in 0..10 {
314+
for i in 0..224 {
315+
set.insert(i);
316+
}
317+
318+
assert_eq!(
319+
set.capacity(),
320+
224,
321+
"The set must be at or close to capacity to trigger a re hashing"
322+
);
323+
324+
for i in 100..1400 {
325+
set.remove(&(i - 100));
326+
set.insert(i);
327+
}
328+
set.clear();
329+
}
330+
});
331+
}

src/raw/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,3 +2161,33 @@ impl<'a, A: Allocator + Clone> Iterator for RawIterHashInner<'a, A> {
21612161
}
21622162
}
21632163
}
2164+
2165+
#[cfg(test)]
2166+
mod test_map {
2167+
use super::*;
2168+
2169+
#[test]
2170+
fn rehash() {
2171+
let mut table = RawTable::new();
2172+
let hasher = |i: &u64| *i;
2173+
for i in 0..100 {
2174+
table.insert(i, i, hasher);
2175+
}
2176+
2177+
for i in 0..100 {
2178+
unsafe {
2179+
assert_eq!(table.find(i, |x| *x == i).map(|b| b.read()), Some(i));
2180+
}
2181+
assert!(table.find(i + 100, |x| *x == i + 100).is_none());
2182+
}
2183+
2184+
table.rehash_in_place(hasher);
2185+
2186+
for i in 0..100 {
2187+
unsafe {
2188+
assert_eq!(table.find(i, |x| *x == i).map(|b| b.read()), Some(i));
2189+
}
2190+
assert!(table.find(i + 100, |x| *x == i + 100).is_none());
2191+
}
2192+
}
2193+
}

src/set.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,4 +2250,24 @@ mod test_set {
22502250
set.insert(19);
22512251
assert!(set.contains(&19));
22522252
}
2253+
2254+
#[test]
2255+
fn rehash_in_place() {
2256+
let mut set = HashSet::new();
2257+
2258+
for i in 0..224 {
2259+
set.insert(i);
2260+
}
2261+
2262+
assert_eq!(
2263+
set.capacity(),
2264+
224,
2265+
"The set must be at or close to capacity to trigger a re hashing"
2266+
);
2267+
2268+
for i in 100..1400 {
2269+
set.remove(&(i - 100));
2270+
set.insert(i);
2271+
}
2272+
}
22532273
}

0 commit comments

Comments
 (0)