-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hashed utility for ValueMap to improve hashing performance
- Loading branch information
Showing
4 changed files
with
222 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
use std::{ | ||
borrow::{Borrow, Cow}, | ||
hash::{BuildHasher, Hash, Hasher}, | ||
ops::Deref, | ||
}; | ||
|
||
use rustc_hash::*; | ||
|
||
/// Hash value only once, works with references and owned types. | ||
pub(crate) struct Hashed<'a, T> | ||
where | ||
T: ToOwned + ?Sized, | ||
{ | ||
value: Cow<'a, T>, | ||
hash: u64, | ||
} | ||
|
||
impl<'a, T> Hashed<'a, T> | ||
where | ||
T: ToOwned + Hash + ?Sized, | ||
{ | ||
pub(crate) fn from_borrowed(value: &'a T) -> Self { | ||
let mut hasher = FxHasher::default(); | ||
value.hash(&mut hasher); | ||
Self { | ||
value: Cow::Borrowed(value), | ||
hash: hasher.finish(), | ||
} | ||
} | ||
|
||
pub(crate) fn from_owned(value: <T as ToOwned>::Owned) -> Self { | ||
let hash = calc_hash(value.borrow()); | ||
Self { | ||
value: Cow::Owned(value), | ||
hash, | ||
} | ||
} | ||
|
||
pub(crate) fn mutate(self, f: impl FnOnce(&mut <T as ToOwned>::Owned)) -> Hashed<'static, T> { | ||
let mut value = self.value.into_owned(); | ||
f(&mut value); | ||
let hash = calc_hash(value.borrow()); | ||
Hashed { | ||
value: Cow::Owned(value), | ||
hash, | ||
} | ||
} | ||
|
||
pub(crate) fn into_owned(self) -> Hashed<'static, T> { | ||
let value = self.value.into_owned(); | ||
Hashed { | ||
value: Cow::Owned(value), | ||
hash: self.hash, | ||
} | ||
} | ||
|
||
pub(crate) fn into_inner_owned(self) -> T::Owned { | ||
self.value.into_owned() | ||
} | ||
} | ||
|
||
fn calc_hash<T>(value: T) -> u64 | ||
where | ||
T: Hash, | ||
{ | ||
let mut hasher = FxHasher::default(); | ||
value.hash(&mut hasher); | ||
hasher.finish() | ||
} | ||
|
||
impl<T> Clone for Hashed<'_, T> | ||
where | ||
T: ToOwned + ?Sized, | ||
{ | ||
fn clone(&self) -> Self { | ||
Self { | ||
value: self.value.clone(), | ||
hash: self.hash, | ||
} | ||
} | ||
|
||
fn clone_from(&mut self, source: &Self) { | ||
self.value.clone_from(&source.value); | ||
self.hash = source.hash; | ||
} | ||
} | ||
|
||
impl<T> Hash for Hashed<'_, T> | ||
where | ||
T: ToOwned + Hash + ?Sized, | ||
{ | ||
fn hash<H: Hasher>(&self, state: &mut H) { | ||
state.write_u64(self.hash); | ||
} | ||
} | ||
|
||
impl<T> PartialEq for Hashed<'_, T> | ||
where | ||
T: ToOwned + PartialEq + ?Sized, | ||
{ | ||
fn eq(&self, other: &Self) -> bool { | ||
self.value.as_ref() == other.value.as_ref() | ||
} | ||
} | ||
|
||
impl<T> Eq for Hashed<'_, T> where T: ToOwned + Eq + ?Sized {} | ||
|
||
impl<T> Deref for Hashed<'_, T> | ||
where | ||
T: ToOwned + ?Sized, | ||
{ | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.value.deref() | ||
} | ||
} | ||
|
||
/// Used to make [`Hashed`] values no-op in [`HashMap`](std::collections::HashMap) or [`HashSet`](std::collections::HashSet). | ||
/// For all other keys types (except for [`u64`]) it will panic. | ||
#[derive(Default, Clone)] | ||
pub(crate) struct HashedNoOpBuilder { | ||
hashed: u64, | ||
} | ||
|
||
impl Hasher for HashedNoOpBuilder { | ||
fn finish(&self) -> u64 { | ||
self.hashed | ||
} | ||
|
||
fn write(&mut self, _bytes: &[u8]) { | ||
panic!("Only works with `Hashed` value") | ||
} | ||
|
||
fn write_u64(&mut self, i: u64) { | ||
self.hashed = i; | ||
} | ||
} | ||
|
||
impl BuildHasher for HashedNoOpBuilder { | ||
type Hasher = HashedNoOpBuilder; | ||
|
||
fn build_hasher(&self) -> Self::Hasher { | ||
HashedNoOpBuilder::default() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters