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
use lru::LruCache;
use std::hash::Hash;
use std::num::NonZeroUsize;
const INITIAL_CAPACITY: Option<NonZeroUsize> = NonZeroUsize::new(4);
pub trait ResidentSize {
fn resident_size(&self) -> usize;
}
pub struct MemoryLruCache<K, V> {
inner: LruCache<K, V>,
cur_size: usize,
max_size: usize,
}
impl<K: Eq + Hash, V: ResidentSize> MemoryLruCache<K, V> {
pub fn new(max_size: usize) -> Self {
MemoryLruCache {
inner: LruCache::new(INITIAL_CAPACITY.expect("4 != 0; qed")),
max_size: max_size,
cur_size: 0,
}
}
pub fn insert(&mut self, key: K, val: V) {
let cap = self.inner.cap().get();
if self.inner.len() == cap {
let next_cap = NonZeroUsize::new(cap.saturating_mul(2)).expect(
"only returns None if value is zero; cap is guaranteed to be non-zero; qed",
);
self.inner.resize(next_cap);
}
self.cur_size += val.resident_size();
if let Some(lru) = self.inner.put(key, val) {
self.cur_size -= lru.resident_size();
}
self.readjust_down();
}
pub fn get(&mut self, key: &K) -> Option<&V> {
self.inner.get(key)
}
pub fn with_mut<U>(&mut self, key: &K, with: impl FnOnce(Option<&mut V>) -> U) -> U {
let mut val = self.inner.get_mut(key);
let prev_size = val.as_ref().map_or(0, |v| v.resident_size());
let res = with(val.as_mut().map(|v: &mut &mut V| &mut **v));
let new_size = val.as_ref().map_or(0, |v| v.resident_size());
self.cur_size -= prev_size;
self.cur_size += new_size;
self.readjust_down();
res
}
pub fn current_size(&self) -> usize {
self.cur_size
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn contains(&self, key: &K) -> bool {
self.inner.contains(key)
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn peek(&self, key: &K) -> Option<&V> {
self.inner.peek(key)
}
fn readjust_down(&mut self) {
while self.cur_size > self.max_size {
match self.inner.pop_lru() {
Some((_, v)) => self.cur_size -= v.resident_size(),
_ => break,
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
impl ResidentSize for Vec<u8> {
fn resident_size(&self) -> usize {
self.len()
}
}
#[test]
fn it_works() {
let mut cache = MemoryLruCache::new(256);
let val1 = vec![0u8; 100];
let size1 = val1.resident_size();
assert_eq!(cache.len(), 0);
cache.insert("hello", val1);
assert_eq!(cache.current_size(), size1);
let val2 = vec![0u8; 210];
let size2 = val2.resident_size();
cache.insert("world", val2);
assert!(cache.get(&"hello").is_none());
assert!(cache.get(&"world").is_some());
assert_eq!(cache.current_size(), size2);
assert_eq!(cache.len(), 1);
}
#[test]
fn it_works_if_cur_size_equals_max_size() {
let mut cache = MemoryLruCache::new(8);
cache.insert(1, vec![0u8, 1u8]);
cache.insert(2, vec![2u8, 3u8]);
cache.insert(3, vec![4u8, 5u8]);
cache.insert(4, vec![6u8, 7u8]);
cache.insert(5, vec![8u8, 9u8]);
assert_eq!(Some(&vec![2u8, 3u8]), cache.get(&2));
}
}