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.
This commit is contained in:
vangel76 committed 2026-11-08 10:36:12 +01:00
1 parent 74099e32bc
commit 2130d01d4a
2 files changed
+52 -4

No files matched your search

+13 -4
View File
@@ -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<usize> {
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<usize> {
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<usize> {
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);
}
}
+39
View File
@@ -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);
}