Files
photocraft/crates/compose/src/bounds.rs
SckraTandClaude be43d58f1a perf(compose): stop sweeping the bounds cache on every call once it is large (#4415)
compose::bounds caches per-tile content bounds and drops entries of dead tiles when it holds
more than 65,536 of them. Once more than 65,536 tiles were alive (a large document plus its
history), every call (every render tile, every GPU plan) walked the whole map again and kept
it all. The sweep now runs past a high-water mark: twice the entries that survived the last
sweep, and at least 65,536.

The per-tile scan moves to raster as Tile::content_bounds, used by both
Surface::content_bounds and the cache instead of two copies of the loop; an empty pixel
yields no content instead of a chunks_exact(0) panic.


Claude-Session: https://claude.ai/code/session_01QUda3K786uop8kvEQoUjSf

Co-authored-by: Claude <[email protected]>
2026-10-12 19:54:57 -04:00

92 lines
3.3 KiB
Rust

//! `Surface::content_bounds` with the per-tile scan cached by copy-on-write tile identity, so
//! layer bounds cost a hash lookup per tile instead of a pixel scan per render tile (the CPU
//! compositor asks for an effect layer's bounds in every tile; the GPU planner every frame).
use std::collections::HashMap;
use std::sync::{Arc, Mutex, Weak};
use photocraft_geom::Rect;
use photocraft_raster::{Surface, Tile};
struct Entry {
tile: Weak<Tile>,
/// Content bounds inside the tile (tile-local), for one default pixel.
default: Box<[u8]>,
bounds: Option<(u16, u16, u16, u16)>,
}
/// The entries, and the size past which dead ones are dropped (see [`next_sweep`]).
struct Cache {
entries: HashMap<usize, Entry>,
sweep_at: usize,
}
const MIN_SWEEP: usize = 1 << 16;
/// The size at which to sweep again after a sweep left `survivors` live entries: twice that (at
/// least [`MIN_SWEEP`]), so a cache full of live tiles isn't swept again on every call.
fn next_sweep(survivors: usize) -> usize {
survivors.saturating_mul(2).max(MIN_SWEEP)
}
fn cache() -> &'static Mutex<Cache> {
static C: std::sync::OnceLock<Mutex<Cache>> = std::sync::OnceLock::new();
C.get_or_init(|| Mutex::new(Cache { entries: HashMap::new(), sweep_at: MIN_SWEEP }))
}
/// `Surface::content_bounds`, cached per tile.
pub fn content_bounds(s: &Surface) -> Rect {
if s.tile_count() == 0 {
return Rect::EMPTY;
}
let fmt = s.format();
let mut dp = vec![0u8; fmt.bytes_per_pixel()];
photocraft_raster::encode_pixel(&fmt, &s.default_pixel(), &mut dp);
let mut c = cache().lock().unwrap_or_else(|e| e.into_inner());
if c.entries.len() > c.sweep_at {
c.entries.retain(|_, e| e.tile.strong_count() > 0);
c.sweep_at = next_sweep(c.entries.len());
}
let mut out = Rect::EMPTY;
for (coord, t) in s.tiles() {
let key = Arc::as_ptr(t) as usize;
let hit = c.entries.get(&key).filter(|e| e.tile.upgrade().is_some_and(|u| Arc::ptr_eq(&u, t)) && *e.default == *dp).map(|e| e.bounds);
let b = match hit {
Some(b) => b,
None => {
let b = t.content_bounds(&dp);
c.entries.insert(key, Entry { tile: Arc::downgrade(t), default: dp.clone().into_boxed_slice(), bounds: b });
b
}
};
if let Some((x0, y0, x1, y1)) = b {
let o = coord.rect();
let r = Rect::new(o.x0 + x0 as i32, o.y0 + y0 as i32, o.x0 + x1 as i32, o.y0 + y1 as i32);
out = if out.is_empty() { r } else { out.union(&r) };
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sweeps_again_only_past_twice_the_survivors() {
// A cache of live tiles past the minimum isn't swept on every call (it used to be).
assert_eq!(next_sweep(0), MIN_SWEEP);
assert_eq!(next_sweep(MIN_SWEEP / 2), MIN_SWEEP);
assert_eq!(next_sweep(MIN_SWEEP), 2 * MIN_SWEEP);
assert_eq!(next_sweep(usize::MAX), usize::MAX);
}
#[test]
fn a_shared_tile_reads_the_same_bounds_at_every_coordinate() {
let mut s = Surface::new(photocraft_color::PixelFormat::RGBA8);
s.fill_rect(Rect::new(-300, 10, 1300, 900), &[0.2, 0.4, 0.6, 1.0]);
assert_eq!(content_bounds(&s), Rect::new(-300, 10, 1300, 900));
assert_eq!(content_bounds(&s), s.content_bounds(), "cached");
}
}