steid

@jamesgill /

steid/src/infrastructure/merge_cache.rs
Rust7.9 KBCode·Blame·History·Raw
1//! The answer `git merge-tree --write-tree` gave for one pair of commits, remembered.
2//!
3//! A pull request's Conversation tab asks whether its two branches merge on **every**
4//! view, and for the genuine three-way case that is a `merge-tree` per view: a full
5//! three-way merge in the object database, and — because `--write-tree` writes — a fresh
6//! set of blobs and trees that nothing references, every time somebody reloads the page.
7//! A conflicted request is the worst of it, since a conflict is exactly the state people
8//! sit on and refresh.
9//!
10//! Keyed by the **pair of commit ids**, which is what makes the entry safe rather than
11//! merely useful: git objects are content-addressed, so two commit ids determine their
12//! merge base, their trees and therefore their merge completely. Nothing about a
13//! repository can change the answer for a pair without changing one of the ids, so there
14//! is no invalidation problem and no staleness — the same property the highlight cache
15//! gets from keying on content.
16//!
17//! Bounded the way [`HighlightCache`](super::highlight::HighlightCache) is: an
18//! [`LruCache`] with no count limit and a byte budget enforced on insert. The budget is
19//! much smaller because the entries are: a clean answer is one object id, and a
20//! conflicted one is a list of paths.
21
22use std::sync::Mutex;
23
24use lru::LruCache;
25
26use crate::{application::port::MergePreview, domain::ObjectId};
27
28/// How much the process keeps before it starts evicting.
29///
30/// One mebibyte, which is thousands of clean answers or hundreds of conflicted ones. The
31/// working set this serves is small by nature — the open pull requests somebody is
32/// actually looking at — so a bigger budget would buy nothing a personal instance can
33/// use.
34const MAX_CACHE_BYTES: usize = 1024 * 1024;
35
36/// A rough per-entry overhead, charged so that an entry with one short path is not
37/// accounted as a handful of bytes. Not exact; the budget is a bound, not an audit.
38const PER_ENTRY_OVERHEAD: usize = 128;
39
40/// A pair of commits, in the order they were asked about.
41///
42/// Order matters: merging `main` into `spike` and `spike` into `main` are different
43/// questions with different conflict sides, so the pair is a key rather than a set.
44type Key = (ObjectId, ObjectId);
45
46/// Merge previews, remembered across requests.
47///
48/// One per process, in app context — a cache built per request would remember nothing,
49/// the same reason the `cat-file` pool and the highlight cache live there.
50///
51/// `Debug` prints the shape rather than the contents: `DiskGitQuery` derives it, and a
52/// dump of every remembered merge would be pages of object ids.
53pub struct MergeCache {
54 entries: Mutex<Entries>,
55}
56
57struct Entries {
58 cache: LruCache<Key, MergePreview>,
59 bytes: usize,
60}
61
62impl std::fmt::Debug for MergeCache {
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 let entries = self.lock();
65
66 f.debug_struct("MergeCache")
67 .field("entries", &entries.cache.len())
68 .field("bytes", &entries.bytes)
69 .finish()
70 }
71}
72
73impl Default for MergeCache {
74 fn default() -> Self {
75 Self::new()
76 }
77}
78
79impl MergeCache {
80 pub fn new() -> Self {
81 Self {
82 entries: Mutex::new(Entries {
83 // Unbounded by count; [`MAX_CACHE_BYTES`] is the real bound and is
84 // enforced on insert.
85 cache: LruCache::unbounded(),
86 bytes: 0,
87 }),
88 }
89 }
90
91 /// What this pair merged to last time, if it is still remembered.
92 pub fn get(&self, target: &ObjectId, source: &ObjectId) -> Option<MergePreview> {
93 let mut entries = self.lock();
94
95 entries
96 .cache
97 .get(&(target.clone(), source.clone()))
98 .cloned()
99 }
100
101 /// Remembers an answer.
102 pub fn put(&self, target: &ObjectId, source: &ObjectId, preview: MergePreview) {
103 let weight = weight_of(&preview);
104
105 // An entry bigger than the whole budget would evict everything and then itself.
106 // It is simply not cached — the highlight cache's rule.
107 if weight > MAX_CACHE_BYTES {
108 return;
109 }
110
111 let mut entries = self.lock();
112
113 if let Some(replaced) = entries.cache.put((target.clone(), source.clone()), preview) {
114 entries.bytes = entries.bytes.saturating_sub(weight_of(&replaced));
115 }
116 entries.bytes += weight;
117
118 while entries.bytes > MAX_CACHE_BYTES {
119 match entries.cache.pop_lru() {
120 Some((_, evicted)) => {
121 entries.bytes = entries.bytes.saturating_sub(weight_of(&evicted))
122 }
123 // Unreachable while the accounting is right, and a `break` rather than a
124 // panic if it ever is not: an over-counted cache holds less than it
125 // could, it does not fail a page.
126 None => break,
127 }
128 }
129 }
130
131 /// A poisoned lock means another thread panicked mid-insert. The cache holds nothing
132 /// anyone needs, so reading on is better than propagating a panic into a page — the
133 /// highlight cache's rule.
134 fn lock(&self) -> std::sync::MutexGuard<'_, Entries> {
135 match self.entries.lock() {
136 Ok(entries) => entries,
137 Err(poisoned) => poisoned.into_inner(),
138 }
139 }
140
141 /// How many entries and how many accounted bytes are held. Tests only.
142 #[cfg(test)]
143 fn stats(&self) -> (usize, usize) {
144 let entries = self.lock();
145 (entries.cache.len(), entries.bytes)
146 }
147}
148
149/// What an entry is charged against the budget.
150fn weight_of(preview: &MergePreview) -> usize {
151 let paths = match preview {
152 MergePreview::Clean { .. } => 0,
153 MergePreview::Conflicted { paths } => paths.iter().map(|path| path.len() + 16).sum(),
154 };
155
156 paths + PER_ENTRY_OVERHEAD
157}
158
159#[cfg(test)]
160mod tests {
161 use super::*;
162
163 fn oid(byte: char) -> ObjectId {
164 ObjectId::new(byte.to_string().repeat(40)).expect("valid id")
165 }
166
167 fn clean() -> MergePreview {
168 MergePreview::Clean { tree: oid('c') }
169 }
170
171 #[test]
172 fn an_answer_comes_back_for_the_pair_it_was_stored_under() {
173 let cache = MergeCache::new();
174 cache.put(&oid('a'), &oid('b'), clean());
175
176 assert_eq!(cache.get(&oid('a'), &oid('b')), Some(clean()));
177 }
178
179 #[test]
180 fn the_pair_is_ordered() {
181 // Merging main into spike is not the same question as the other way round, and
182 // answering one with the other's result would show the wrong conflicts.
183 let cache = MergeCache::new();
184 cache.put(&oid('a'), &oid('b'), clean());
185
186 assert_eq!(cache.get(&oid('b'), &oid('a')), None);
187 }
188
189 #[test]
190 fn an_answer_larger_than_the_whole_budget_is_not_cached() {
191 // Caching it would evict everything and then itself.
192 let cache = MergeCache::new();
193 cache.put(
194 &oid('a'),
195 &oid('b'),
196 MergePreview::Conflicted {
197 paths: vec!["x".repeat(MAX_CACHE_BYTES); 1],
198 },
199 );
200
201 assert_eq!(cache.stats(), (0, 0));
202 }
203
204 #[test]
205 fn a_repeated_pair_is_one_entry_and_one_charge() {
206 let cache = MergeCache::new();
207
208 for _ in 0..5 {
209 cache.put(&oid('a'), &oid('b'), clean());
210 }
211
212 let (entries, bytes) = cache.stats();
213 assert_eq!(entries, 1);
214 assert_eq!(bytes, weight_of(&clean()));
215 }
216
217 #[test]
218 fn the_budget_evicts_the_least_recently_used() {
219 let cache = MergeCache::new();
220 // Two-thirds of the budget each, so the second insert must push the first out.
221 let big = || MergePreview::Conflicted {
222 paths: vec!["x".repeat(MAX_CACHE_BYTES * 2 / 3); 1],
223 };
224
225 cache.put(&oid('a'), &oid('b'), big());
226 cache.put(&oid('c'), &oid('d'), big());
227
228 assert_eq!(cache.stats().0, 1);
229 assert_eq!(cache.get(&oid('a'), &oid('b')), None);
230 assert!(cache.get(&oid('c'), &oid('d')).is_some());
231 }
232}