feat(optimizer): use a faster hashmap

This commit is contained in:
Mayeul@Zama
2022-12-01 17:58:31 +01:00
committed by mayeul-zama
parent d6e69f878c
commit a0fcae1c9b
4 changed files with 40 additions and 6 deletions

View File

@@ -11,6 +11,8 @@ file-lock = "2.1.6"
serde = { version = "1.0", features = ["derive"] }
bincode = "1.3"
puruspe = "0.2.0"
rustc-hash = "1.1"
rand = "0.8"
[dev-dependencies]
approx = "0.5"

View File

@@ -1,12 +1,10 @@
use std::cmp::Eq;
use std::collections::hash_map::RandomState;
use crate::utils::hasher_builder::FxRandomState;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::collections::HashMap;
use std::hash::Hash;
use serde::de::DeserializeOwned;
use serde::Serialize;
pub type Map<K, V> = HashMap<K, V, RandomState>;
pub type Map<K, V> = HashMap<K, V, FxRandomState>;
#[allow(clippy::len_without_is_empty)]
pub trait ReadOnlyCache: Clone + Serialize + DeserializeOwned + Default {

View File

@@ -0,0 +1,33 @@
use rand::Rng;
use rustc_hash::FxHasher;
use std::cell::Cell;
use std::hash::{BuildHasher, Hasher};
// Randomized hasher builder to avoid the stable hashmap trap
// see https://morestina.net/blog/1843/the-stable-hashmap-trap
#[derive(Copy, Clone, Debug)]
pub struct FxRandomState(usize);
impl BuildHasher for FxRandomState {
type Hasher = FxHasher;
fn build_hasher(&self) -> FxHasher {
let mut hasher = FxHasher::default();
hasher.write_usize(self.0);
hasher
}
}
impl Default for FxRandomState {
fn default() -> Self {
thread_local! {
static SEED: Cell<usize> = Cell::new(rand::thread_rng().gen())
}
let seed = SEED.with(|seed| {
let n = seed.get();
seed.set(n.wrapping_add(1));
n
});
Self(seed)
}
}

View File

@@ -1,4 +1,5 @@
pub mod cache;
pub mod hasher_builder;
pub fn square<V>(v: V) -> V
where