From 2130d01d4ad090911fb8e92d4ff71c9e67c1ef2d Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 8 Nov 2026 10:36:12 +0100 Subject: [PATCH] Footage cache (#3271): the memory accountant trims only preview frames in RAM PreviewMemory::trim_to counts the frames in RAM but took the next victim among all frames, GPU frames too: dropping one freed nothing it counts and lost a viewer frame. Victims are now taken among the frames an eligibility test accepts, in the same order (other scales first, then by the playhead): frames in RAM for the accountant, all frames for the cache's own budget. A test (with a GPU adapter) keeps the two oldest frames on the GPU; it fails on the old code. --- crates/ui-egui/src/frames.rs | 17 ++++++++++--- crates/ui-egui/src/frames/tests.rs | 39 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/frames.rs b/crates/ui-egui/src/frames.rs index 22a95dae3..75f364ab6 100644 --- a/crates/ui-egui/src/frames.rs +++ b/crates/ui-egui/src/frames.rs @@ -239,14 +239,18 @@ impl Cache { /// prefetch kept refilling while it evicted the next frames (#595). Frames of another scale /// than the one on screen go first (a zoom or resolution change). fn victim(&self) -> Option { + self.victim_of(|_| true) + } + /// [`Cache::victim`] among the frames `eligible` accepts (in the same order). + fn victim_of(&self, eligible: impl Fn(&FrameKey) -> bool) -> Option { if let Some(s) = self.scale - && let Some(i) = self.order.iter().position(|k| k.scale != s) + && let Some(i) = self.order.iter().position(|k| k.scale != s && eligible(k)) { return Some(i); } - let Some(p) = &self.playhead else { return (!self.order.is_empty()).then_some(0) }; + let Some(p) = &self.playhead else { return self.order.iter().position(&eligible) }; let mut farthest: Option<(i64, usize)> = None; - for (i, k) in self.order.iter().enumerate() { + for (i, k) in self.order.iter().enumerate().filter(|(_, k)| eligible(k)) { match p.ahead(k) { None => return Some(i), Some(d) if farthest.is_none_or(|(f, _)| d > f) => farthest = Some((d, i)), @@ -255,6 +259,10 @@ impl Cache { } farthest.map(|(_, i)| i) } + /// [`Cache::victim`] among the frames in RAM (a GPU frame's bytes are video memory). + fn cpu_victim(&self) -> Option { + self.victim_of(|k| self.map.get(k).is_some_and(|c| !c.img.is_gpu())) + } /// Video memory: drop the oldest GPU frames past their own budget, frames of another scale /// than the one on screen first (a zoom or resolution change). fn evict_gpu(&mut self) { @@ -354,8 +362,9 @@ impl effectcraft_engine::render::memory::MemoryHolder for PreviewMemory { } fn trim_to(&self, bytes: usize) { let Ok(mut c) = self.0.lock() else { return }; + // (only frames in RAM: dropping a GPU frame frees no RAM, and loses a viewer frame) while c.bytes.saturating_sub(c.gpu_bytes) > bytes { - let Some(old) = c.victim().and_then(|i| c.order.remove(i)) else { break }; + let Some(old) = c.cpu_victim().and_then(|i| c.order.remove(i)) else { break }; c.remove(&old); } } diff --git a/crates/ui-egui/src/frames/tests.rs b/crates/ui-egui/src/frames/tests.rs index 360ddf1b6..80950e9a9 100644 --- a/crates/ui-egui/src/frames/tests.rs +++ b/crates/ui-egui/src/frames/tests.rs @@ -2957,3 +2957,42 @@ mod frame_files { assert_eq!(healed.hits(), (1, 0), "the rewritten entry is used"); } } + +/// #3271 review: the memory accountant trims the preview frames held in RAM; GPU frames (video +/// memory, their own budget) are left alone, even when they are the least recently shown. +#[test] +fn the_accountant_trims_only_frames_in_ram() { + let Some(gpu) = Gpu::headless() else { + eprintln!("no GPU adapter: skipped"); + return; + }; + let texture = gpu.device().create_texture(&eframe::wgpu::TextureDescriptor { + label: None, + size: eframe::wgpu::Extent3d { width: 2, height: 2, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: eframe::wgpu::TextureDimension::D2, + format: eframe::wgpu::TextureFormat::Rgba8Unorm, + usage: eframe::wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }); + let gpu_frame = || FrameImage::Gpu(Arc::new(DisplayFrame { texture: texture.clone(), width: 2, height: 2 })); + let frames = Frames::default(); + { + let mut c = frames.cache.lock().unwrap(); + // The oldest frames are on the GPU. + c.insert(key(1, 0, 0), gpu_frame()); + c.insert(key(1, 1, 0), gpu_frame()); + c.insert(key(1, 2, 0), img()); + c.insert(key(1, 3, 0), img()); + } + let memory = PreviewMemory(frames.cache.clone()); + use effectcraft_engine::render::memory::MemoryHolder; + assert_eq!(memory.bytes(), 2 * 16); + memory.trim_to(16); + let c = frames.cache.lock().unwrap(); + assert!(c.map.contains_key(&key(1, 0, 0)) && c.map.contains_key(&key(1, 1, 0)), "the GPU frames stay"); + assert!(!c.map.contains_key(&key(1, 2, 0)) && c.map.contains_key(&key(1, 3, 0)), "the oldest frame in RAM went"); + drop(c); + assert_eq!(memory.bytes(), 16); +}