Skip to content

Commit 50b009c

Browse files
authored
refactor(header): inline FNV hasher to reduce dependencies (#796)
1 parent b370d36 commit 50b009c

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ std = []
3737

3838
[dependencies]
3939
bytes = "1"
40-
fnv = "1.0.5"
4140
itoa = "1"
4241

4342
[dev-dependencies]

src/header/map.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3654,8 +3654,6 @@ fn hash_elem_using<K>(danger: &Danger, k: &K) -> HashValue
36543654
where
36553655
K: Hash + ?Sized,
36563656
{
3657-
use fnv::FnvHasher;
3658-
36593657
const MASK: u64 = (MAX_SIZE as u64) - 1;
36603658

36613659
let hash = match *danger {
@@ -3667,7 +3665,7 @@ where
36673665
}
36683666
// Fast hash
36693667
_ => {
3670-
let mut h = FnvHasher::default();
3668+
let mut h = FnvHasher::new();
36713669
k.hash(&mut h);
36723670
h.finish()
36733671
}
@@ -3676,6 +3674,32 @@ where
36763674
HashValue((hash & MASK) as u16)
36773675
}
36783676

3677+
struct FnvHasher(u64);
3678+
3679+
impl FnvHasher {
3680+
#[inline]
3681+
fn new() -> Self {
3682+
FnvHasher(0xcbf29ce484222325)
3683+
}
3684+
}
3685+
3686+
impl std::hash::Hasher for FnvHasher {
3687+
#[inline]
3688+
fn finish(&self) -> u64 {
3689+
self.0
3690+
}
3691+
3692+
#[inline]
3693+
fn write(&mut self, bytes: &[u8]) {
3694+
let mut hash = self.0;
3695+
for &b in bytes {
3696+
hash = hash ^ (b as u64);
3697+
hash = hash.wrapping_mul(0x100000001b3);
3698+
}
3699+
self.0 = hash;
3700+
}
3701+
}
3702+
36793703
/*
36803704
*
36813705
* ===== impl IntoHeaderName / AsHeaderName =====

0 commit comments

Comments
 (0)