perf: use FxHashMap for sets of keys

This commit is contained in:
Greg Johnston 2024-09-14 21:20:09 -04:00
parent 3310e7766b
commit 550a3a4e6d

View file

@ -60,20 +60,21 @@ impl TriggerMap {
pub struct FieldKeys<K> { pub struct FieldKeys<K> {
spare_keys: Vec<StorePathSegment>, spare_keys: Vec<StorePathSegment>,
current_key: usize, current_key: usize,
keys: HashMap<K, (StorePathSegment, usize)>, keys: FxHashMap<K, (StorePathSegment, usize)>,
} }
impl<K> FieldKeys<K> impl<K> FieldKeys<K>
where where
K: Debug + Hash + PartialEq + Eq, K: Debug + Hash + PartialEq + Eq,
{ {
pub fn new(from_iter: impl IntoIterator<Item = K>) -> Self { pub fn new(from_keys: Vec<K>) -> Self {
let mut current_key = 0; let mut keys = FxHashMap::with_capacity_and_hasher(
let mut keys = HashMap::new(); from_keys.len(),
for (idx, key) in from_iter.into_iter().enumerate() { Default::default(),
let segment = current_key.into(); );
for (idx, key) in from_keys.into_iter().enumerate() {
let segment = idx.into();
keys.insert(key, (segment, idx)); keys.insert(key, (segment, idx));
current_key += 1;
} }
Self { Self {
@ -104,7 +105,7 @@ where
.into_iter() .into_iter()
.enumerate() .enumerate()
.map(|(idx, key)| (key, idx)) .map(|(idx, key)| (key, idx))
.collect::<HashMap<K, usize>>(); .collect::<FxHashMap<K, usize>>();
// remove old keys and recycle the slots // remove old keys and recycle the slots
self.keys.retain(|key, old_entry| match new_keys.get(key) { self.keys.retain(|key, old_entry| match new_keys.get(key) {