mirror of
https://github.com/zama-ai/concrete.git
synced 2026-02-09 12:15:09 -05:00
feat(optimizer): use a faster hashmap
This commit is contained in:
@@ -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"
|
||||
|
||||
10
concrete-optimizer/src/utils/cache/read_only.rs
vendored
10
concrete-optimizer/src/utils/cache/read_only.rs
vendored
@@ -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 {
|
||||
|
||||
33
concrete-optimizer/src/utils/hasher_builder.rs
Normal file
33
concrete-optimizer/src/utils/hasher_builder.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod cache;
|
||||
pub mod hasher_builder;
|
||||
|
||||
pub fn square<V>(v: V) -> V
|
||||
where
|
||||
|
||||
Reference in New Issue
Block a user