mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-23 16:21:33 +11:00
Massively improve high bit benchmarks with some bit mixing
This commit is contained in:
parent
82855ac66b
commit
626e4da8e4
|
@ -477,7 +477,12 @@ where
|
||||||
{
|
{
|
||||||
let mut hasher = self.hasher.build_hasher();
|
let mut hasher = self.hasher.build_hasher();
|
||||||
key.hash(&mut hasher);
|
key.hash(&mut hasher);
|
||||||
hasher.finish().into()
|
let result = hasher.finish();
|
||||||
|
|
||||||
|
// we want to allow truncation here since we're reducing 64 bits to 32
|
||||||
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
|
let reduced = (result as u32) ^ ((result >> 32) as u32);
|
||||||
|
HashType::bit_mix(reduced)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -899,14 +904,6 @@ const fn number_before_resize(capacity: usize) -> usize {
|
||||||
#[derive(Default, Clone, Copy, PartialEq, Eq)]
|
#[derive(Default, Clone, Copy, PartialEq, Eq)]
|
||||||
pub(crate) struct HashType(u32);
|
pub(crate) struct HashType(u32);
|
||||||
|
|
||||||
impl From<u64> for HashType {
|
|
||||||
fn from(value: u64) -> Self {
|
|
||||||
// we explicitly want to allow truncation
|
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
|
||||||
Self(value as u32)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<usize> for HashType {
|
impl From<usize> for HashType {
|
||||||
fn from(value: usize) -> Self {
|
fn from(value: usize) -> Self {
|
||||||
// we explicitly want to allow truncation
|
// we explicitly want to allow truncation
|
||||||
|
@ -916,6 +913,16 @@ impl From<usize> for HashType {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HashType {
|
impl HashType {
|
||||||
|
// 32 bit mix function from here: https://gist.github.com/badboy/6267743
|
||||||
|
fn bit_mix(key: u32) -> Self {
|
||||||
|
let key = !key.wrapping_add(key << 15);
|
||||||
|
let key = key ^ key.rotate_right(12);
|
||||||
|
let key = key.wrapping_add(key << 2);
|
||||||
|
let key = key ^ key.rotate_right(4);
|
||||||
|
let key = key.wrapping_add(key << 3).wrapping_add(key << 11);
|
||||||
|
Self(key ^ key.rotate_right(16))
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn fast_mod(self, len: usize) -> usize {
|
pub(crate) fn fast_mod(self, len: usize) -> usize {
|
||||||
debug_assert!(len.is_power_of_two(), "Length must be a power of 2");
|
debug_assert!(len.is_power_of_two(), "Length must be a power of 2");
|
||||||
(self.0 as usize) & (len - 1)
|
(self.0 as usize) & (len - 1)
|
||||||
|
|
Loading…
Reference in a new issue