diff --git a/crates/engine/src/settings/session_settings.rs b/crates/engine/src/settings/session_settings.rs index 77010a8cf..516932932 100644 --- a/crates/engine/src/settings/session_settings.rs +++ b/crates/engine/src/settings/session_settings.rs @@ -115,11 +115,12 @@ impl Session { self.layer_cache.set_budget(b.layer); self.footage.set_cache_budget(b.media); // One budget over the big caches together (#3271): the layer cache, the footage frames - // and decoded planes, and the preview frames' share. + // and decoded planes (sharing the footage budget, see `set_cache_budget`), and the + // preview frames' share. let accountant = effectcraft_render::memory::Accountant::global(); let layers: std::sync::Arc = self.layer_cache.clone(); accountant.register(&layers); - accountant.set_budget(b.layer.saturating_add(b.media).saturating_add(b.preview).saturating_add(effectcraft_render::memory::PLANE_BUDGET)); + accountant.set_budget(b.layer.saturating_add(b.media).saturating_add(b.preview)); } /// Take the latest reading of the system's memory and start the next one in the background diff --git a/crates/media/src/exr_channels.rs b/crates/media/src/exr_channels.rs index 42c3e47eb..a49bf009e 100644 --- a/crates/media/src/exr_channels.rs +++ b/crates/media/src/exr_channels.rs @@ -77,14 +77,18 @@ pub(crate) enum Rgb { Unsupported, } -/// How many bytes of decoded planes the plane store keeps, like the frame cache's default -/// ([`crate::DEFAULT_BUDGET`]): two 2560×1440 files of 32 channels (470 MB each, as every plane -/// is f32). A soft bound: the newest file is kept whatever its size (so a frame's picture and its +/// How many bytes of decoded planes the plane store keeps until the Footage Frame Cache is set +/// (then it shares that budget with the frame cache, see [`crate::exr_planes::set_budget`]), +/// like the frame cache's default ([`crate::DEFAULT_BUDGET`]): two 2560×1440 files of 32 +/// channels (470 MB each, as every plane is f32). A soft bound: the newest file is kept whatever its size (so a frame's picture and its /// effects share its decode), and files still in use are never dropped (planes an effect holds, /// pictures waiting for their effects, decodes in progress), so memory can go above it while /// they are. (Measured on such a sequence decoded one frame at a time: 1 GiB held peak memory /// about 1 GB lower than 2 GiB, at the same speed.) -pub const CACHE_BUDGET: usize = effectcraft_render::memory::PLANE_BUDGET; +pub const CACHE_BUDGET: usize = if cfg!(target_arch = "wasm32") { WEB_CACHE_CAP } else { crate::DEFAULT_BUDGET }; + +/// The most decoded planes the plane store keeps in the browser, whatever the footage budget. +pub const WEB_CACHE_CAP: usize = 512 << 20; /// The index of an EXR file's bytes and the planes of `ids` (all when `None`). `None` when it is /// not a readable EXR or the planes need too much memory. diff --git a/crates/media/src/exr_planes.rs b/crates/media/src/exr_planes.rs index c8c41befd..5ff74c38c 100644 --- a/crates/media/src/exr_planes.rs +++ b/crates/media/src/exr_planes.rs @@ -263,7 +263,7 @@ struct Entry { /// least as much fails the same way until the file is forgotten. failed: Vec<(Want, Refused)>, bytes: usize, - /// When it was last asked for or finished decoding (a counter). + /// When it was last asked for or finished decoding ([`effectcraft_render::memory::tick`]). used: u64, } @@ -276,7 +276,6 @@ impl Entry { struct State { entries: Vec, - clock: u64, /// Pictures' planes kept for their effects, oldest first (at most [`HELD_FILES`]). held: VecDeque<(String, Arc)>, } @@ -312,7 +311,8 @@ impl Drop for Owning<'_> { /// Decoded planes by file, bounded by bytes. pub(crate) struct PlaneStore { state: Mutex, - budget: usize, + /// Bytes it keeps at most ([`set_budget`]). + budget: std::sync::atomic::AtomicUsize, } thread_local! { @@ -428,7 +428,17 @@ fn cap_of(path: &str) -> usize { impl PlaneStore { pub(crate) const fn new(budget: usize) -> PlaneStore { - PlaneStore { state: Mutex::new(State { entries: Vec::new(), clock: 0, held: VecDeque::new() }), budget } + PlaneStore { state: Mutex::new(State { entries: Vec::new(), held: VecDeque::new() }), budget: std::sync::atomic::AtomicUsize::new(budget) } + } + + fn budget(&self) -> usize { + self.budget.load(std::sync::atomic::Ordering::Relaxed) + } + + /// Keep at most `bytes` from now on, dropping the oldest entries not in use at once. + pub(crate) fn set_budget(&self, bytes: usize) { + self.budget.store(bytes, std::sync::atomic::Ordering::Relaxed); + Self::drop_oldest(&mut self.lock(), bytes, CACHE_FILES, None); } fn lock(&self) -> MutexGuard<'_, State> { @@ -474,8 +484,8 @@ impl PlaneStore { #[allow(clippy::too_many_arguments)] fn step(&self, path: &str, source: &ExrSource, index: &Arc, want: &Want, by: Use, opened: &mut Option, since: u64) -> Step { let mut st = self.lock(); - st.clock += 1; - let clock = st.clock; + // (shared with the footage frames: one least-recently-used order across both) + let clock = effectcraft_render::memory::tick(); let at = match st.entries.iter().position(|e| e.path == path) { Some(i) => i, None => { @@ -574,8 +584,8 @@ impl PlaneStore { /// meanwhile (then nothing made of it is kept or handed out); drop what doesn't fit. fn finish(&self, path: &str, flight: &Arc, outcome: Result, Refused>) -> Result, Refused> { let mut st = self.lock(); - st.clock += 1; - let clock = st.clock; + // (shared with the footage frames: one least-recently-used order across both) + let clock = effectcraft_render::memory::tick(); let Some(e) = st.entries.iter_mut().find(|e| e.path == path && e.flight.as_ref().is_some_and(|f| Arc::ptr_eq(f, flight))) else { return match outcome { Err(e @ (Refused::Cancelled | Refused::Stopped)) => Err(e), @@ -603,7 +613,7 @@ impl PlaneStore { } Err(Refused::Cancelled | Refused::Forgotten | Refused::Stopped) => {} } - Self::drop_oldest(&mut st, self.budget, CACHE_FILES, Some(path)); + Self::drop_oldest(&mut st, self.budget(), CACHE_FILES, Some(path)); outcome } @@ -673,6 +683,32 @@ impl PlaneStore { st.held.retain(|(p, _)| p != path); } + /// The index of the least recently used entry that may be dropped (holding bytes, not in + /// use, not held for a picture's effects). + fn oldest_droppable(st: &State) -> Option { + st.entries + .iter() + .enumerate() + .filter(|(_, e)| e.bytes > 0 && !e.in_use()) + .filter(|(_, e)| !e.batch.as_ref().is_some_and(|b| st.held.iter().any(|(_, h)| Arc::ptr_eq(h, b)))) + .min_by_key(|(_, e)| e.used) + .map(|(i, _)| i) + } + + /// When the least recently used entry that may be dropped was last used (the memory + /// accountant's footage group, #3271). + pub(crate) fn oldest_use(&self) -> Option { + let st = self.lock(); + Self::oldest_droppable(&st).and_then(|i| st.entries.get(i)).map(|e| e.used) + } + + /// Drop the least recently used entry that may be dropped; the bytes freed. + pub(crate) fn drop_oldest_one(&self) -> usize { + let mut st = self.lock(); + let Some(i) = Self::oldest_droppable(&st) else { return 0 }; + if i < st.entries.len() { st.entries.remove(i).bytes } else { 0 } + } + /// Drop least recently used entries not in use until at most `bytes` are held (the memory /// accountant, #3271). fn trim_to(&self, bytes: usize) { @@ -983,6 +1019,30 @@ impl effectcraft_render::memory::MemoryHolder for SharedStore { fn trim_to(&self, bytes: usize) { STORE.trim_to(bytes); } + fn group(&self) -> Option<&'static str> { + Some(effectcraft_render::memory::FOOTAGE) + } + fn oldest_use(&self) -> Option { + STORE.oldest_use() + } + fn drop_oldest(&self) -> usize { + STORE.drop_oldest_one() + } +} + +/// The footage budget (Settings ▸ Memory & CPU ▸ Footage Frame Cache): the shared plane store +/// keeps at most this much (in the browser at most [`crate::exr_channels::WEB_CACHE_CAP`]); the +/// footage frames and the planes together stay within it (the memory accountant's +/// [`effectcraft_render::memory::FOOTAGE`] group). +pub(crate) fn set_budget(bytes: usize) { + let bytes = if cfg!(target_arch = "wasm32") { bytes.min(crate::exr_channels::WEB_CACHE_CAP) } else { bytes }; + store().set_budget(bytes); +} + +/// The bytes the shared plane store keeps at most. +#[cfg(test)] +pub(crate) fn budget() -> usize { + STORE.budget() } /// Is `path` an OpenEXR file by name (the store reads those; other files, and files named so diff --git a/crates/media/src/exr_planes/tests.rs b/crates/media/src/exr_planes/tests.rs index 4360db7a6..8d8b51e9f 100644 --- a/crates/media/src/exr_planes/tests.rs +++ b/crates/media/src/exr_planes/tests.rs @@ -1267,3 +1267,90 @@ fn trimming_for_bytes_keeps_remembered_failures() { assert!(store.get(path, &broken, &index, &all, Use::Other).is_err()); assert_eq!(decodes_of(path).len(), 1, "not read again"); } + +/// #3271 (user test): the decoded planes have no budget of their own; they share the Footage +/// Frame Cache with the footage frames. A footage budget of 10000 MB lets the plane store hold +/// far more than 1 GB, and a change of the setting resizes it at once. +#[test] +fn the_footage_budget_sizes_the_plane_store() { + use effectcraft_render::FootageSource; + let pool = crate::MediaPool::new(); + pool.set_cache_budget(10_000 << 20); + assert_eq!(super::budget(), 10_000 << 20); + assert!(super::budget() > 1 << 30, "more than the old fixed 1 GB"); + let acc = effectcraft_render::memory::Accountant::global(); + assert_eq!(acc.group_budget(effectcraft_render::memory::FOOTAGE), Some(10_000 << 20), "frames and planes share it"); + pool.set_cache_budget(3_000 << 20); + assert_eq!(super::budget(), 3_000 << 20, "resized at run time"); + // (back to the default for the tests beside it) + pool.set_cache_budget(CACHE_BUDGET); +} + +/// #3271 (user decision): the footage frames and the decoded planes share one footage budget +/// with one least-recently-used order: filling the frame cache drops the oldest plane sets, +/// filling the plane store drops the oldest frames; neither side has a share of its own. +#[test] +fn frames_and_planes_make_room_for_each_other() { + use effectcraft_render::memory::{Accountant, FOOTAGE, MemoryHolder}; + struct Planes(Arc); + impl MemoryHolder for Planes { + fn name(&self) -> &'static str { + "planes" + } + fn bytes(&self) -> usize { + self.0.lock().entries.iter().map(|e| e.bytes).sum() + } + fn priority(&self) -> u8 { + 1 + } + fn trim_to(&self, bytes: usize) { + self.0.trim_to(bytes); + } + fn group(&self) -> Option<&'static str> { + Some(FOOTAGE) + } + fn oldest_use(&self) -> Option { + self.0.oldest_use() + } + fn drop_oldest(&self) -> usize { + self.0.drop_oldest_one() + } + } + let bytes: Arc<[u8]> = write(vec![part(None, &RGBA, (32, 32), Compression::ZIP1)], (32, 32)).into(); + let one = 4 * (32 * 32 * 4 + 1); + let budget = 5 * one; + let store = Arc::new(PlaneStore::new(budget)); + let pool = crate::MediaPool::with_budget(budget); + let acc = Accountant::new(usize::MAX); + let holders: Vec> = vec![Arc::new(Planes(store.clone())), pool.memory_holder()]; + for h in &holders { + acc.register(h); + } + acc.set_group_budget(FOOTAGE, budget); + let all = Want { all: true, ids: Default::default() }; + let planes = |p: &str| { + let source = ExrSource::Bytes(bytes.clone()); + let index = crate::exr_index::get(p, &source).unwrap(); + drop(store.get(p, &source, &index, &all, Use::Other).unwrap()); + }; + // Plane sets first, then frames: the frames push the oldest plane sets out. + planes("/3271-share-p1.exr"); + planes("/3271-share-p2.exr"); + planes("/3271-share-p3.exr"); + for f in ["/f1", "/f2", "/f3"] { + pool.keep_test_frame(f, 32, 32); + acc.balance(); + } + assert_eq!(pool.test_frames(), ["/f1", "/f2", "/f3"], "the frames stay"); + assert!(!store.cached().contains(&"/3271-share-p1.exr".to_string()), "the oldest plane set went: {:?}", store.cached()); + assert!(store.cached().contains(&"/3271-share-p3.exr".to_string())); + // Now plane sets: they push the oldest frames out. + for p in ["/3271-share-p4.exr", "/3271-share-p5.exr", "/3271-share-p6.exr"] { + planes(p); + acc.balance(); + } + let frames = pool.test_frames(); + assert!(!frames.contains(&"/f1".to_string()), "the oldest frame went: {frames:?}"); + assert!(store.cached().contains(&"/3271-share-p6.exr".to_string()), "the newest plane set stays"); + assert!(acc.total() <= budget, "{} of {budget}", acc.total()); +} diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index f10127dcd..040796d97 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -64,6 +64,8 @@ struct Entry { files: Arc, stamp: u64, bytes: usize, + /// When it was last used ([`effectcraft_render::memory::tick`], shared with the plane store). + used: u64, } /// Conformed audio files written by builds before this version are ignored (and written again): @@ -121,6 +123,7 @@ impl Lru { self.order.remove(&e.stamp); self.clock += 1; e.stamp = self.clock; + e.used = effectcraft_render::memory::tick(); self.order.insert(e.stamp, k.clone()); Some((e.img.clone(), e.files.clone())) } @@ -128,7 +131,7 @@ impl Lru { fn insert(&mut self, k: Key, img: Arc, files: Arc, budget: usize) { let bytes = img.data.len() * std::mem::size_of::() + 64; self.clock += 1; - if let Some(old) = self.map.insert(k.clone(), Entry { img, files, stamp: self.clock, bytes }) { + if let Some(old) = self.map.insert(k.clone(), Entry { img, files, stamp: self.clock, bytes, used: effectcraft_render::memory::tick() }) { self.order.remove(&old.stamp); self.bytes -= old.bytes; } @@ -156,6 +159,21 @@ impl Lru { } } } + /// The least recently used frame it may drop (the newest always stays) and its last use. + fn oldest(&self) -> Option<(&Key, u64)> { + if self.map.len() <= 1 { + return None; + } + let (_, k) = self.order.first_key_value()?; + Some((k, self.map.get(k)?.used)) + } + /// Drop the least recently used frame it may drop; the bytes freed. + fn drop_oldest(&mut self) -> usize { + let Some(k) = self.oldest().map(|(k, _)| k.clone()) else { return 0 }; + let had = self.bytes; + self.remove(&k); + had.saturating_sub(self.bytes) + } /// A recycled buffer of exactly `len` pixels, or an empty Vec. fn take_spare(&mut self, len: usize) -> Vec { match self.spare.iter().position(|b| b.len() == len) { @@ -237,6 +255,8 @@ struct Recent { bytes: usize, /// The frame cache's clock when it was kept. stamp: u64, + /// When it was kept ([`effectcraft_render::memory::tick`]). + used: u64, } /// At most this many forgotten paths are remembered ([`CacheState::forgotten`]); beyond that @@ -278,7 +298,7 @@ impl CacheState { return; } let stamp = self.lru.clock; - self.recent.push_back(Recent { key, img, files, bytes, stamp }); + self.recent.push_back(Recent { key, img, files, bytes, stamp, used: effectcraft_render::memory::tick() }); self.trim_recent(budget); } @@ -611,6 +631,29 @@ impl MediaPool { self.inner.budget() } + /// Keep a frame of `width` by `height` as the frame of `path` (tests of the footage budget). + #[cfg(test)] + pub(crate) fn keep_test_frame(&self, path: &str, width: u32, height: u32) { + let key = Key { path: path.into(), frame: 0, interp: 0, matte: [0; 3], size: (0, 0), nominal: (0, 0), rows: None }; + let budget = self.inner.budget(); + lock(&self.inner.cache).lru.insert(key, Arc::new(Image::new(width, height)), Arc::default(), budget); + } + + /// The paths of the frames [`MediaPool::keep_test_frame`] kept that are still cached. + #[cfg(test)] + pub(crate) fn test_frames(&self) -> Vec { + let c = lock(&self.inner.cache); + let mut v: Vec = c.lru.map.keys().map(|k| k.path.to_string()).collect(); + v.sort(); + v + } + + /// The frame cache as the memory accountant sees it (tests). + #[cfg(test)] + pub(crate) fn memory_holder(&self) -> Arc { + self.inner.clone() + } + /// Change the frame-cache budget (evicts immediately when shrinking). pub fn set_budget(&self, bytes: usize) { self.inner.budget.store(bytes as u64, Ordering::Relaxed); @@ -1514,6 +1557,25 @@ impl effectcraft_render::memory::MemoryHolder for Inner { c.recent.clear(); c.lru.evict(bytes); } + fn group(&self) -> Option<&'static str> { + Some(effectcraft_render::memory::FOOTAGE) + } + fn oldest_use(&self) -> Option { + let c = lock(&self.cache); + let frame = c.lru.oldest().map(|(_, t)| t); + let still = c.recent.front().map(|r| r.used); + frame.into_iter().chain(still).min() + } + fn drop_oldest(&self) -> usize { + let mut c = lock(&self.cache); + let frame = c.lru.oldest().map(|(_, t)| t); + let still = c.recent.front().map(|r| r.used); + match (frame, still) { + (Some(f), Some(s)) if s < f => c.recent.pop_front().map_or(0, |r| r.bytes), + (None, Some(_)) => c.recent.pop_front().map_or(0, |r| r.bytes), + _ => c.lru.drop_oldest(), + } + } } impl MediaPool { @@ -1540,8 +1602,12 @@ impl FootageSource for MediaPool { } self.inner.model(&footage.path) } + /// The Footage Frame Cache: the frame cache, the decoded OpenEXR planes and both together + /// (#3271: the planes share the footage budget, they have none of their own). fn set_cache_budget(&self, bytes: usize) { self.set_budget(bytes); + crate::exr_planes::set_budget(bytes); + effectcraft_render::memory::Accountant::global().set_group_budget(effectcraft_render::memory::FOOTAGE, bytes); } fn purge(&self) { self.clear_frames(); diff --git a/crates/render/src/memory.rs b/crates/render/src/memory.rs index be806b507..0f7210a0e 100644 --- a/crates/render/src/memory.rs +++ b/crates/render/src/memory.rs @@ -5,6 +5,13 @@ //! frees from the lowest priority first, in large steps: down to [`TRIM_TO`] of the budget, so //! a cache doesn't fill up and trim again with every insert. //! +//! Holders can share a budget of their own ([`MemoryHolder::group`], +//! [`Accountant::set_group_budget`]): the footage frames and the decoded footage planes +//! ([`FOOTAGE`]) together take at most the Footage Frame Cache. Within a group the least recently +//! used item goes first, whichever holder keeps it ([`MemoryHolder::oldest_use`], stamped with +//! [`tick`]): a frame can push out plane sets and plane sets can push out frames; neither has a +//! share of its own. +//! //! A holder that had to make again what it let go of shortly before (its "refills", //! [`MemoryHolder::refills`]) is thrashing: its priority goes up by one until it settles, so the //! data that is needed again is kept longest. @@ -19,12 +26,16 @@ use std::sync::{Arc, Mutex, PoisonError, Weak}; /// The share of the budget the accountant frees down to once it is over (hysteresis). pub const TRIM_TO: f64 = 0.9; -/// The decoded footage planes' own budget (the plane store in `effectcraft-media`): part of the -/// common budget's sum. -#[cfg(not(target_arch = "wasm32"))] -pub const PLANE_BUDGET: usize = 1 << 30; -#[cfg(target_arch = "wasm32")] -pub const PLANE_BUDGET: usize = 512 << 20; +/// The group of the footage frames and the decoded footage planes: together within the +/// Footage Frame Cache ([`Accountant::set_group_budget`]). +pub const FOOTAGE: &str = "footage"; + +/// A use stamp the holders of a group share: later uses get larger stamps (a counter, not a +/// clock: two uses never tie). +pub fn tick() -> u64 { + static TICK: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); + TICK.fetch_add(1, Ordering::Relaxed) +} /// How long a holder that refilled stays protected. const THRASH_FOR: std::time::Duration = std::time::Duration::from_secs(10); @@ -48,6 +59,19 @@ pub trait MemoryHolder: Send + Sync { fn refills(&self) -> u64 { 0 } + /// The budget it shares with other holders ([`Accountant::set_group_budget`]), if any. + fn group(&self) -> Option<&'static str> { + None + } + /// The [`tick`] its least recently used item it may drop was last used at (`None`: nothing + /// to drop, or it doesn't tell; then its group trims it by priority). + fn oldest_use(&self) -> Option { + None + } + /// Drop its least recently used item it may drop; the bytes freed (0: nothing dropped). + fn drop_oldest(&self) -> usize { + 0 + } } struct Registered { @@ -60,6 +84,8 @@ struct Registered { /// The common budget of the big caches (see the module notes). pub struct Accountant { budget: AtomicUsize, + /// The budgets holders share ([`MemoryHolder::group`]). + groups: Mutex>, holders: Mutex>, /// Bytes freed by the accountant so far (stats, tests). freed: AtomicUsize, @@ -77,7 +103,13 @@ impl Default for Accountant { impl Accountant { /// An accountant with `budget` bytes (`usize::MAX`: no limit). pub const fn new(budget: usize) -> Accountant { - Accountant { budget: AtomicUsize::new(budget), holders: Mutex::new(Vec::new()), freed: AtomicUsize::new(0), balancing: Mutex::new(None) } + Accountant { + budget: AtomicUsize::new(budget), + groups: Mutex::new(Vec::new()), + holders: Mutex::new(Vec::new()), + freed: AtomicUsize::new(0), + balancing: Mutex::new(None), + } } /// The process's accountant. @@ -96,6 +128,23 @@ impl Accountant { self.budget.load(Ordering::Relaxed) } + /// Set the budget the holders of `group` share and free down to it at once. + pub fn set_group_budget(&self, group: &'static str, bytes: usize) { + { + let mut g = self.groups.lock().unwrap_or_else(PoisonError::into_inner); + match g.iter_mut().find(|(n, _)| *n == group) { + Some(e) => e.1 = bytes, + None => g.push((group, bytes)), + } + } + self.balance(); + } + + /// The budget the holders of `group` share (`None`: none set). + pub fn group_budget(&self, group: &str) -> Option { + self.groups.lock().unwrap_or_else(PoisonError::into_inner).iter().find(|(n, _)| *n == group).map(|(_, b)| *b) + } + /// Keep `holder` within the common budget (held weakly). pub fn register(&self, holder: &Arc) { let mut h = self.holders.lock().unwrap_or_else(PoisonError::into_inner); @@ -111,7 +160,7 @@ impl Accountant { /// A holder grew (an insert): balance, at most every [`BALANCE_EVERY`] (the holders' locks /// aren't taken on every insert). pub fn note_growth(&self) { - if self.budget() == usize::MAX { + if self.budget() == usize::MAX && self.groups.lock().unwrap_or_else(PoisonError::into_inner).is_empty() { return; } let recent = self.balancing.try_lock().map(|g| g.is_some_and(|t| t.elapsed() < BALANCE_EVERY)).unwrap_or(true); @@ -153,24 +202,62 @@ impl Accountant { self.freed.load(Ordering::Relaxed) } - /// When the holders together are over the budget, free down to [`TRIM_TO`] of it, lowest - /// priority first (a holder is trimmed as far as needed before the next one is touched). + /// When the holders of a group are over its budget, or all holders over the common one, + /// free down to [`TRIM_TO`] of it, lowest priority first (a holder is trimmed as far as + /// needed before the next one is touched): the groups first, then all holders. pub fn balance(&self) { let budget = self.budget(); - if budget == usize::MAX { + let groups = self.groups.lock().unwrap_or_else(PoisonError::into_inner).clone(); + if budget == usize::MAX && groups.is_empty() { return; } // (one at a time: a balance running meanwhile does the work) let Ok(mut last) = self.balancing.try_lock() else { return }; *last = Some(web_time::Instant::now()); let mut holders = self.alive(); - let mut total: usize = holders.iter().map(|(h, _)| h.bytes()).fold(0, usize::saturating_add); + holders.sort_by_key(|(_, p)| *p); + for (group, bytes) in groups { + let members: Vec<&Arc> = holders.iter().filter(|(h, _)| h.group() == Some(group)).map(|(h, _)| h).collect(); + self.trim_oldest(&members, bytes); + self.trim(&members, bytes); + } + if budget != usize::MAX { + let all: Vec<&Arc> = holders.iter().map(|(h, _)| h).collect(); + self.trim(&all, budget); + } + } + + /// Free from `holders` least recently used first, whichever holder keeps it, down to + /// [`TRIM_TO`] of `budget` when they hold more than it (holders that tell their oldest use). + fn trim_oldest(&self, holders: &[&Arc], budget: usize) { + let mut total: usize = holders.iter().map(|h| h.bytes()).fold(0, usize::saturating_add); if total <= budget { return; } let target = (budget as f64 * TRIM_TO) as usize; - holders.sort_by_key(|(_, p)| *p); - for (h, _) in holders { + let mut left: Vec<&Arc> = holders.to_vec(); + while total > target { + let Some((i, _)) = left.iter().enumerate().filter_map(|(i, h)| Some((i, h.oldest_use()?))).min_by_key(|(_, t)| *t) else { break }; + let Some(h) = left.get(i) else { break }; + let freed = h.drop_oldest(); + if freed == 0 { + left.remove(i); + continue; + } + total = total.saturating_sub(freed); + self.freed.fetch_add(freed, Ordering::Relaxed); + } + } + + /// Free from `holders` (lowest priority first) down to [`TRIM_TO`] of `budget` when they + /// hold more than it. + fn trim(&self, holders: &[&Arc], budget: usize) { + let mut total: usize = holders.iter().map(|h| h.bytes()).fold(0, usize::saturating_add); + if total <= budget { + return; + } + let target = (budget as f64 * TRIM_TO) as usize; + for h in holders { if total <= target { break; } @@ -196,11 +283,15 @@ mod tests { step: usize, priority: u8, refills: Mutex, + group: Option<&'static str>, } impl Fake { fn new(name: &'static str, bytes: usize, priority: u8) -> Arc { - Arc::new(Fake { name, bytes: Mutex::new(bytes), step: 10, priority, refills: Mutex::new(0) }) + Arc::new(Fake { name, bytes: Mutex::new(bytes), step: 10, priority, refills: Mutex::new(0), group: None }) + } + fn in_group(name: &'static str, bytes: usize, priority: u8, group: &'static str) -> Arc { + Arc::new(Fake { name, bytes: Mutex::new(bytes), step: 10, priority, refills: Mutex::new(0), group: Some(group) }) } fn held(&self) -> usize { *self.bytes.lock().unwrap() @@ -229,6 +320,34 @@ mod tests { fn refills(&self) -> u64 { *self.refills.lock().unwrap() } + fn group(&self) -> Option<&'static str> { + self.group + } + } + + /// #3271: holders of a group share its budget and make room for each other (lowest + /// priority first) before the common budget is looked at; a holder outside the group is + /// left alone while the common budget holds. + #[test] + fn a_group_shares_its_budget() { + let acc = Accountant::new(usize::MAX); + let (frames, planes, layers) = (Fake::in_group("frames", 400, 0, FOOTAGE), Fake::in_group("planes", 500, 1, FOOTAGE), Fake::new("layers", 800, 2)); + let held: Vec> = vec![holder(&frames), holder(&planes), holder(&layers)]; + for h in &held { + acc.register(h); + } + acc.set_group_budget(FOOTAGE, 1000); + assert_eq!((frames.held(), planes.held(), layers.held()), (400, 500, 800), "within the group's budget"); + planes.fill(300); + acc.balance(); + // 1200 in the group, down to 900: the frames make room for the planes. + assert_eq!((frames.held(), planes.held(), layers.held()), (100, 800, 800)); + // A larger footage budget: the planes alone may hold more than the old fixed 1 GB. + acc.set_group_budget(FOOTAGE, 10 << 30); + planes.fill(2 << 30); + acc.balance(); + assert!(planes.held() > 1 << 30, "{}", planes.held()); + assert_eq!(acc.group_budget(FOOTAGE), Some(10 << 30)); } fn holder(f: &Arc) -> Arc { diff --git a/docs/preferences.md b/docs/preferences.md index 82dcafb6d..74d440347 100644 --- a/docs/preferences.md +++ b/docs/preferences.md @@ -80,7 +80,10 @@ Commands (CLI, MCP, control channel): - `audio.outputLeft`: Left (Audio Output Mapping) - `audio.outputRight`: Right (Audio Output Mapping) - `memory.layerCacheMb`: Layer Cache (in the browser at most 512 MB, see [web.md](web.md)) -- `memory.mediaCacheMb`: Footage Frame Cache (in the browser at most 256 MB) +- `memory.mediaCacheMb`: Footage Frame Cache (in the browser at most 256 MB). It covers the + decoded footage frames and the decoded OpenEXR channels together: one budget, the least + recently used frame or channel set goes first, whichever it is (in the browser the channels + take at most 512 MB of it) - `memory.previewCacheMb`: Preview (RAM) Cache - `threeD.defaultRenderer`: Default 3D Renderer - `labels.N.name` / `labels.N.color`: the 16 label names and colours, used by the Label menu,