diff --git a/crates/compose/src/shape_split.rs b/crates/compose/src/shape_split.rs index 5a44e739bb..ac7f9f8d11 100644 --- a/crates/compose/src/shape_split.rs +++ b/crates/compose/src/shape_split.rs @@ -15,9 +15,10 @@ use photocraft_raster::Surface; /// (fill, stroke), last use, and the shape's pixels the stroke was fitted to (kept alive so /// their tile addresses, part of the key, can't be reused). type Entry = ((Surface, Surface), u64, Option); +type Cache = (HashMap, u64); -fn cache() -> &'static Mutex<(HashMap, u64)> { - static C: OnceLock, u64)>> = OnceLock::new(); +fn cache() -> &'static Mutex { + static C: OnceLock> = OnceLock::new(); C.get_or_init(|| Mutex::new((HashMap::new(), 0))) } @@ -25,25 +26,59 @@ pub(crate) fn purge() { cache().lock().unwrap_or_else(|e| e.into_inner()).0.clear(); } -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. +/// Pixel-byte budget for split parts and their pinned source surfaces (#4530). #[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.len()).sum::()).sum() +/// Pixel bytes retained by the parts and their source; overflow makes the entry oversized. +fn entry_bytes(parts: &(Surface, Surface), source: Option<&Surface>) -> usize { + [&parts.0, &parts.1].into_iter().chain(source).flat_map(|s| s.tiles().map(|(_, t)| t.len())).fold(0, usize::saturating_add) } -/// Drop every cached split (Edit › Purge › All), returning the parts' pixel bytes. Callers may +/// Look up a split, refreshing recency on a hit. +fn get(c: &Mutex, key: u64) -> Option<(Surface, Surface)> { + let mut c = c.lock().unwrap_or_else(|e| e.into_inner()); + c.1 += 1; + let tick = c.1; + let entry = c.0.get_mut(&key)?; + entry.1 = tick; + Some(entry.0.clone()) +} + +/// Cache a split only when its retained pixel bytes fit the budget and are nonzero. +fn insert(c: &Mutex, key: u64, parts: &(Surface, Surface), source: Option<&Surface>) { + let new_bytes = entry_bytes(parts, source); + // Don't cache entries that hold no pixels or exceed the byte budget. + if new_bytes == 0 || new_bytes > BUDGET { + return; + } + let mut c = c.lock().unwrap_or_else(|e| e.into_inner()); + // Another caller may have filled this key while we rendered. Charge its replacement once. + c.0.remove(&key); + // Count the retained pixels once, then subtract each eviction's charge. + let mut held: usize = c.0.values().map(|e| entry_bytes(&e.0, e.2.as_ref())).sum(); + // Least recently used first, until the new entry fits the byte budget. + loop { + if 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 }; + let Some(removed) = c.0.remove(&old) else { break }; + held = held.saturating_sub(entry_bytes(&removed.0, removed.2.as_ref())); + } + c.1 += 1; + let tick = c.1; + c.0.insert(key, (parts.clone(), tick, source.cloned())); +} + +/// Drop every cached split (Edit › Purge › All), returning its retained 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(); + let bytes = c.0.values().map(|e| entry_bytes(&e.0, e.2.as_ref())).sum(); c.0.clear(); bytes } @@ -67,14 +102,8 @@ pub fn split(sh: &ShapeLayer, canvas: Rect, depth: SampleType) -> Option<(Surfac } } let key = h.finish(); - { - let mut c = cache().lock().unwrap_or_else(|e| e.into_inner()); - c.1 += 1; - let tick = c.1; - if let Some(e) = c.0.get_mut(&key) { - e.1 = tick; - return Some(e.0.clone()); - } + if let Some(parts) = get(cache(), key) { + return Some(parts); } let fmt = PixelFormat::RGBA8.with_sample(depth); let fill_only = ShapeLayer { stroke: None, cache: None, ..sh.clone() }; @@ -86,23 +115,7 @@ pub fn split(sh: &ShapeLayer, canvas: Rect, depth: SampleType) -> Option<(Surfac if photocraft_raster::spill::check_since(generation).is_err() || sh.cache.as_ref().is_some_and(|c| c.scratch_health().is_err()) { return Some(parts); } - let mut c = cache().lock().unwrap_or_else(|e| e.into_inner()); - // 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())); + insert(cache(), key, &parts, sh.cache.as_ref()); Some(parts) } @@ -227,7 +240,116 @@ mod tests { } fn held() -> usize { - cache().lock().unwrap_or_else(|e| e.into_inner()).0.values().map(|e| entry_bytes(&e.0)).sum() + cache().lock().unwrap_or_else(|e| e.into_inner()).0.values().map(|e| entry_bytes(&e.0, e.2.as_ref())).sum() + } + + fn tile(sample: SampleType, value: f32) -> Surface { + let mut surface = Surface::new(PixelFormat::RGBA8.with_sample(sample)); + surface.fill_rect(Rect::new(0, 0, 1, 1), &[value, value, value, 1.0]); + surface + } + + /// A hit protects that entry while one or several older entries make room for the new split. + #[test] + fn split_cache_evicts_the_exact_least_recently_used_entry() { + for sample in [SampleType::F32, SampleType::U8] { + let c = Mutex::new(Cache::default()); + let parts = (tile(sample, 0.25), tile(sample, 0.5)); + let source = tile(SampleType::U8, 0.75); + let bytes = entry_bytes(&parts, Some(&source)); + let slots = (BUDGET / bytes) as u64; + let incoming = (tile(SampleType::F32, 0.5), tile(SampleType::F32, 0.75)); + let incoming_bytes = entry_bytes(&incoming, Some(&source)); + let victims = (incoming_bytes / bytes) as u64; + assert_eq!(incoming_bytes % bytes, 0); + assert!(slots > victims); + for key in 0..slots { + assert!(get(&c, key).is_none()); + insert(&c, key, &parts, Some(&source)); + } + assert!(get(&c, 0).is_some()); + assert!(get(&c, slots).is_none()); + insert(&c, slots, &incoming, Some(&source)); + let c = c.lock().unwrap(); + assert_eq!(c.0.len(), (slots - victims + 1) as usize); + for key in 0..=slots { + assert_eq!(c.0.contains_key(&key), key == 0 || key > victims, "{sample:?}: entry {key}"); + } + assert_eq!(c.0.values().map(|e| entry_bytes(&e.0, e.2.as_ref())).sum::(), bytes * (slots - victims) as usize + incoming_bytes); + } + } + + /// Replacement accounts only the new parts/source and releases the previous tile owners. + #[test] + fn split_cache_replacement_updates_bytes_and_releases_old_surfaces() { + let c = Mutex::new(Cache::default()); + let old_parts = (tile(SampleType::U8, 0.25), tile(SampleType::U8, 0.5)); + let source = tile(SampleType::U8, 0.75); + let old_part = std::sync::Arc::downgrade(old_parts.0.tiles().next().unwrap().1); + let old_source = std::sync::Arc::downgrade(source.tiles().next().unwrap().1); + assert!(get(&c, 7).is_none()); + insert(&c, 7, &old_parts, Some(&source)); + drop(old_parts); + drop(source); + assert!(old_part.upgrade().is_some() && old_source.upgrade().is_some()); + let new_parts = (tile(SampleType::F32, 0.5), tile(SampleType::F32, 0.75)); + let source = tile(SampleType::U16, 0.25); + let expected = entry_bytes(&new_parts, Some(&source)); + insert(&c, 7, &new_parts, Some(&source)); + assert!(old_part.upgrade().is_none() && old_source.upgrade().is_none()); + assert_eq!(get(&c, 7).unwrap().0.pixel(0, 0), new_parts.0.pixel(0, 0)); + let c = c.lock().unwrap(); + assert_eq!(c.0.len(), 1); + assert_eq!(c.0.values().map(|e| entry_bytes(&e.0, e.2.as_ref())).sum::(), expected); + } + + /// Two callers may miss before either renders; the second completion replaces the first. + #[test] + fn split_cache_duplicate_misses_do_not_evict_unrelated_entries() { + let c = Mutex::new(Cache::default()); + let parts = (tile(SampleType::F32, 0.25), tile(SampleType::F32, 0.5)); + let bytes = entry_bytes(&parts, None); + let slots = (BUDGET / bytes) as u64; + assert!(slots >= 2); + for key in 0..slots - 1 { + assert!(get(&c, key).is_none()); + insert(&c, key, &parts, None); + } + // These are the lookup/insert lock acquisitions of two overlapping cold splits. + assert!(get(&c, slots - 1).is_none()); + assert!(get(&c, slots - 1).is_none()); + insert(&c, slots - 1, &parts, None); + insert(&c, slots - 1, &parts, None); + let c = c.lock().unwrap(); + assert_eq!(c.0.len(), slots as usize, "replacing the first result must not evict a neighbor"); + for key in 0..slots { + assert!(c.0.contains_key(&key), "entry {key} must survive"); + } + assert_eq!(c.0.values().map(|e| entry_bytes(&e.0, e.2.as_ref())).sum::(), bytes * slots as usize); + } + + /// Two cold splits can both miss before either completes; completion order sets recency. + #[test] + fn split_cache_orders_overlapping_misses_by_completion() { + let c = Mutex::new(Cache::default()); + let parts = (tile(SampleType::F32, 0.25), tile(SampleType::F32, 0.5)); + let slots = (BUDGET / entry_bytes(&parts, None)) as u64; + assert!(slots >= 2); + assert!(get(&c, 0).is_none()); + assert!(get(&c, 1).is_none()); + insert(&c, 0, &parts, None); + insert(&c, 1, &parts, None); + { + let c = c.lock().unwrap(); + assert!(c.0[&0].1 < c.0[&1].1, "the later completion must get a newer tick"); + } + for key in 2..=slots { + insert(&c, key, &parts, None); + } + let c = c.lock().unwrap(); + assert_eq!(c.0.len(), slots as usize); + assert!(!c.0.contains_key(&0), "evict the earlier completion"); + assert!(c.0.contains_key(&1), "keep the later completion"); } /// The split cache stays within its byte budget, evicting the least recently used, and @@ -237,7 +359,7 @@ mod tests { 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!(entry_bytes(&parts, None) > 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. diff --git a/crates/compose/tests/shape_split_cache.rs b/crates/compose/tests/shape_split_cache.rs new file mode 100644 index 0000000000..3bd163ee5c --- /dev/null +++ b/crates/compose/tests/shape_split_cache.rs @@ -0,0 +1,39 @@ +use std::sync::Arc; + +use photocraft_color::{Color, SampleType}; +use photocraft_compose::shape_split; +use photocraft_doc::Fill; +use photocraft_doc::vector::{Path, ShapeLayer, ShapeStroke, Subpath}; +use photocraft_geom::Rect; + +/// A small working set beyond 32 shapes should reuse its tiles at every sample depth. +#[test] +fn small_shape_working_sets_keep_their_cached_tiles() { + let canvas = Rect::new(0, 0, 400, 240); + let shapes: Vec<_> = (0..40) + .map(|i| { + let (x, y) = (12.0 + f64::from(i % 8) * 40.0, 12.0 + f64::from(i / 8) * 40.0); + ShapeLayer { + path: Path::new(vec![Subpath::polygon(&[(x, y), (x + 20.0, y), (x + 20.0, y + 20.0), (x, y + 20.0)])]), + fill: Some(Fill::Solid(Color::rgb(0.2, 0.4, 0.6))), + stroke: Some(ShapeStroke { width: 2.0, ..ShapeStroke::default() }), + ..ShapeLayer::default() + } + }) + .collect(); + for depth in SampleType::ALL { + shape_split::purge_cache(); + let first: Vec<_> = shapes.iter().map(|shape| shape_split::split(shape, canvas, depth).expect("stroked shape")).collect(); + for (i, (shape, before)) in shapes.iter().zip(&first).enumerate() { + let after = shape_split::split(shape, canvas, depth).expect("cached shape"); + for (before, after) in [(&before.0, &after.0), (&before.1, &after.1)] { + assert!(before.tile_count() > 0); + assert_eq!(before.tile_count(), after.tile_count()); + for (coord, tile) in before.tiles() { + assert!(Arc::ptr_eq(tile, after.tile(*coord).expect("cached tile")), "shape {i}, {depth:?}: rerasterized cached pixels"); + } + } + } + } + shape_split::purge_cache(); +} diff --git a/crates/compose/tests/shape_split_source_budget.rs b/crates/compose/tests/shape_split_source_budget.rs new file mode 100644 index 0000000000..52f23c8c90 --- /dev/null +++ b/crates/compose/tests/shape_split_source_budget.rs @@ -0,0 +1,30 @@ +use std::sync::Arc; + +use photocraft_color::{Color, PixelFormat, SampleType}; +use photocraft_compose::shape_split; +use photocraft_doc::Fill; +use photocraft_doc::vector::{Path, ShapeLayer, ShapeStroke, Subpath}; +use photocraft_geom::{Rect, TileCoord}; +use photocraft_raster::Surface; + +/// Source pixels retained to identify a split must count toward its cache budget too. +#[test] +fn oversized_source_pixels_are_not_pinned_by_the_split_cache() { + shape_split::purge_cache(); + let mut source = Surface::new(PixelFormat::RGBA8); + // Shared tiles represent 256 MiB of source pixels without a large test allocation. + source.fill_rect(Rect::new(0, 0, 8192, 8192), &[1.0, 1.0, 1.0, 1.0]); + let source_tile = Arc::downgrade(source.tile(TileCoord::new(0, 0)).expect("source tile")); + let shape = ShapeLayer { + path: Path::new(vec![Subpath::polygon(&[(10.0, 10.0), (30.0, 10.0), (30.0, 30.0), (10.0, 30.0)])]), + fill: Some(Fill::Solid(Color::rgb(0.2, 0.4, 0.6))), + stroke: Some(ShapeStroke { width: 2.0, ..ShapeStroke::default() }), + cache: Some(source), + ..ShapeLayer::default() + }; + let parts = shape_split::split(&shape, Rect::new(0, 0, 64, 64), SampleType::U8).expect("stroked shape"); + drop(parts); + drop(shape); + assert!(source_tile.upgrade().is_none(), "the source plus split parts exceed the cache budget"); + shape_split::purge_cache(); +}