use std::{ hash::Hash, sync::{Arc, Mutex}, }; use dashmap::mapref::entry::Entry; use crate::FxDashMap; pub struct OnceConcurrentlyMap { inner: FxDashMap<&'static K, Arc>>>, } impl Default for OnceConcurrentlyMap { fn default() -> Self { Self::new() } } impl OnceConcurrentlyMap { pub fn new() -> Self { Self { inner: FxDashMap::default(), } } pub fn action(&self, key: &K, func: impl FnOnce() -> V) -> V { let temp = TemporarilyInserted { inner: &self.inner, key, }; let mutex = match temp.entry() { Entry::Occupied(e) => e.get().clone(), Entry::Vacant(e) => e.insert(Arc::new(Mutex::new(None))).clone(), }; let mut guard = mutex.lock().unwrap(); if let Some(value) = &*guard { // Yeah, somebody else already did it for us return value.clone(); } // We are the one responsible for computing let value = func(); *guard = Some(value.clone()); drop(guard); drop(temp); value } } struct TemporarilyInserted<'a, K: 'static + Hash + Eq + Ord + Send + Sync, V: Send + Sync> { inner: &'a FxDashMap<&'static K, V>, key: &'a K, } impl<'a, K: Hash + Eq + Ord + Send + Sync, V: Send + Sync> TemporarilyInserted<'a, K, V> { fn entry(&self) -> Entry<'a, &'static K, V> { // SAFETY: We remove the value again after this function is done let static_key: &'static K = unsafe { std::mem::transmute(self.key) }; self.inner.entry(static_key) } } impl Drop for TemporarilyInserted<'_, K, V> { fn drop(&mut self) { let static_key: &'static K = unsafe { std::mem::transmute(self.key) }; self.inner.remove(&static_key); } } pub struct SafeOnceConcurrentlyMap< K: Clone + Hash + Eq + Ord + Send + Sync + 'static, V: Clone + Send + Sync, > { inner: FxDashMap>>>, } impl Default for SafeOnceConcurrentlyMap { fn default() -> Self { Self::new() } } impl SafeOnceConcurrentlyMap { pub fn new() -> Self { Self { inner: FxDashMap::default(), } } pub fn action(&self, key: &K, func: impl FnOnce() -> V) -> V { let temp = SafeTemporarilyInserted { inner: &self.inner, key, }; let mutex = match temp.entry() { Entry::Occupied(e) => e.get().clone(), Entry::Vacant(e) => e.insert(Arc::new(Mutex::new(None))).clone(), }; let mut guard = mutex.lock().unwrap(); if let Some(value) = &*guard { // Yeah, somebody else already did it for us return value.clone(); } // We are the one responsible for computing let value = func(); *guard = Some(value.clone()); drop(guard); drop(temp); value } } struct SafeTemporarilyInserted< 'a, K: 'static + Clone + Hash + Eq + Ord + Send + Sync, V: Send + Sync, > { inner: &'a FxDashMap, key: &'a K, } impl SafeTemporarilyInserted<'_, K, V> { fn entry(&self) -> Entry<'_, K, V> { // SAFETY: We remove the value again after this function is done self.inner.entry(self.key.clone()) } } impl Drop for SafeTemporarilyInserted<'_, K, V> { fn drop(&mut self) { self.inner.remove(self.key); } }