Skip to content

Commit

Permalink
Merge #56 #62
Browse files Browse the repository at this point in the history
56: Add additional benchmarks. r=Amanieu a=tkaitchuck

This covers performance of three cases I wanted to study when looking into https://github.com/Amanieu/hashbrown/issues/48

They are:
`grow_by_insertion_kb` which is similar to grow by insertion, but instead of every entry differing by 1, they differ by 1024. This makes an important performance difference to the hasher.

`find_existing_high_bits` which is similar to find_existing but uses 64 bit keys instead of 32 bit keys, where the lower 32 bits are zeros. This is a pathologically bad case for FxHash.

`insert_8_char_string` tests a case where the key is a string. (As opposed to all the existing tests which operate on u32 values. This is important to cover because strings as keys are very common.

62: Remove incorrect debug_assert r=Amanieu a=Amanieu

Fixes #60 

Co-authored-by: Tom Kaitchuck <tom.kaitchuck@dell.com>
Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
  • Loading branch information
3 people committed Apr 13, 2019
3 parents ab3e4ea + 1cf0503 + aea2f62 commit 5adef52
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
46 changes: 46 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ fn grow_by_insertion(b: &mut Bencher) {
});
}

#[bench]
fn grow_by_insertion_kb(b: &mut Bencher) {
let mut m = new_map();
let kb = 1024;
for i in 1..1001 {
m.insert(i * kb, i);
}

let mut k = 1001 * kb;

b.iter(|| {
m.insert(k, k);
k += kb;
});
}

#[bench]
fn find_existing(b: &mut Bencher) {
let mut m = new_map();
Expand All @@ -61,6 +77,21 @@ fn find_existing(b: &mut Bencher) {
});
}

#[bench]
fn find_existing_high_bits(b: &mut Bencher) {
let mut m = new_map();

for i in 1..1001_u64 {
m.insert(i << 32, i);
}

b.iter(|| {
for i in 1..1001_u64 {
m.contains_key(&(i << 32));
}
});
}

#[bench]
fn find_nonexisting(b: &mut Bencher) {
let mut m = new_map();
Expand Down Expand Up @@ -111,3 +142,18 @@ fn get_remove_insert(b: &mut Bencher) {
k += 1;
})
}

#[bench]
fn insert_8_char_string(b: &mut Bencher) {
let mut strings: Vec<_> = Vec::new();
for i in 1..1001 {
strings.push(format!("{:x}", -i));
}

let mut m = new_map();
b.iter(|| {
for key in &strings {
m.insert(key, key);
}
})
}
7 changes: 2 additions & 5 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ fn special_is_empty(ctrl: u8) -> bool {
#[inline]
#[allow(clippy::cast_possible_truncation)]
fn h1(hash: u64) -> usize {
#[cfg(target_pointer_width = "32")]
{
debug_assert!(hash <= u64::from(u32::max_value()));
}
hash as usize // truncation
// On 32-bit platforms we simply ignore the higher hash bits.
hash as usize
}

/// Secondary hash function, saved in the low 7 bits of the control byte.
Expand Down

0 comments on commit 5adef52

Please sign in to comment.