atmos/os_lib/web_engine/
flatten_memo_budget.rs1extern crate alloc;
20
21use alloc::collections::BTreeMap;
22
23pub const FLATTEN_MEMO_BUDGET_BYTES: usize = 24 * 1024 * 1024;
30
31pub const FLATTEN_MEMO_MAX_ENTRY_BYTES: usize = FLATTEN_MEMO_BUDGET_BYTES / 4;
33
34pub const FLATTEN_MEMO_MAX_ENTRIES: usize = 2048;
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
39pub struct Limits {
40 pub budget_bytes: usize,
41 pub max_entry_bytes: usize,
42 pub max_entries: usize,
43}
44
45impl Limits {
46 pub const FLATTEN_MEMO: Limits = Limits {
48 budget_bytes: FLATTEN_MEMO_BUDGET_BYTES,
49 max_entry_bytes: FLATTEN_MEMO_MAX_ENTRY_BYTES,
50 max_entries: FLATTEN_MEMO_MAX_ENTRIES,
51 };
52
53 pub fn admits(&self, bytes: usize) -> bool {
56 bytes > 0 && bytes <= self.max_entry_bytes && bytes <= self.budget_bytes && self.max_entries > 0
57 }
58}
59
60pub const SMALL_ENTRY_BYTES: usize = 1024 * 1024;
67
68pub fn worth_storing(count: usize, covered: usize, bytes: usize) -> bool {
73 count > 0 && (covered.min(count).saturating_mul(2) <= count || bytes <= SMALL_ENTRY_BYTES)
74}
75
76struct Slot<V> {
77 value: V,
78 bytes: usize,
79 group: u8,
80 used: u64,
82}
83
84pub struct BudgetLru<K: Ord + Copy, V> {
86 map: BTreeMap<K, Slot<V>>,
87 order: BTreeMap<u64, K>,
89 clock: u64,
90 total: usize,
91 limits: Limits,
92}
93
94#[derive(Clone, Copy, Debug, PartialEq, Eq)]
96pub enum InsertOutcome {
97 Stored { evicted: usize, evicted_bytes: usize },
99 Rejected,
101}
102
103impl<K: Ord + Copy, V> BudgetLru<K, V> {
104 pub const fn new(limits: Limits) -> Self {
105 Self { map: BTreeMap::new(), order: BTreeMap::new(), clock: 0, total: 0, limits }
106 }
107
108 pub fn len(&self) -> usize {
109 self.map.len()
110 }
111
112 pub fn is_empty(&self) -> bool {
113 self.map.is_empty()
114 }
115
116 pub fn total_bytes(&self) -> usize {
118 self.total
119 }
120
121 pub fn limits(&self) -> Limits {
122 self.limits
123 }
124
125 fn next_clock(&mut self) -> u64 {
126 self.clock = self.clock.wrapping_add(1);
127 self.clock
128 }
129
130 pub fn get(&mut self, key: &K) -> Option<&V> {
132 let now = self.next_clock();
133 let slot = self.map.get_mut(key)?;
134 self.order.remove(&slot.used);
135 slot.used = now;
136 self.order.insert(now, *key);
137 Some(&slot.value)
138 }
139
140 pub fn peek(&self, key: &K) -> Option<&V> {
142 self.map.get(key).map(|s| &s.value)
143 }
144
145 fn remove_slot(&mut self, key: &K) -> Option<Slot<V>> {
146 let slot = self.map.remove(key)?;
147 self.order.remove(&slot.used);
148 self.total = self.total.saturating_sub(slot.bytes);
149 Some(slot)
150 }
151
152 fn evict_oldest(&mut self) -> Option<usize> {
154 let (_, key) = self.order.pop_first()?;
155 let slot = self.map.remove(&key)?;
156 self.total = self.total.saturating_sub(slot.bytes);
157 Some(slot.bytes)
158 }
159
160 pub fn insert(&mut self, key: K, value: V, bytes: usize, group: u8) -> InsertOutcome {
162 if !self.limits.admits(bytes) {
163 return InsertOutcome::Rejected;
164 }
165 self.remove_slot(&key);
166 let mut evicted = 0;
167 let mut evicted_bytes = 0;
168 while self.total.saturating_add(bytes) > self.limits.budget_bytes || self.map.len() >= self.limits.max_entries {
169 match self.evict_oldest() {
170 Some(b) => {
171 evicted += 1;
172 evicted_bytes += b;
173 }
174 None => break,
175 }
176 }
177 let now = self.next_clock();
178 self.order.insert(now, key);
179 self.map.insert(key, Slot { value, bytes, group, used: now });
180 self.total = self.total.saturating_add(bytes);
181 InsertOutcome::Stored { evicted, evicted_bytes }
182 }
183
184 pub fn retain(&mut self, mut keep: impl FnMut(u8, &V) -> bool) -> (usize, usize) {
186 let mut dropped = 0;
187 let mut dropped_bytes = 0;
188 let order = &mut self.order;
189 self.map.retain(|_, s| {
190 let k = keep(s.group, &s.value);
191 if !k {
192 order.remove(&s.used);
193 dropped += 1;
194 dropped_bytes += s.bytes;
195 }
196 k
197 });
198 self.total = self.total.saturating_sub(dropped_bytes);
199 (dropped, dropped_bytes)
200 }
201
202 pub fn clear(&mut self) {
204 self.map.clear();
205 self.order.clear();
206 self.total = 0;
207 }
208}