mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Keep small shape splits cached within the pixel budget
This commit is contained in:
1 parent
952c328b3d
commit
2c5cd7c780
3 files changed
+85
-18
No files matched your search
@@ -21,25 +21,23 @@ fn cache() -> &'static Mutex<(HashMap<u64, Entry>, u64)> {
|
||||
C.get_or_init(|| Mutex::new((HashMap::new(), 0)))
|
||||
}
|
||||
|
||||
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.bytes().len()).sum::<usize>()).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.bytes().len())).fold(0, usize::saturating_add)
|
||||
}
|
||||
|
||||
/// Drop every cached split (Edit › Purge › All), returning the parts' pixel bytes. Callers may
|
||||
/// 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
|
||||
}
|
||||
@@ -78,21 +76,21 @@ pub fn split(sh: &ShapeLayer, canvas: Rect, depth: SampleType) -> Option<(Surfac
|
||||
if let Some(cache) = &sh.cache {
|
||||
fit_stroke(&parts.0, &mut parts.1, cache, canvas);
|
||||
}
|
||||
let new_bytes = entry_bytes(&parts, sh.cache.as_ref());
|
||||
// Don't cache entries that hold no pixels or exceed the byte budget.
|
||||
if new_bytes == 0 || new_bytes > BUDGET {
|
||||
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);
|
||||
// Least recently used first, until the new entry fits the byte budget.
|
||||
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 {
|
||||
let held: usize = c.0.values().map(|e| entry_bytes(&e.0, e.2.as_ref())).sum();
|
||||
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 };
|
||||
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)
|
||||
@@ -215,7 +213,7 @@ 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()
|
||||
}
|
||||
|
||||
/// The split cache stays within its byte budget, evicting the least recently used, and
|
||||
@@ -225,7 +223,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.
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
Reference in new issue
Block a user