Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge #814: fix(render): cap the failed-image cache at 64 entries (#728)

aiongg732008dd5ad4
diff --cc crates/render/src/images.rs
index b68b09497,465867e82..4a6052781
--- a/crates/render/src/images.rs
+++ b/crates/render/src/images.rs
@@@ -28,11 -31,22 +31,24 @@@ struct Cache
bytes: usize,
clock: u64,
/// Assets that failed to decode (don't retry every frame).
- bad: HashMap<(usize, u32), Arc<Vec<u8>>>,
+ /// Value: (insertion stamp, the encoded bytes keeping the address reserved).
+ bad: HashMap<(usize, u32), (u64, Arc<Vec<u8>>)>,
+ /// The colour settings the pixels were converted with ([`designcraft_color::cms::generation`]).
+ colour: u64,
}
+ impl Cache {
+ /// Remember a failed asset, evicting the oldest failures beyond [`MAX_BAD`].
+ fn mark_bad(&mut self, key: (usize, u32), bytes: &Arc<Vec<u8>>) {
+ self.clock += 1;
+ self.bad.insert(key, (self.clock, bytes.clone()));
+ while self.bad.len() > MAX_BAD {
+ let Some(oldest) = self.bad.iter().min_by_key(|(_, (stamp, _))| *stamp).map(|(k, _)| *k) else { break };
+ self.bad.remove(&oldest);
+ }
+ }
+ }
+
static CACHE: Mutex<Option<Cache>> = Mutex::new(None);
fn size_of(pm: &Pixmap) -> usize {