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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::{
collections::HashMap,
sync::{
atomic::{AtomicIsize, Ordering as AtomicOrdering},
Arc,
},
};
pub trait Size {
fn size(&self) -> usize;
}
#[derive(Debug, parity_util_mem::MallocSizeOf)]
pub struct TrackedMap<K, V> {
index: Arc<RwLock<HashMap<K, V>>>,
bytes: AtomicIsize,
length: AtomicIsize,
}
impl<K, V> Default for TrackedMap<K, V> {
fn default() -> Self {
Self { index: Arc::new(HashMap::default().into()), bytes: 0.into(), length: 0.into() }
}
}
impl<K, V> TrackedMap<K, V> {
pub fn len(&self) -> usize {
std::cmp::max(self.length.load(AtomicOrdering::Relaxed), 0) as usize
}
pub fn bytes(&self) -> usize {
std::cmp::max(self.bytes.load(AtomicOrdering::Relaxed), 0) as usize
}
pub fn clone(&self) -> ReadOnlyTrackedMap<K, V> {
ReadOnlyTrackedMap(self.index.clone())
}
pub fn read(&self) -> TrackedMapReadAccess<K, V> {
TrackedMapReadAccess { inner_guard: self.index.read() }
}
pub fn write(&self) -> TrackedMapWriteAccess<K, V> {
TrackedMapWriteAccess {
inner_guard: self.index.write(),
bytes: &self.bytes,
length: &self.length,
}
}
}
pub struct ReadOnlyTrackedMap<K, V>(Arc<RwLock<HashMap<K, V>>>);
impl<K, V> ReadOnlyTrackedMap<K, V>
where
K: Eq + std::hash::Hash,
{
pub fn read(&self) -> TrackedMapReadAccess<K, V> {
TrackedMapReadAccess { inner_guard: self.0.read() }
}
}
pub struct TrackedMapReadAccess<'a, K, V> {
inner_guard: RwLockReadGuard<'a, HashMap<K, V>>,
}
impl<'a, K, V> TrackedMapReadAccess<'a, K, V>
where
K: Eq + std::hash::Hash,
{
pub fn contains_key(&self, key: &K) -> bool {
self.inner_guard.contains_key(key)
}
pub fn get(&self, key: &K) -> Option<&V> {
self.inner_guard.get(key)
}
pub fn values(&self) -> std::collections::hash_map::Values<K, V> {
self.inner_guard.values()
}
}
pub struct TrackedMapWriteAccess<'a, K, V> {
bytes: &'a AtomicIsize,
length: &'a AtomicIsize,
inner_guard: RwLockWriteGuard<'a, HashMap<K, V>>,
}
impl<'a, K, V> TrackedMapWriteAccess<'a, K, V>
where
K: Eq + std::hash::Hash,
V: Size,
{
pub fn insert(&mut self, key: K, val: V) -> Option<V> {
let new_bytes = val.size();
self.bytes.fetch_add(new_bytes as isize, AtomicOrdering::Relaxed);
self.length.fetch_add(1, AtomicOrdering::Relaxed);
self.inner_guard.insert(key, val).map(|old_val| {
self.bytes.fetch_sub(old_val.size() as isize, AtomicOrdering::Relaxed);
self.length.fetch_sub(1, AtomicOrdering::Relaxed);
old_val
})
}
pub fn remove(&mut self, key: &K) -> Option<V> {
let val = self.inner_guard.remove(key);
if let Some(size) = val.as_ref().map(Size::size) {
self.bytes.fetch_sub(size as isize, AtomicOrdering::Relaxed);
self.length.fetch_sub(1, AtomicOrdering::Relaxed);
}
val
}
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
self.inner_guard.get_mut(key)
}
}
#[cfg(test)]
mod tests {
use super::*;
impl Size for i32 {
fn size(&self) -> usize {
*self as usize / 10
}
}
#[test]
fn basic() {
let map = TrackedMap::default();
map.write().insert(5, 10);
map.write().insert(6, 20);
assert_eq!(map.bytes(), 3);
assert_eq!(map.len(), 2);
map.write().insert(6, 30);
assert_eq!(map.bytes(), 4);
assert_eq!(map.len(), 2);
map.write().remove(&6);
assert_eq!(map.bytes(), 1);
assert_eq!(map.len(), 1);
}
}