perf: change BTreeMap to StraightHashMap

This commit is contained in:
quake
2022-09-05 13:13:06 +09:00
parent aaa5254c29
commit 239438754c
7 changed files with 46 additions and 45 deletions

View File

@@ -4,9 +4,13 @@
use std::prelude::v1::*;
use std::{
any::TypeId,
collections::{HashMap, HashSet},
hash::{BuildHasher, Hash, Hasher},
};
pub type StraightHashMap<K, V> = HashMap<K, V, StraightHasherBuilder>;
pub type StraightHashSet<K> = HashSet<K, StraightHasherBuilder>;
/// Dummy hash value to map zeros to. This value can be anything.
///
/// # Notes
@@ -30,7 +34,7 @@ pub const ALT_ZERO_HASH: u64 = 42;
///
/// Panics when hashing any data type other than a [`u64`].
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
struct StraightHasher(u64);
pub struct StraightHasher(u64);
impl Hasher for StraightHasher {
#[inline(always)]
@@ -38,23 +42,18 @@ impl Hasher for StraightHasher {
self.0
}
#[inline]
fn write(&mut self, bytes: &[u8]) {
assert_eq!(bytes.len(), 8, "StraightHasher can only hash u64 values");
fn write(&mut self, _bytes: &[u8]) {
panic!("StraightHasher can only hash u64 values");
}
let mut key = [0_u8; 8];
key.copy_from_slice(bytes);
self.0 = u64::from_ne_bytes(key);
if self.0 == 0 {
self.0 = ALT_ZERO_HASH;
}
fn write_u64(&mut self, i: u64) {
self.0 = i;
}
}
/// A hash builder for `StraightHasher`.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
struct StraightHasherBuilder;
pub struct StraightHasherBuilder;
impl BuildHasher for StraightHasherBuilder {
type Hasher = StraightHasher;