mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Scan cache misses outside the bounds lock and respect concurrent Purge
This commit is contained in:
1 parent
dea55cbaf0
commit
60476d146f
1 file changed
+67
-23
@@ -19,6 +19,7 @@ struct Entry {
|
||||
struct Cache {
|
||||
entries: HashMap<usize, Entry>,
|
||||
sweep_at: usize,
|
||||
purge_generation: u64,
|
||||
}
|
||||
|
||||
const MIN_SWEEP: usize = 1 << 16;
|
||||
@@ -31,16 +32,22 @@ fn next_sweep(survivors: usize) -> usize {
|
||||
|
||||
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 }))
|
||||
C.get_or_init(|| Mutex::new(Cache { entries: HashMap::new(), sweep_at: MIN_SWEEP, purge_generation: 0 }))
|
||||
}
|
||||
|
||||
impl Cache {
|
||||
fn purge(&mut self) {
|
||||
self.entries.clear();
|
||||
self.sweep_at = MIN_SWEEP;
|
||||
self.purge_generation = self.purge_generation.wrapping_add(1);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn purge() {
|
||||
let mut cache = cache().lock().unwrap_or_else(|e| e.into_inner());
|
||||
cache.entries.clear();
|
||||
cache.sweep_at = MIN_SWEEP;
|
||||
cache().lock().unwrap_or_else(|e| e.into_inner()).purge();
|
||||
}
|
||||
|
||||
/// `Surface::content_bounds`, cached per tile.
|
||||
/// Surface content bounds, cached per tile.
|
||||
pub fn content_bounds(s: &Surface) -> Rect {
|
||||
if s.tile_count() == 0 {
|
||||
return Rect::EMPTY;
|
||||
@@ -48,26 +55,9 @@ pub fn content_bounds(s: &Surface) -> Rect {
|
||||
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 generation = photocraft_raster::spill::read_error_generation();
|
||||
let b = t.content_bounds(&dp);
|
||||
if photocraft_raster::spill::check_since(generation).is_ok() && t.scratch_health().is_ok() {
|
||||
c.entries.insert(key, Entry { tile: Arc::downgrade(t), default: dp.clone().into_boxed_slice(), bounds: b });
|
||||
}
|
||||
b
|
||||
}
|
||||
};
|
||||
let b = tile_bounds(cache(), t, &dp, || t.content_bounds(&dp));
|
||||
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);
|
||||
@@ -77,10 +67,64 @@ pub fn content_bounds(s: &Surface) -> Rect {
|
||||
out
|
||||
}
|
||||
|
||||
/// Pixel scans and spill reads must not hold the cache lock used by input handling.
|
||||
fn tile_bounds(cache: &Mutex<Cache>, tile: &Arc<Tile>, default: &[u8], scan: impl FnOnce() -> Option<(u16, u16, u16, u16)>) -> Option<(u16, u16, u16, u16)> {
|
||||
let key = Arc::as_ptr(tile) as usize;
|
||||
let purge_generation = {
|
||||
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());
|
||||
}
|
||||
if let Some(hit) = c.entries.get(&key).filter(|e| e.tile.upgrade().is_some_and(|u| Arc::ptr_eq(&u, tile)) && *e.default == *default) {
|
||||
return hit.bounds;
|
||||
}
|
||||
c.purge_generation
|
||||
};
|
||||
let generation = photocraft_raster::spill::read_error_generation();
|
||||
let bounds = scan();
|
||||
if photocraft_raster::spill::check_since(generation).is_ok() && tile.scratch_health().is_ok() {
|
||||
let mut c = cache.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if c.purge_generation == purge_generation {
|
||||
c.entries.insert(key, Entry { tile: Arc::downgrade(tile), default: default.into(), bounds });
|
||||
}
|
||||
}
|
||||
bounds
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cache_misses_scan_without_holding_the_shared_lock() {
|
||||
let mut surface = Surface::new(photocraft_color::PixelFormat::RGBA8);
|
||||
surface.fill_rect(Rect::new(1, 2, 30, 40), &[0.2, 0.4, 0.6, 1.0]);
|
||||
let (_, tile) = surface.tiles().next().unwrap();
|
||||
let cache = Mutex::new(Cache { entries: HashMap::new(), sweep_at: MIN_SWEEP, purge_generation: 0 });
|
||||
let default = [0u8; 4];
|
||||
let actual = tile_bounds(&cache, tile, &default, || {
|
||||
assert!(cache.try_lock().is_ok(), "pixel scanning must not hold the shared bounds-cache lock");
|
||||
tile.content_bounds(&default)
|
||||
});
|
||||
assert_eq!(actual, tile.content_bounds(&default));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn purging_during_a_scan_does_not_repopulate_the_cache() {
|
||||
let mut surface = Surface::new(photocraft_color::PixelFormat::RGBA8);
|
||||
surface.fill_rect(Rect::new(1, 2, 30, 40), &[0.2, 0.4, 0.6, 1.0]);
|
||||
let (_, tile) = surface.tiles().next().unwrap();
|
||||
let cache = Mutex::new(Cache { entries: HashMap::new(), sweep_at: MIN_SWEEP, purge_generation: 0 });
|
||||
let default = [0u8; 4];
|
||||
let actual = tile_bounds(&cache, tile, &default, || {
|
||||
cache.lock().unwrap().purge();
|
||||
tile.content_bounds(&default)
|
||||
});
|
||||
assert_eq!(actual, tile.content_bounds(&default), "an in-flight caller still gets its bounds");
|
||||
assert!(cache.lock().unwrap().entries.is_empty(), "Purge also discards in-flight cache fills");
|
||||
}
|
||||
|
||||
#[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).
|
||||
|
||||
Reference in new issue
Block a user