Skip to content

Commit dfb4bf2

Browse files
committed
Hash up to 8 bytes at once with FxHasher
1 parent 29ffe51 commit dfb4bf2

File tree

1 file changed

+19
-4
lines changed
  • src/librustc_data_structures

1 file changed

+19
-4
lines changed

src/librustc_data_structures/fx.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::collections::{HashMap, HashSet};
1212
use std::default::Default;
1313
use std::hash::{Hasher, Hash, BuildHasherDefault};
1414
use std::ops::BitXor;
15+
use std::mem::size_of;
1516

1617
pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
1718
pub type FxHashSet<V> = HashSet<V, BuildHasherDefault<FxHasher>>;
@@ -62,10 +63,24 @@ impl FxHasher {
6263

6364
impl Hasher for FxHasher {
6465
#[inline]
65-
fn write(&mut self, bytes: &[u8]) {
66-
for byte in bytes {
67-
let i = *byte;
68-
self.add_to_hash(i as usize);
66+
fn write(&mut self, mut bytes: &[u8]) {
67+
unsafe {
68+
assert!(size_of::<usize>() <= 8);
69+
while bytes.len() >= size_of::<usize>() {
70+
self.add_to_hash(*(bytes.as_ptr() as *const usize));
71+
bytes = &bytes[size_of::<usize>()..];
72+
}
73+
if (size_of::<usize>() > 4) && (bytes.len() >= 4) {
74+
self.add_to_hash(*(bytes.as_ptr() as *const u32) as usize);
75+
bytes = &bytes[4..];
76+
}
77+
if (size_of::<usize>() > 2) && bytes.len() >= 2 {
78+
self.add_to_hash(*(bytes.as_ptr() as *const u16) as usize);
79+
bytes = &bytes[2..];
80+
}
81+
if (size_of::<usize>() > 1) && bytes.len() >= 1 {
82+
self.add_to_hash(bytes[0] as usize);
83+
}
6984
}
7085
}
7186

0 commit comments

Comments
 (0)