File size: 3,985 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use std::{
    hash::Hash,
    sync::{Arc, Mutex},
};

use dashmap::mapref::entry::Entry;

use crate::FxDashMap;

pub struct OnceConcurrentlyMap<K: Hash + Eq + Ord + Send + Sync + 'static, V: Clone + Send + Sync> {
    inner: FxDashMap<&'static K, Arc<Mutex<Option<V>>>>,
}

impl<K: Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> Default
    for OnceConcurrentlyMap<K, V>
{
    fn default() -> Self {
        Self::new()
    }
}

impl<K: Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> OnceConcurrentlyMap<K, V> {
    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<K: Hash + Eq + Ord + Send + Sync, V: Send + Sync> 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<K, Arc<Mutex<Option<V>>>>,
}

impl<K: Clone + Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> Default
    for SafeOnceConcurrentlyMap<K, V>
{
    fn default() -> Self {
        Self::new()
    }
}

impl<K: Clone + Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync>
    SafeOnceConcurrentlyMap<K, V>
{
    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<K, V>,
    key: &'a K,
}

impl<K: Clone + Hash + Eq + Ord + Send + Sync, V: Send + Sync> 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<K: Clone + Hash + Eq + Ord + Send + Sync, V: Send + Sync> Drop
    for SafeTemporarilyInserted<'_, K, V>
{
    fn drop(&mut self) {
        self.inner.remove(self.key);
    }
}