From 2fe4128b1b91a9a132596c01521a14d769b8e9c7 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 20 Oct 2025 09:50:33 -0400 Subject: [PATCH] refactor(header): inline FNV hasher to reduce dependencies --- Cargo.toml | 1 - src/header/map.rs | 30 +++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1bc883aa..c66c6fa4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,6 @@ std = [] [dependencies] bytes = "1" -fnv = "1.0.5" itoa = "1" [dev-dependencies] diff --git a/src/header/map.rs b/src/header/map.rs index e670f484..af6b623b 100644 --- a/src/header/map.rs +++ b/src/header/map.rs @@ -3654,8 +3654,6 @@ fn hash_elem_using(danger: &Danger, k: &K) -> HashValue where K: Hash + ?Sized, { - use fnv::FnvHasher; - const MASK: u64 = (MAX_SIZE as u64) - 1; let hash = match *danger { @@ -3667,7 +3665,7 @@ where } // Fast hash _ => { - let mut h = FnvHasher::default(); + let mut h = FnvHasher::new(); k.hash(&mut h); h.finish() } @@ -3676,6 +3674,32 @@ where HashValue((hash & MASK) as u16) } +struct FnvHasher(u64); + +impl FnvHasher { + #[inline] + fn new() -> Self { + FnvHasher(0xcbf29ce484222325) + } +} + +impl std::hash::Hasher for FnvHasher { + #[inline] + fn finish(&self) -> u64 { + self.0 + } + + #[inline] + fn write(&mut self, bytes: &[u8]) { + let mut hash = self.0; + for &b in bytes { + hash = hash ^ (b as u64); + hash = hash.wrapping_mul(0x100000001b3); + } + self.0 = hash; + } +} + /* * * ===== impl IntoHeaderName / AsHeaderName =====