Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

perf(compose): the shape stroke split cache keeps to a byte budget and empties on Purge (#4530) (#4774)

FinnNk752f6112647f
diff --git a/crates/compose/src/shape_split.rs b/crates/compose/src/shape_split.rs
index 3c1a4b106..9f0143f88 100644
--- a/crates/compose/src/shape_split.rs
+++ b/crates/compose/src/shape_split.rs
@@ -21,6 +21,27 @@ fn cache() -> &'static Mutex<(HashMap<u64, Entry>, u64)> {
}
const CAPACITY: usize = 32;
+/// Most pixel bytes the split parts may hold (#4530): each entry is two canvas-sized surfaces,
+/// so 32 of them at 24 MP could hold several GB outside every memory budget.
+#[cfg(not(test))]
+const BUDGET: usize = 256 << 20;
+/// Small under test, so a few 512² shapes exercise the eviction.
+#[cfg(test)]
+const BUDGET: usize = 8 << 20;
+
+/// Pixel bytes of one entry's (fill, stroke) parts (the pinned shape pixels are the document's).
+fn entry_bytes(parts: &(Surface, Surface)) -> usize {
+ [&parts.0, &parts.1].iter().map(|s| s.tiles().map(|(_, t)| t.bytes().len()).sum::<usize>()).sum()
+}
+
+/// Drop every cached split (Edit › Purge › All), returning the parts' pixel bytes. Callers may
+/// still hold returned surfaces, so this is not a measure of freed process memory.
+pub fn purge_cache() -> usize {
+ let mut c = cache().lock().unwrap_or_else(|e| e.into_inner());
+ let bytes = c.0.values().map(|e| entry_bytes(&e.0)).sum();
+ c.0.clear();
+ bytes
+}
/// (fill only, stroke only) of a stroked shape, rendered over `canvas` in RGBA at `depth`.
/// `None` without a stroke. Where the shape's pixels
@@ -56,11 +77,20 @@ pub fn split(sh: &ShapeLayer, canvas: Rect, depth: SampleType) -> Option<(Surfac
fit_stroke(&parts.0, &mut parts.1, cache, canvas);
}
let mut c = cache().lock().unwrap_or_else(|e| e.into_inner());
- if c.0.len() >= CAPACITY
- && let Some(old) = c.0.iter().min_by_key(|e| (e.1).1).map(|e| *e.0)
- {
+ // Least recently used first, until the new entry fits both the count and the byte budget.
+ let new_bytes = entry_bytes(&parts);
+ loop {
+ let held: usize = c.0.values().map(|e| entry_bytes(&e.0)).sum();
+ if c.0.len() < CAPACITY && held.saturating_add(new_bytes) <= BUDGET {
+ break;
+ }
+ let Some(old) = c.0.iter().min_by_key(|e| (e.1).1).map(|e| *e.0) else { break };
c.0.remove(&old);
}
+ if new_bytes > BUDGET {
+ // Larger than the whole budget on its own: use it, don't keep it.
+ return Some(parts);
+ }
let tick = c.1;
c.0.insert(key, (parts.clone(), tick, sh.cache.clone()));
Some(parts)
@@ -117,3 +147,40 @@ fn fit_stroke(fill: &Surface, stroke: &mut Surface, cache: &Surface, canvas: Rec
}
stroke.write_region(r, &out);
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use photocraft_color::Color;
+ use photocraft_doc::Fill;
+ use photocraft_doc::vector::{Path, ShapeStroke, Subpath};
+
+ fn shape(x: f64) -> ShapeLayer {
+ ShapeLayer {
+ path: Path::new(vec![Subpath::polygon(&[(x, 10.0), (x + 400.0, 10.0), (x + 400.0, 500.0), (x, 500.0)])]),
+ fill: Some(Fill::Solid(Color::rgb(0.2, 0.4, 0.6))),
+ stroke: Some(ShapeStroke { width: 6.0, ..ShapeStroke::default() }),
+ live: None,
+ cache: None,
+ psd_raw: None,
+ }
+ }
+
+ fn held() -> usize {
+ cache().lock().unwrap_or_else(|e| e.into_inner()).0.values().map(|e| entry_bytes(&e.0)).sum()
+ }
+
+ /// The split cache stays within its byte budget, evicting the least recently used, and
+ /// Edit › Purge › All empties it (#4530).
+ #[test]
+ fn split_cache_keeps_to_its_budget_and_purges() {
+ let canvas = Rect::new(0, 0, 1024, 512);
+ for i in 0..6 {
+ let parts = split(&shape(f64::from(i) * 10.0), canvas, SampleType::U8).expect("stroked");
+ assert!(entry_bytes(&parts) > 0);
+ assert!(held() <= BUDGET, "shape {i}: {} bytes held", held());
+ }
+ // Other tests share the cache, so only what this test can rely on: a purge frees bytes.
+ assert!(purge_cache() > 0, "something to purge");
+ }
+}
diff --git a/crates/engine/src/edit_menu_cmds.rs b/crates/engine/src/edit_menu_cmds.rs
index f798b6e4a..344141204 100644
--- a/crates/engine/src/edit_menu_cmds.rs
+++ b/crates/engine/src/edit_menu_cmds.rs
@@ -274,6 +274,11 @@ fn purge(s: &mut Session, what: &str) -> Result<Value> {
freed = freed.saturating_add(stages);
items.push("smart filter cache");
}
+ let strokes = photocraft_compose::shape_split::purge_cache();
+ if strokes > 0 {
+ freed = freed.saturating_add(strokes);
+ items.push("shape stroke cache");
+ }
}
s.edit_state.fade = None;
let msg = if items.is_empty() { "nothing to purge".to_string() } else { format!("purged {}", items.join(", ")) };