Use const fn to make resizes a bit faster

This commit is contained in:
Gwilym Inzani 2023-05-10 18:08:39 +01:00
parent 65dd1341e5
commit 943fd1154f
3 changed files with 9 additions and 11 deletions

View file

@ -907,7 +907,7 @@ const fn number_before_resize(capacity: usize) -> usize {
capacity * 60 / 100 capacity * 60 / 100
} }
#[derive(Default, Clone, Copy, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) struct HashType(u32); pub(crate) struct HashType(u32);
impl From<usize> for HashType { impl From<usize> for HashType {
@ -919,13 +919,17 @@ impl From<usize> for HashType {
} }
impl HashType { impl HashType {
pub(crate) const fn new() -> Self {
Self(0)
}
// 32 bit mix function from here: https://gist.github.com/badboy/6267743 // 32 bit mix function from here: https://gist.github.com/badboy/6267743
fn bit_mix(key: u32) -> Self { fn bit_mix(key: u32) -> Self {
use core::num::Wrapping; use core::num::Wrapping;
let key = Wrapping(key); let key = Wrapping(key);
let key = (key + Wrapping(0x7ed55d16u32)) + (key << 12); let key = (key + Wrapping(0x7ed55d16)) + (key << 12);
let key = (key ^ Wrapping(0xc761c23c)) ^ (key >> 19); let key = (key ^ Wrapping(0xc761c23c)) ^ (key >> 19);
let key = (key + Wrapping(0x165667b1)) + (key << 5); let key = (key + Wrapping(0x165667b1)) + (key << 5);
let key = (key + Wrapping(0xd3a2646c)) ^ (key << 9); let key = (key + Wrapping(0xd3a2646c)) ^ (key << 9);

View file

@ -16,9 +16,9 @@ pub(crate) struct Node<K, V> {
} }
impl<K, V> Node<K, V> { impl<K, V> Node<K, V> {
fn new() -> Self { pub(crate) const fn new() -> Self {
Self { Self {
hash: HashType::default(), hash: HashType::new(),
distance_to_initial_bucket: -1, distance_to_initial_bucket: -1,
key: MaybeUninit::uninit(), key: MaybeUninit::uninit(),
value: MaybeUninit::uninit(), value: MaybeUninit::uninit(),
@ -144,12 +144,6 @@ impl<K, V> Drop for Node<K, V> {
} }
} }
impl<K, V> Default for Node<K, V> {
fn default() -> Self {
Self::new()
}
}
impl<K, V> Clone for Node<K, V> impl<K, V> Clone for Node<K, V>
where where
K: Clone, K: Clone,

View file

@ -19,7 +19,7 @@ impl<K, V, ALLOCATOR: ClonableAllocator> NodeStorage<K, V, ALLOCATOR> {
let mut nodes = Vec::with_capacity_in(capacity, alloc); let mut nodes = Vec::with_capacity_in(capacity, alloc);
for _ in 0..capacity { for _ in 0..capacity {
nodes.push(Node::default()); nodes.push(Node::new());
} }
Self { Self {