Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement compile-time hashing for HeaderName::from_static #659

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 7 additions & 13 deletions src/header/map.rs
Expand Up @@ -8,7 +8,7 @@ use std::{fmt, mem, ops, ptr, vec};

use crate::Error;

use super::name::{HdrName, HeaderName, InvalidHeaderName};
use super::name::{FastHash, HdrName, HeaderName, InvalidHeaderName};
use super::HeaderValue;

pub use self::as_header_name::AsHeaderName;
Expand Down Expand Up @@ -1077,7 +1077,7 @@ impl<T> HeaderMap<T> {

fn entry2<K>(&mut self, key: K) -> Entry<'_, T>
where
K: Hash + Into<HeaderName>,
K: FastHash + Into<HeaderName>,
HeaderName: PartialEq<K>,
{
// Ensure that there is space in the map
Expand Down Expand Up @@ -1149,7 +1149,7 @@ impl<T> HeaderMap<T> {
#[inline]
fn insert2<K>(&mut self, key: K, value: T) -> Option<T>
where
K: Hash + Into<HeaderName>,
K: FastHash + Into<HeaderName>,
HeaderName: PartialEq<K>,
{
self.reserve_one();
Expand Down Expand Up @@ -1250,7 +1250,7 @@ impl<T> HeaderMap<T> {
#[inline]
fn append2<K>(&mut self, key: K, value: T) -> bool
where
K: Hash + Into<HeaderName>,
K: FastHash + Into<HeaderName>,
HeaderName: PartialEq<K>,
{
self.reserve_one();
Expand Down Expand Up @@ -1287,7 +1287,7 @@ impl<T> HeaderMap<T> {
#[inline]
fn find<K: ?Sized>(&self, key: &K) -> Option<(usize, usize)>
where
K: Hash + Into<HeaderName>,
K: FastHash + Into<HeaderName>,
HeaderName: PartialEq<K>,
{
if self.entries.is_empty() {
Expand Down Expand Up @@ -3272,10 +3272,8 @@ fn probe_distance(mask: Size, hash: HashValue, current: usize) -> usize {

fn hash_elem_using<K: ?Sized>(danger: &Danger, k: &K) -> HashValue
where
K: Hash,
K: FastHash,
{
use fnv::FnvHasher;

const MASK: u64 = (MAX_SIZE as u64) - 1;

let hash = match *danger {
Expand All @@ -3286,11 +3284,7 @@ where
h.finish()
}
// Fast hash
_ => {
let mut h = FnvHasher::default();
k.hash(&mut h);
h.finish()
}
_ => k.fast_hash()
};

HashValue((hash & MASK) as u16)
Expand Down