diff --git a/crates/engine/src/cmd/app_more.rs b/crates/engine/src/cmd/app_more.rs index ea4230852..be52fb232 100644 --- a/crates/engine/src/cmd/app_more.rs +++ b/crates/engine/src/cmd/app_more.rs @@ -424,7 +424,8 @@ pub fn system_report(s: &Session) -> Value { let gb = |b: u64| format!("{:.1} GB", b as f64 / (1u64 << 30) as f64); let export: Vec<&str> = s.exporter.as_ref().map(|e| e.formats().into_iter().map(|f| f.label()).collect()).unwrap_or_default(); let gpu = s.accel.as_ref().map(|a| a.name()); - let budgets = s.prefs.cache_budgets(mem.or(s.sys_memory)); + let read = mem.or(s.sys_memory); + let budget = s.prefs.memory_budget(read, read.is_some_and(|m| m.still_low(s.memory_low))); let mut issues = vec![]; if gpu.is_none() { issues.push(match &s.accel_note { @@ -448,7 +449,7 @@ pub fn system_report(s: &Session) -> Value { "gpu": gpu, "gpuNote": if gpu.is_none() { s.accel_note.clone() } else { None }, "windowGraphics": s.window_adapter, - "caches": {"layer": gb(budgets.layer as u64), "footage": gb(budgets.media as u64), "preview": gb(budgets.preview as u64)}, + "caches": {"budget": gb(budget.bytes as u64), "inUse": gb(effectcraft_render::memory::Accountant::global().total() as u64), "reduced": budget.reduced}, "import": IMPORT_FORMATS, "export": export, "logging": crate::logging::is_enabled(), @@ -482,12 +483,7 @@ pub fn report_text(r: &Value) -> String { t.push_str(&format!("Window graphics: {w}\n")); } let c = &r["caches"]; - t.push_str(&format!( - "Cache budgets: layer {}, footage {}, preview {}\n", - c["layer"].as_str().unwrap_or("?"), - c["footage"].as_str().unwrap_or("?"), - c["preview"].as_str().unwrap_or("?") - )); + t.push_str(&format!("Cache memory: {} in use of {}\n", c["inUse"].as_str().unwrap_or("?"), c["budget"].as_str().unwrap_or("?"))); t.push_str(&format!("Import: {}\nExport: {}\n", list("import"), list("export"))); let issues = list("issues"); t.push_str(&if issues.is_empty() { "No compatibility issues found.".to_string() } else { format!("Issues: {issues}") }); diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index cebd5473b..904aaaa55 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -218,6 +218,9 @@ pub struct Session { /// The last physical-memory reading (Settings ▸ Memory & CPU budgets); `None` = unknown /// (headless sessions don't query it, see [`Session::memory_tick`]). pub sys_memory: Option, + /// The system is in a low-memory episode (the cache budgets stay reduced until it ends, see + /// [`sysinfo::SysMemory::still_low`] and [`Session::memory_budget`]). + pub memory_low: bool, /// The keyframe drag in progress (`keys.move` with a merge key). pub key_move: Option, /// Reads the system's memory in the background for [`Session::memory_tick`]. @@ -376,6 +379,7 @@ impl Default for Session { library: Default::default(), script_ui: scriptui::ScriptUi::default(), sys_memory: None, + memory_low: false, key_move: None, memory_watch: Default::default(), disk_watch: Default::default(), diff --git a/crates/engine/src/settings/prefs.rs b/crates/engine/src/settings/prefs.rs index 94c90c3a3..4743f5292 100644 --- a/crates/engine/src/settings/prefs.rs +++ b/crates/engine/src/settings/prefs.rs @@ -27,7 +27,10 @@ mod sections; pub use choices::{APPEARANCE_MODES, DARK_THEMES, LANGUAGES, LIGHT_THEMES, THEMES, WINDOW_GRAPHICS, theme_family}; pub use dialog::{Item, Kind, Page, pages, pages_json, todo_keys}; pub use document::{migrate, page_id, section_key}; -pub use queries::{CacheBudgets, WEB_LAYER_CACHE, WEB_MEDIA_CACHE, mask_color}; +pub use queries::{ + CacheCaps, FALLBACK_MEMORY_BUDGET, MEMORY_HEADROOM_PERCENT, MIN_MEMORY_BUDGET, MemoryBudget, WEB_LAYER_CACHE, WEB_MEDIA_CACHE, WEB_MEMORY_BUDGET, + WEB_PREVIEW_CACHE, mask_color, +}; pub use recent::{MAX_RECENT_SCRIPTS, RecentScript}; pub use sections::{ Appearance, Audio, AutoSave, CompPreset, CompositionPrefs, Disk, Export, FacePrefs, General, Grids, Import, LabelPref, Memory, Previews, ProjectPrefs, @@ -36,7 +39,7 @@ pub use sections::{ use sections::{Extra, default_labels, lenient_comp_presets}; /// Current settings layout version. -pub const PREFS_VERSION: u32 = 4; +pub const PREFS_VERSION: u32 = 5; /// Settings file name in the config store. pub const PREFS_FILE: &str = "prefs.json"; diff --git a/crates/engine/src/settings/prefs/dialog.rs b/crates/engine/src/settings/prefs/dialog.rs index 00e7a057a..92dc8fa36 100644 --- a/crates/engine/src/settings/prefs/dialog.rs +++ b/crates/engine/src/settings/prefs/dialog.rs @@ -47,6 +47,9 @@ pub enum Item { }, /// Read-only information line. Note(&'static str), + /// Memory & CPU: how much RAM EffectCraft's caches may use, of the installed RAM + /// (`Prefs::memory_budget`, `Session::memory_budget`). + MemoryUse, /// The browser's storage manager (web app only, `storage.*`): usage and quota, persistent /// storage, Clear buttons. Not shown where there is no browser storage. BrowserStorage, @@ -310,10 +313,7 @@ pub fn pages() -> Vec { title: "Memory & CPU", items: vec![ s("memory.ramReservedGb", "RAM Reserved for Other Applications", Kind::Int(0, 1024, "GB"), true), - Section("Cache Budgets"), - s("memory.layerCacheMb", "Layer Cache", Kind::Int(64, 1 << 20, "MB"), true), - s("memory.mediaCacheMb", "Footage Frame Cache", Kind::Int(64, 1 << 20, "MB"), true), - s("memory.previewCacheMb", "Preview (RAM) Cache", Kind::Int(64, 1 << 20, "MB"), true), + MemoryUse, s("memory.reduceCacheWhenLow", "Reduce Cache Size When System Is Low on Memory", B, true), ], }, @@ -441,6 +441,7 @@ pub fn pages_json() -> Value { Item::Labels => json!({"labels": "labels.N.name / labels.N.color (N = 0..15)"}), Item::AudioDevices { key } => json!({"type": "device", "key": key, "label": "Default Output"}), Item::Note(t) => json!({"note": t}), + Item::MemoryUse => json!({"memoryUse": "help.systemInfo → caches.budget / caches.inUse (what EffectCraft's caches may use, and use now)"}), Item::BrowserStorage => json!({"browserStorage": "storage.info / storage.persist / storage.clear (web app)"}), Item::PluginLibrary => json!({"pluginLibrary": "library.list / library.refresh / library.install / library.update / library.uninstall"}), Item::Models(task) => { diff --git a/crates/engine/src/settings/prefs/document.rs b/crates/engine/src/settings/prefs/document.rs index 873e6fe33..32e7126ec 100644 --- a/crates/engine/src/settings/prefs/document.rs +++ b/crates/engine/src/settings/prefs/document.rs @@ -11,6 +11,9 @@ use super::{Appearance, General, Map, PREFS_VERSION, Prefs, Startup, Value, json /// old default becomes the new one (a size the user chose is kept). /// - version 3 and earlier could still hold `createSplitLayersAbove: false`, the default before /// #2083 (every save kept it); it becomes After Effects' default, above (#2545). +/// - version 4 and earlier had the Layer Cache, Footage Frame Cache and Preview (RAM) Cache +/// budgets (`memory.layerCacheMb` / `mediaCacheMb` / `previewCacheMb`); they are dropped: the +/// caches share one budget (#3298). pub fn migrate(mut v: Value) -> Value { let Some(obj) = v.as_object_mut() else { return json!({}) }; let version = obj.get("version").and_then(Value::as_u64).unwrap_or(0); @@ -50,6 +53,14 @@ pub fn migrate(mut v: Value) -> Value { { g.insert("createSplitLayersAbove".into(), json!(true)); } + // Version 4 and earlier had three cache budgets (#3298): the caches now share one budget + // from RAM Reserved for Other Applications. The old keys are removed whatever the version + // says, not kept as unknown keys (the page keeps those and would write them back forever). + if let Some(m) = obj.get_mut("memory").and_then(Value::as_object_mut) { + for key in ["layerCacheMb", "mediaCacheMb", "previewCacheMb"] { + m.remove(key); + } + } // Settings saved before appearance modes had one theme: keep showing it, as a fixed Dark or // Light mode, instead of switching established users to Auto. if let Some(a) = obj.get_mut("appearance").and_then(Value::as_object_mut) @@ -155,10 +166,6 @@ impl Prefs { l.color = d.color.clone(); } } - let m = &mut self.memory; - m.layer_cache_mb = m.layer_cache_mb.clamp(64, 1 << 20); - m.media_cache_mb = m.media_cache_mb.clamp(64, 1 << 20); - m.preview_cache_mb = m.preview_cache_mb.clamp(64, 1 << 20); self.grids.grid_spacing = self.grids.grid_spacing.clamp(1.0, 10_000.0); let n = self.general.recent_items as usize; self.recent_projects.truncate(n); diff --git a/crates/engine/src/settings/prefs/queries.rs b/crates/engine/src/settings/prefs/queries.rs index 74be30f8f..e48518906 100644 --- a/crates/engine/src/settings/prefs/queries.rs +++ b/crates/engine/src/settings/prefs/queries.rs @@ -20,19 +20,6 @@ impl Prefs { Label::from_name(s).or_else(|| Label::ALL.iter().skip(1).copied().find(|l| self.label_name(*l).eq_ignore_ascii_case(s))) } - /// Layer / media / preview cache budgets in bytes. - pub fn layer_cache_bytes(&self) -> usize { - (self.memory.layer_cache_mb as usize) << 20 - } - - pub fn media_cache_bytes(&self) -> usize { - (self.memory.media_cache_mb as usize) << 20 - } - - pub fn preview_cache_bytes(&self) -> usize { - (self.memory.preview_cache_mb as usize) << 20 - } - /// The adaptive resolution limit as a render-scale floor (1/2 → 0.5). pub fn adaptive_limit(&self) -> f64 { match self.previews.adaptive_resolution_limit.as_str() { @@ -42,40 +29,29 @@ impl Prefs { } } - /// The Memory & CPU cache budgets in effect: the configured layer / footage / preview - /// budgets, scaled down so together they leave **RAM Reserved for Other Applications** free, - /// and halved while the system is low on memory when **Reduce Cache Size When System Is Low - /// on Memory** is on. `mem` = the system's memory (`None` = unknown: configured budgets). - /// In the browser the layer and footage caches also stay within [`WEB_LAYER_CACHE`] and - /// [`WEB_MEDIA_CACHE`]. - pub fn cache_budgets(&self, mem: Option) -> CacheBudgets { - self.cache_budgets_in(mem, cfg!(target_arch = "wasm32")) - } - - /// [`Prefs::cache_budgets`], in the browser's memory (`web`) or not. - pub(crate) fn cache_budgets_in(&self, mem: Option, web: bool) -> CacheBudgets { - let (mut l, mut m, mut p) = (self.layer_cache_bytes() as u64, self.media_cache_bytes() as u64, self.preview_cache_bytes() as u64); - if web { - (l, m) = (l.min(WEB_LAYER_CACHE), m.min(WEB_MEDIA_CACHE)); + /// The memory EffectCraft's caches share (Settings ▸ Memory & CPU): the layer cache, the + /// footage frames, the decoded OpenEXR planes and the RAM preview frames together, kept within + /// it by the memory accountant (`effectcraft_render::memory::Accountant`), which decides + /// what each holds. It is [`MEMORY_HEADROOM_PERCENT`] of the installed RAM left after **RAM + /// Reserved for Other Applications** (the rest is room for EffectCraft's own working memory), + /// halved while the system is low on memory (`low`) when **Reduce Cache Size When System Is + /// Low on Memory** is on, and never below [`MIN_MEMORY_BUDGET`]. `mem` = the system's memory + /// (`None` = unknown: [`FALLBACK_MEMORY_BUDGET`], in the browser [`WEB_MEMORY_BUDGET`]). + pub fn memory_budget(&self, mem: Option, low: bool) -> MemoryBudget { + let installed = mem.map(|m| m.total).filter(|t| *t > 0); + let reserved = u64::from(self.memory.ram_reserved_gb) << 30; + let mut bytes = match installed { + // u128: `room × percent` cannot overflow. + Some(total) => u64::try_from(u128::from(total.saturating_sub(reserved)) * u128::from(MEMORY_HEADROOM_PERCENT) / 100).unwrap_or(u64::MAX), + None if cfg!(target_arch = "wasm32") => WEB_MEMORY_BUDGET, + None => FALLBACK_MEMORY_BUDGET, + }; + let reduced = installed.is_some() && low && self.memory.reduce_cache_when_low; + if reduced { + bytes /= 2; } - let floor = 64u64 << 20; - let mut capped = false; - let mut reduced = false; - if let Some(mem) = mem.filter(|m| m.total > 0) { - let reserved = (self.memory.ram_reserved_gb as u64) << 30; - let cap = mem.total.saturating_sub(reserved).max(3 * floor); - let sum = l + m + p; - if sum > cap { - let k = cap as f64 / sum as f64; - (l, m, p) = ((l as f64 * k) as u64, (m as f64 * k) as u64, (p as f64 * k) as u64); - capped = true; - } - if self.memory.reduce_cache_when_low && mem.is_low() { - (l, m, p) = (l / 2, m / 2, p / 2); - reduced = true; - } - } - CacheBudgets { layer: l.max(floor) as usize, media: m.max(floor) as usize, preview: p.max(floor) as usize, capped, reduced } + let bytes = bytes.max(MIN_MEMORY_BUDGET); + MemoryBudget { bytes: usize::try_from(bytes).unwrap_or(usize::MAX), installed, reduced } } /// Composition ▸ Motion Path: the layer-time span of a motion path to draw around `now` @@ -175,27 +151,70 @@ impl Prefs { } } -/// The most the layer cache holds in the browser. A wasm32 page addresses at most 4 GiB, which -/// these caches share with the app and with the decoders' own decoded frames (FilmCraft keeps -/// up to 384 MB per open movie): with the desktop's 1 GB defaults, eight stacked 1080p clips ran -/// the page out of memory a few seconds into playback. +/// The most the layer cache holds in the browser, inside the common budget. A wasm32 page +/// addresses at most 4 GiB, which these caches share with the app and with the decoders' own +/// decoded frames (FilmCraft keeps up to 384 MB per open movie): with 1 GB of layers, eight +/// stacked 1080p clips ran the page out of memory a few seconds into playback. On the desktop +/// no cache has a limit of its own. pub const WEB_LAYER_CACHE: u64 = 512 << 20; /// The most the footage frame cache holds in the browser (see [`WEB_LAYER_CACHE`]). pub const WEB_MEDIA_CACHE: u64 = 256 << 20; -/// Effective cache budgets in bytes (see [`Prefs::cache_budgets`]). +/// The part (percent) of the installed RAM left after **RAM Reserved for Other Applications** +/// that EffectCraft's caches share ([`Prefs::memory_budget`]). The rest is headroom for +/// EffectCraft's own working memory (decoders, render buffers, undo, GPU staging, the interface), +/// so that does not eat into the memory reserved for other applications. +pub const MEMORY_HEADROOM_PERCENT: u64 = 80; + +/// The caches' budget while the installed RAM is unknown (the browser, a sandbox, or the desktop +/// before its first background reading): 5 GB, what the three fixed cache budgets added up to +/// before they became one. +pub const FALLBACK_MEMORY_BUDGET: u64 = 5 << 30; + +/// The caches' budget in the browser, where the installed RAM is unknown and a page addresses at +/// most 4 GiB, shared with the app and the decoders' own frames: 3.5 GB, a little more than the +/// RAM preview's 3 GB there, leaving at least half a gigabyte of the page to the rest. (The +/// separate budgets there added up to 4.25 GB before they became one, more than a page can +/// spare.) +pub const WEB_MEMORY_BUDGET: u64 = 3584 << 20; + +/// The most the RAM preview frames hold in the browser, inside the common budget (as before). +pub const WEB_PREVIEW_CACHE: u64 = 3 << 30; + +/// The least the caches share, however little RAM is left after the reserve: 192 MB, three times +/// the smallest budget one cache had before they became one. +pub const MIN_MEMORY_BUDGET: u64 = 192 << 20; + +/// The memory EffectCraft's caches share (see [`Prefs::memory_budget`]). #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)] -pub struct CacheBudgets { - pub layer: usize, - pub media: usize, - pub preview: usize, - /// Scaled down to leave the reserved RAM free. - pub capped: bool, +pub struct MemoryBudget { + /// The budget in bytes. + pub bytes: usize, + /// The installed RAM it was taken from (`None`: unknown, the fallback). + pub installed: Option, /// Halved because the system is low on memory. pub reduced: bool, } +impl MemoryBudget { + /// What each big cache may hold by itself within the budget: on the desktop all of it (no + /// cache has a share of its own, the memory accountant decides what is kept), in the browser + /// (`web`) also [`WEB_LAYER_CACHE`], [`WEB_MEDIA_CACHE`] and [`WEB_PREVIEW_CACHE`]. + pub fn caps(&self, web: bool) -> CacheCaps { + let cap = |c: u64| if web { usize::try_from(c).unwrap_or(usize::MAX).min(self.bytes) } else { self.bytes }; + CacheCaps { layer: cap(WEB_LAYER_CACHE), footage: cap(WEB_MEDIA_CACHE), preview: cap(WEB_PREVIEW_CACHE) } + } +} + +/// The most each big cache holds by itself ([`MemoryBudget::caps`]), in bytes. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct CacheCaps { + pub layer: usize, + pub footage: usize, + pub preview: usize, +} + /// Colour of the `k`-th mask of a layer: Appearance ▸ Cycle Mask Colors cycles through the mask /// colours, otherwise every new mask takes the first one. pub fn mask_color(cycle: bool, k: usize) -> [u8; 3] { diff --git a/crates/engine/src/settings/prefs/sections.rs b/crates/engine/src/settings/prefs/sections.rs index 0d31702ee..cb1d1bfb4 100644 --- a/crates/engine/src/settings/prefs/sections.rs +++ b/crates/engine/src/settings/prefs/sections.rs @@ -201,13 +201,9 @@ page!(Disk { }); page!(Memory { + /// RAM Reserved for Other Applications (GB): EffectCraft's caches share most of the rest + /// (see [`crate::prefs::Prefs::memory_budget`]). ram_reserved_gb: u32 = 6, - /// Processed-layer cache budget (MB). - layer_cache_mb: u32 = 1024, - /// Decoded footage frame cache budget (MB). - media_cache_mb: u32 = 1024, - /// RAM preview (viewer frame) cache budget (MB). - preview_cache_mb: u32 = 3072, reduce_cache_when_low: bool = true, }); diff --git a/crates/engine/src/settings/session_settings.rs b/crates/engine/src/settings/session_settings.rs index 77010a8cf..bf4acbde3 100644 --- a/crates/engine/src/settings/session_settings.rs +++ b/crates/engine/src/settings/session_settings.rs @@ -108,30 +108,48 @@ impl Session { self.cache_folder(&self.prefs.disk.conformed_media_folder, "Conformed Audio") } - /// Apply the Memory & CPU cache budgets, limited by RAM Reserved for Other Applications and - /// reduced while the system is low on memory (see [`crate::prefs::Prefs::cache_budgets`]). + /// Apply the Memory & CPU budget ([`Session::memory_budget`]): one budget the big caches share + /// (the layer cache, the footage frames and decoded planes, the preview frames), kept by the + /// memory accountant, which frees the least valuable data first whichever cache holds it. No + /// cache has a limit of its own below that budget, except the browser's caps + /// ([`crate::prefs::MemoryBudget::caps`]; frontends apply the preview frames' cap). pub fn apply_cache_budgets(&mut self) { - let b = self.prefs.cache_budgets(self.sys_memory); - 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. + let b = self.memory_budget(); + let caps = b.caps(cfg!(target_arch = "wasm32")); + self.layer_cache.set_budget(caps.layer); + self.footage.set_cache_budget(caps.footage); 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.bytes); + } + + /// The memory the caches share for the last memory reading ([`Prefs::memory_budget`]), kept + /// reduced through a low-memory episode until the system has recovered + /// ([`crate::sysinfo::SysMemory::still_low`]), so the budget (and the toast) doesn't flip + /// with every reading. + pub fn memory_budget(&self) -> crate::prefs::MemoryBudget { + let low = self.sys_memory.is_some_and(|m| m.still_low(self.memory_low)); + self.prefs.memory_budget(self.sys_memory, low) } /// Take the latest reading of the system's memory and start the next one in the background /// (desktop frontends call this every few seconds; it never waits for the system), then - /// re-apply the cache budgets when the reading changes them. Returns the effective budgets. + /// re-apply the memory budget when the reading changes it. Returns the budget in effect. /// The disk cache's free disk space is read the same way (it keeps a reserve free). - pub fn memory_tick(&mut self) -> crate::prefs::CacheBudgets { + pub fn memory_tick(&mut self) -> crate::prefs::MemoryBudget { self.poll_disk_space(); - let before = self.prefs.cache_budgets(self.sys_memory); - self.sys_memory = self.memory_watch.latest().or(self.sys_memory); + let reading = self.memory_watch.latest(); self.memory_watch.poll(); - let after = self.prefs.cache_budgets(self.sys_memory); + self.take_memory_reading(reading) + } + + /// [`Session::memory_tick`] with the memory reading taken (`None`: no new one). + pub fn take_memory_reading(&mut self, reading: Option) -> crate::prefs::MemoryBudget { + let before = self.memory_budget(); + self.sys_memory = reading.or(self.sys_memory); + self.memory_low = self.sys_memory.is_some_and(|m| m.still_low(self.memory_low)); + let after = self.memory_budget(); if after != before { self.apply_cache_budgets(); // Frontends re-apply their preview cache budget. diff --git a/crates/engine/src/system/sysinfo.rs b/crates/engine/src/system/sysinfo.rs index 80b51b462..b86fbbce0 100644 --- a/crates/engine/src/system/sysinfo.rs +++ b/crates/engine/src/system/sysinfo.rs @@ -24,7 +24,20 @@ pub struct SysMemory { impl SysMemory { /// The system is low on memory: less than 10% (or less than 1 GB) is available. pub fn is_low(&self) -> bool { - self.total > 0 && (self.available < self.total / 10 || self.available < 1 << 30) + self.total > 0 && self.available < self.low_threshold() + } + + /// Below this much available memory the system is low: 10% of the total, at least 1 GB. + fn low_threshold(&self) -> u64 { + (self.total / 10).max(1 << 30) + } + + /// Still low on memory, given whether it was (`was_low`): a low episode ends only once twice + /// the low threshold is available again. Without that margin, halving the caches frees just + /// enough memory to end it, the budgets double again and fill it, and the system is low + /// again a reading later. + pub fn still_low(&self, was_low: bool) -> bool { + if was_low { self.total > 0 && self.available < self.low_threshold().saturating_mul(2) } else { self.is_low() } } } diff --git a/crates/engine/src/tests/m145.rs b/crates/engine/src/tests/m145.rs index 831a05ead..2bd32deb1 100644 --- a/crates/engine/src/tests/m145.rs +++ b/crates/engine/src/tests/m145.rs @@ -179,44 +179,127 @@ fn pick_whip_writes_match_names_without_compact_english() { // ---------------------------------------------------------------- pure setting helpers +/// #3298: one budget for the caches together, 80 % of the installed RAM left after RAM Reserved +/// for Other Applications (three fixed budgets of 5 GB in all, whatever the machine had, before). #[test] -fn cache_budgets_leave_reserved_ram_and_shrink_when_low() { +fn the_memory_budget_follows_the_installed_ram() { + use crate::prefs::{FALLBACK_MEMORY_BUDGET, MEMORY_HEADROOM_PERCENT, MIN_MEMORY_BUDGET}; use crate::sysinfo::SysMemory; + assert_eq!(MEMORY_HEADROOM_PERCENT, 80); let mut p = crate::prefs::Prefs::default(); - p.memory.layer_cache_mb = 4096; - p.memory.media_cache_mb = 4096; - p.memory.preview_cache_mb = 8192; - p.memory.ram_reserved_gb = 6; - let b = p.cache_budgets(None); - assert_eq!((b.layer >> 20, b.preview >> 20, b.capped), (4096, 8192, false)); - // 16 GB machine, 6 GB reserved: 16 GB of caches scaled into 10 GB. - let m = SysMemory { total: 16 << 30, available: 12 << 30 }; - let b = p.cache_budgets(Some(m)); - assert!(b.capped && !b.reduced); - assert!(((b.layer + b.media + b.preview) as i64 - (10i64 << 30)).abs() < 4, "{b:?}"); + assert_eq!(p.memory.ram_reserved_gb, 6); + let at = |gb: u64| Some(SysMemory { total: gb << 30, available: gb << 30 }); + for (gb, mb) in [(16u64, 8192u64), (64, 47_513), (128, 99_942)] { + let b = p.memory_budget(at(gb), false); + assert_eq!(b.bytes as u64 >> 20, mb, "{gb} GB: {b:?}"); + assert_eq!((b.installed, b.reduced), (Some(gb << 30), false)); + } + // Unknown RAM: the old three budgets' sum. + let b = p.memory_budget(None, false); + assert_eq!((b.bytes as u64, b.installed), (FALLBACK_MEMORY_BUDGET, None)); + assert_eq!(FALLBACK_MEMORY_BUDGET, 5 << 30); + // Less RAM than the reserve: the floor, for the caches together. + assert_eq!(p.memory_budget(at(4), false).bytes as u64, MIN_MEMORY_BUDGET); + // A larger reserve leaves less. + p.memory.ram_reserved_gb = 32; + assert_eq!(p.memory_budget(at(64), false).bytes as u64, (32u64 << 30) * 80 / 100); // Low on memory: halved; off: not. - let low = SysMemory { total: 16 << 30, available: 512 << 20 }; - let r = p.cache_budgets(Some(low)); - assert!(r.reduced && r.preview < b.preview / 2 + 2); + let b = p.memory_budget(at(64), true); + assert!(b.reduced && b.bytes as u64 == (32u64 << 30) * 80 / 200, "{b:?}"); p.memory.reduce_cache_when_low = false; - assert!(!p.cache_budgets(Some(low)).reduced); + assert!(!p.memory_budget(at(64), true).reduced); } -/// Eight stacked 1080p clips ran the browser build out of its 4 GiB of memory: the footage and -/// layer caches kept their desktop sizes there, beside the decoders' own frames. +/// Halving the caches frees memory: a low-memory episode ends only once twice the threshold is +/// available again, so the budget (and the toast) doesn't flip with every reading. #[test] -fn web_cache_budgets_leave_room_for_the_decoders() { - use crate::prefs::{WEB_LAYER_CACHE, WEB_MEDIA_CACHE}; - let mut p = crate::prefs::Prefs::default(); - p.memory.layer_cache_mb = 4096; - p.memory.media_cache_mb = 4096; - let web = p.cache_budgets_in(None, true); - assert_eq!((web.layer as u64, web.media as u64), (WEB_LAYER_CACHE, WEB_MEDIA_CACHE)); - assert_eq!(web.preview, p.cache_budgets_in(None, false).preview, "the RAM preview keeps its budget"); - // Smaller settings stay as they are; the desktop is unchanged. - p.memory.media_cache_mb = 128; - assert_eq!(p.cache_budgets_in(None, true).media, 128 << 20); - assert_eq!(p.cache_budgets_in(None, false).layer, 4096 << 20); +fn low_memory_budgets_hold_until_the_system_recovers() { + use crate::sysinfo::SysMemory; + // 16 GB: low below 1.6 GB available, recovered from 3.2 GB. + let at = |available: u64| SysMemory { total: 16 << 30, available }; + assert!(at(1 << 30).still_low(false)); + assert!(!at(2 << 30).still_low(false)); + assert!(at(2 << 30).still_low(true), "not recovered yet"); + assert!(!at(4 << 30).still_low(true)); + let mut s = Session::default(); + assert!(!s.memory_budget().reduced, "no reading: nothing reduced"); + s.sys_memory = Some(at(2 << 30)); + assert!(!s.memory_budget().reduced); + s.memory_low = true; + assert!(s.memory_budget().reduced, "the episode holds"); + s.sys_memory = Some(at(4 << 30)); + assert!(!s.memory_budget().reduced, "recovered"); +} + +/// The memory readings over a low-memory episode: the budget is halved once, stays halved while +/// the system has not recovered (a reading between the low line and twice it), and the toast +/// shows once; recovered, the budget is whole again, and the next episode toasts again. +#[test] +fn a_low_memory_episode_toasts_once_and_reapplies_the_budget() { + use crate::sysinfo::SysMemory; + let at = |available: u64| Some(SysMemory { total: 64 << 30, available }); + let mut s = Session::default(); + s.drain_events(); + let toasts = |s: &mut Session| s.drain_events().into_iter().filter(|e| matches!(e, crate::Event::Toast { .. })).count(); + let whole = s.take_memory_reading(at(40 << 30)); + assert!(!whole.reduced); + assert_eq!(s.layer_cache.budget(), whole.bytes, "applied"); + let revision = s.prefs_revision; + // Low (below 6.4 GB): halved, re-applied, one toast. + let low = s.take_memory_reading(at(4 << 30)); + assert!(low.reduced && low.bytes == whole.bytes / 2, "{low:?}"); + assert_eq!(s.layer_cache.budget(), low.bytes); + assert!(s.prefs_revision > revision, "frontends re-apply theirs"); + assert_eq!(toasts(&mut s), 1); + // Between the low line and twice it, then low again: still the same episode. + for available in [8u64 << 30, 3 << 30, 10 << 30] { + assert!(s.take_memory_reading(at(available)).reduced, "{available}"); + } + assert_eq!(toasts(&mut s), 0, "one toast per episode"); + // Recovered (13 GB, over twice 6.4 GB): whole again; a new episode toasts again. + assert_eq!(s.take_memory_reading(at(13 << 30)), whole); + assert_eq!(s.layer_cache.budget(), whole.bytes); + assert!(s.take_memory_reading(at(2 << 30)).reduced); + assert_eq!(toasts(&mut s), 1); +} + +/// #3298: settings saved with the three cache budgets (version 4, or a later file that still +/// has them) keep RAM Reserved for Other Applications and lose the budgets: they are not kept +/// as unknown keys and written back. +#[test] +fn the_old_cache_budgets_are_dropped_from_saved_settings() { + use crate::prefs::Prefs; + for version in [4, 5] { + let old = format!( + r#"{{"version": {version}, "memory": {{"ramReservedGb": 12, "layerCacheMb": 2048, "mediaCacheMb": 4096, "previewCacheMb": 8192, "reduceCacheWhenLow": false}}}}"# + ); + let p = Prefs::from_json(&old); + assert_eq!((p.memory.ram_reserved_gb, p.memory.reduce_cache_when_low), (12, false), "v{version}"); + let saved: Value = serde_json::from_str(&p.to_json()).unwrap(); + let memory = saved["memory"].as_object().unwrap(); + for key in ["layerCacheMb", "mediaCacheMb", "previewCacheMb"] { + assert!(!memory.contains_key(key), "v{version}: {key} dropped"); + } + assert_eq!(memory["ramReservedGb"], json!(12)); + } +} + +/// What each cache may hold by itself: on the desktop all of the budget (the accountant +/// decides), in the browser its cap, as before one budget: the RAM preview keeps its 3 GB and the +/// layer and footage caches their 512 / 256 MB (#3298 review: the preview may not take the whole +/// web budget), all within the web budget. +#[test] +fn caches_have_no_limit_of_their_own_below_the_budget() { + use crate::prefs::{MemoryBudget, WEB_LAYER_CACHE, WEB_MEDIA_CACHE, WEB_MEMORY_BUDGET, WEB_PREVIEW_CACHE}; + let b = |bytes: usize| MemoryBudget { bytes, installed: None, reduced: false }; + let desktop = b(40 << 30).caps(false); + assert_eq!((desktop.layer, desktop.footage, desktop.preview), (40 << 30, 40 << 30, 40 << 30)); + let web = b(WEB_MEMORY_BUDGET as usize).caps(true); + assert_eq!((web.layer as u64, web.footage as u64, web.preview as u64), (WEB_LAYER_CACHE, WEB_MEDIA_CACHE, WEB_PREVIEW_CACHE)); + assert_eq!(WEB_PREVIEW_CACHE, 3 << 30, "the RAM preview keeps its budget"); + const { assert!(WEB_MEMORY_BUDGET <= 3584 << 20 && WEB_MEMORY_BUDGET >= WEB_PREVIEW_CACHE) }; + let small = b(100 << 20).caps(true); + assert_eq!((small.layer, small.footage, small.preview), (100 << 20, 100 << 20, 100 << 20)); } #[test] diff --git a/crates/engine/src/tests/settings.rs b/crates/engine/src/tests/settings.rs index 7d65061ea..696626b92 100644 --- a/crates/engine/src/tests/settings.rs +++ b/crates/engine/src/tests/settings.rs @@ -76,8 +76,8 @@ fn prefs_migrate_old_layout_and_keep_unknown_keys() { assert_eq!(p.labels[1].name, "Sun"); assert_eq!(p.labels[2].name, "Aqua"); assert_eq!(p.labels.len(), 16); - // A bad value falls back to that page's defaults; other pages survive. - assert_eq!(p.memory.layer_cache_mb, 1024); + // The old cache budgets (#3298) are dropped, not kept as unknown keys. + assert_eq!(p.memory, crate::prefs::Memory::default()); // Unknown keys (a newer version's) are kept and written back. let again: serde_json::Value = serde_json::from_str(&p.to_json()).unwrap(); assert_eq!(again["futureSetting"], json!({"x": 1})); @@ -351,10 +351,16 @@ fn renamed_labels_show_in_the_label_menu_and_apply_by_name() { } #[test] +/// #3298: the caches share one budget from RAM Reserved for Other Applications; a workload of +/// footage only may use nearly all of it (it was held to the 1 GB Footage Frame Cache). fn cache_budgets_are_applied() { let mut s = Session::default(); - s.execute("prefs.set", json!({"key": "memory.layerCacheMb", "value": 128})).unwrap(); - assert_eq!(s.layer_cache.budget(), 128 << 20); + assert!(s.layer_cache.budget() > 0); + s.sys_memory = Some(crate::sysinfo::SysMemory { total: 64 << 30, available: 60 << 30 }); + s.execute("prefs.set", json!({"key": "memory.ramReservedGb", "value": 6})).unwrap(); + let budget = (58usize << 30) * 80 / 100; + assert_eq!(s.memory_budget().bytes, budget); + assert_eq!(s.layer_cache.budget(), budget, "no layer cache budget of its own"); struct Src(std::sync::Mutex); impl effectcraft_render::FootageSource for Src { fn frame(&self, _: effectcraft_project::ItemId, _: &effectcraft_project::Footage, _: effectcraft_time::Tick) -> Option> { @@ -366,8 +372,9 @@ fn cache_budgets_are_applied() { } let src = Arc::new(Src(Default::default())); s.footage = src.clone(); - s.execute("prefs.set", json!({"key": "memory.mediaCacheMb", "value": 256})).unwrap(); - assert_eq!(*src.0.lock().unwrap(), 256 << 20); + s.execute("prefs.set", json!({"key": "memory.ramReservedGb", "value": 32})).unwrap(); + assert_eq!(*src.0.lock().unwrap(), (32usize << 30) * 80 / 100, "the footage frames may use the whole budget"); + assert!(s.execute("prefs.set", json!({"key": "memory.mediaCacheMb", "value": 256})).is_err(), "no such setting any more"); } #[test] diff --git a/crates/host/src/lib.rs b/crates/host/src/lib.rs index 74fa8348d..19d611d41 100644 --- a/crates/host/src/lib.rs +++ b/crates/host/src/lib.rs @@ -101,6 +101,10 @@ pub fn session() -> Session { ..Default::default() }; effectcraft_script::install(&mut s); + // The caches' common budget from the start (#3298): the CLI and the MCP server never load + // the settings, and the caches have no limits of their own below it. Without a memory + // reading yet, the budget for unknown RAM. + s.apply_cache_budgets(); s } diff --git a/crates/host/tests/it/main.rs b/crates/host/tests/it/main.rs index 168ec127a..f5ab6827c 100644 --- a/crates/host/tests/it/main.rs +++ b/crates/host/tests/it/main.rs @@ -7,6 +7,7 @@ mod exr_layers; mod footage_reload; mod gif_animates; mod library; +mod memory_budget; mod pick_whip; mod plugins; mod relink; diff --git a/crates/host/tests/it/memory_budget.rs b/crates/host/tests/it/memory_budget.rs new file mode 100644 index 000000000..1a0ddc8df --- /dev/null +++ b/crates/host/tests/it/memory_budget.rs @@ -0,0 +1,14 @@ +//! #3298: every host session (the desktop app, the CLI, the MCP server) starts with the caches' +//! common budget: the caches have no limits of their own below it, so an unset budget would let +//! them grow until memory runs out. + +use effectcraft_engine::render::memory::Accountant; + +#[test] +fn a_host_session_bounds_the_caches() { + let s = effectcraft_host::session(); + let budget = Accountant::global().budget(); + assert!(budget < usize::MAX, "a finite budget"); + assert_eq!(budget, s.memory_budget().bytes); + assert!(s.layer_cache.budget() <= budget); +} diff --git a/crates/media/src/exr_channels.rs b/crates/media/src/exr_channels.rs index 42c3e47eb..276b15df5 100644 --- a/crates/media/src/exr_channels.rs +++ b/crates/media/src/exr_channels.rs @@ -77,14 +77,16 @@ 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 -/// 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; +/// How many bytes of decoded planes the plane store keeps by itself: no limit on the desktop +/// (#3298: the planes share one budget with the other big caches, and the memory accountant +/// frees the least valuable data first, whichever cache holds it), 512 MB in the browser, whose +/// page addresses at most 4 GiB. Files still in use are never dropped (planes an effect holds, +/// pictures waiting for their effects, decodes in progress), and the newest file is kept +/// whatever its size (so a frame's picture and its effects share its decode). +#[cfg(not(target_arch = "wasm32"))] +pub const CACHE_BUDGET: usize = usize::MAX; +#[cfg(target_arch = "wasm32")] +pub const CACHE_BUDGET: 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..d487795cc 100644 --- a/crates/media/src/exr_planes.rs +++ b/crates/media/src/exr_planes.rs @@ -15,9 +15,11 @@ //! a parallel loop run other jobs, which could wait on that very decode): it decodes a copy. //! - **Nothing made before a Reload is kept or served** ([`forget`]): a decode that started //! before the file was forgotten keeps nothing, and the requests waiting for it ask again. -//! - Bounded by bytes ([`CACHE_BUDGET`]), least recently used out first; entries in use (planes -//! an effect holds, a decode in progress, the last pictures' planes kept for their effects, -//! [`HELD_FILES`]) are never dropped, and the newest stays even when it alone is over. +//! - Kept by the memory accountant (#3298): least recently used offered first, each entry worth +//! what decoding it took. In the browser, and while the accountant has no budget, also bounded +//! by bytes ([`CACHE_BUDGET`], [`UNMANAGED_BUDGET`]); there the newest entry stays even when it +//! alone is over. Entries in use (planes an effect holds, a decode in progress, the last +//! pictures' planes kept for their effects, [`HELD_FILES`]) are never dropped, by either. use std::collections::{BTreeSet, VecDeque}; use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError}; @@ -34,6 +36,10 @@ use crate::exr_channels::{Rgb, planes_picture}; use crate::exr_index::{self, ExrIndex, IndexError}; use crate::exr_read::{ExrSource, Opened, ReadError, ReadRequest, read_planes}; +/// What the store holds at most while the memory accountant has no budget (a session that never +/// set one): the planes' own budget before they shared one with the other caches. +const UNMANAGED_BUDGET: usize = 1 << 30; + /// At most this many entries are kept (unreadable files hold no bytes). const CACHE_FILES: usize = 64; @@ -169,6 +175,9 @@ struct Flight { /// thread that claims it decodes under them (its own being called off doesn't call off the /// asker's decode). hints: filmcraft_media::cancel::Context, + /// When the decode was asked for ([`effectcraft_render::memory::tick`]): what it took is the + /// planes' cost to the memory accountant. + asked: u64, } /// [`Flight::phase`] values. @@ -204,6 +213,7 @@ impl Flight { base, since, hints: filmcraft_media::cancel::Context::current(), + asked: effectcraft_render::memory::tick(), }) } @@ -265,6 +275,10 @@ struct Entry { bytes: usize, /// When it was last asked for or finished decoding (a counter). used: u64, + /// The same, for the memory accountant ([`effectcraft_render::memory::tick`]). + stamp: u64, + /// What decoding its planes took, in microseconds (0: not decoded yet). + cost_us: u64, } impl Entry { @@ -294,9 +308,9 @@ struct Owning<'a> { impl Owning<'_> { fn end(&mut self, outcome: Result, Refused>) -> Result, Refused> { self.ended = true; - let outcome = self.store.finish(self.path, self.flight, outcome); + let (outcome, grown) = self.store.finish(self.path, self.flight, outcome); self.flight.end(outcome.clone()); - effectcraft_render::memory::Accountant::global().note_growth(); + effectcraft_render::memory::Accountant::global().note_growth(grown); outcome } } @@ -479,12 +493,13 @@ impl PlaneStore { let at = match st.entries.iter().position(|e| e.path == path) { Some(i) => i, None => { - st.entries.push(Entry { path: path.to_string(), batch: None, flight: None, failed: vec![], bytes: 0, used: clock }); + st.entries.push(Entry { path: path.to_string(), batch: None, flight: None, failed: vec![], bytes: 0, used: clock, stamp: 0, cost_us: 0 }); st.entries.len() - 1 } }; let Some(e) = st.entries.get_mut(at) else { return Step::Done(Err(Refused::Unreadable)) }; e.used = clock; + e.stamp = effectcraft_render::memory::tick(); // What the store holds first: a failure of a larger request doesn't hide it. if let Some(b) = e.batch.clone().filter(|b| b.covers(index, want)) { Self::hold(&mut st, path, &b, by); @@ -572,22 +587,30 @@ impl PlaneStore { /// Store the outcome of `flight`'s decode in its entry, unless the file was forgotten /// 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> { + /// Also the bytes the store grew by (for the memory accountant). + fn finish(&self, path: &str, flight: &Arc, outcome: Result, Refused>) -> (Result, Refused>, usize) { let mut st = self.lock(); st.clock += 1; let clock = st.clock; 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 { + let outcome = match outcome { Err(e @ (Refused::Cancelled | Refused::Stopped)) => Err(e), _ => Err(Refused::Forgotten), }; + return (outcome, 0); }; e.flight = None; e.used = clock; + e.stamp = effectcraft_render::memory::tick(); + let had = e.bytes; match &outcome { Ok(b) => { e.bytes = b.bytes(); e.batch = Some(b.clone()); + let took = e.stamp.saturating_sub(flight.asked).max(1); + // A decode adding channels to what was held: both decodes made the planes; one + // that read them all anew replaces what was held, and its cost. + e.cost_us = if flight.base.is_some() { e.cost_us.saturating_add(took) } else { took }; } Err(why @ (Refused::Unreadable | Refused::TooLarge(_))) => { let want = flight.state().want.clone(); @@ -600,11 +623,20 @@ impl PlaneStore { Err(Refused::Changed) => { e.batch = None; e.bytes = 0; + e.cost_us = 0; } Err(Refused::Cancelled | Refused::Forgotten | Refused::Stopped) => {} } - Self::drop_oldest(&mut st, self.budget, CACHE_FILES, Some(path)); - outcome + let grown = st.entries.iter().find(|e| e.path == path).map_or(0, |e| e.bytes.saturating_sub(had)); + Self::drop_oldest(&mut st, self.own_budget(), CACHE_FILES, Some(path)); + (outcome, grown) + } + + /// The most it holds by itself: its budget ([`CACHE_BUDGET`]), and while nothing keeps the + /// caches within a common budget (the memory accountant has none, #3298 review) + /// [`UNMANAGED_BUDGET`]. + fn own_budget(&self) -> usize { + if effectcraft_render::memory::Accountant::global().budget() == usize::MAX { self.budget.min(UNMANAGED_BUDGET) } else { self.budget } } /// Drop least recently used entries until the rest take at most `bytes` in at most `files` @@ -617,20 +649,41 @@ impl PlaneStore { } // (an entry holding no bytes only goes for the count) let over_count = st.entries.len() > files; - let held = &st.held; - let oldest = st - .entries - .iter() - .enumerate() - .filter(|(_, e)| Some(e.path.as_str()) != newest && !e.in_use() && (e.bytes > 0 || over_count)) - .filter(|(_, e)| !e.batch.as_ref().is_some_and(|b| held.iter().any(|(_, h)| Arc::ptr_eq(h, b)))) - .min_by_key(|(_, e)| e.used) - .map(|(i, _)| i); - let Some(i) = oldest else { break }; + let Some(i) = Self::oldest_droppable(st, newest, over_count) else { break }; st.entries.remove(i); } } + /// The least recently used entry that may be dropped: not `newest`, not in use, not a + /// picture's planes held for its effects, and holding bytes (unless `empty_too`). + fn oldest_droppable(st: &State, newest: Option<&str>, empty_too: bool) -> Option { + let held = &st.held; + st.entries + .iter() + .enumerate() + .filter(|(_, e)| Some(e.path.as_str()) != newest && !e.in_use() && (e.bytes > 0 || empty_too)) + .filter(|(_, e)| !e.batch.as_ref().is_some_and(|b| held.iter().any(|(_, h)| Arc::ptr_eq(h, b)))) + .min_by_key(|(_, e)| e.used) + .map(|(i, _)| i) + } + + /// The entry the memory accountant may drop next ([`PlaneStore::oldest_droppable`]), with + /// what decoding it took (a typical cost when not timed). + fn oldest_candidate(&self) -> Option { + let st = self.lock(); + let e = st.entries.get(Self::oldest_droppable(&st, None, false)?)?; + let cost_us = + if e.cost_us > 0 { e.cost_us } else { effectcraft_render::memory::default_cost(e.bytes, effectcraft_render::memory::DEFAULT_PLANE_US_PER_MB) }; + Some(effectcraft_render::memory::Candidate { used: e.stamp, bytes: e.bytes, cost_us }) + } + + /// Drop [`PlaneStore::oldest_candidate`]; the bytes freed. + fn drop_candidate(&self) -> usize { + let mut st = self.lock(); + let Some(i) = Self::oldest_droppable(&st, None, false) else { return 0 }; + if i < st.entries.len() { st.entries.remove(i).bytes } else { 0 } + } + /// Wait for the outcome of another request's decode (`flight`). A thread running a store /// decode doesn't wait (see the module notes); the time limit is a safety net. #[cfg(not(target_arch = "wasm32"))] @@ -673,12 +726,6 @@ impl PlaneStore { st.held.retain(|(p, _)| p != path); } - /// Drop least recently used entries not in use until at most `bytes` are held (the memory - /// accountant, #3271). - fn trim_to(&self, bytes: usize) { - Self::drop_oldest(&mut self.lock(), bytes, usize::MAX, None); - } - /// Stop keeping the last pictures' planes for their effects. pub(crate) fn release_held(&self) { self.lock().held.clear(); @@ -966,8 +1013,8 @@ fn store() -> &'static PlaneStore { &STORE } -/// The shared plane store in the common memory budget: decoded planes the effects read, costly -/// to decode again, freed after the footage frames and before rendered layers. +/// The shared plane store in the common memory budget (#3271, #3298): decoded planes the effects +/// read, each file's worth what decoding it took, per byte. struct SharedStore; impl effectcraft_render::memory::MemoryHolder for SharedStore { @@ -977,11 +1024,11 @@ impl effectcraft_render::memory::MemoryHolder for SharedStore { fn bytes(&self) -> usize { STORE.lock().entries.iter().map(|e| e.bytes).fold(0, usize::saturating_add) } - fn priority(&self) -> u8 { - 1 + fn oldest(&self) -> Option { + STORE.oldest_candidate() } - fn trim_to(&self, bytes: usize) { - STORE.trim_to(bytes); + fn drop_oldest(&self) -> usize { + STORE.drop_candidate() } } diff --git a/crates/media/src/exr_planes/tests.rs b/crates/media/src/exr_planes/tests.rs index 4360db7a6..b50c0003a 100644 --- a/crates/media/src/exr_planes/tests.rs +++ b/crates/media/src/exr_planes/tests.rs @@ -1036,11 +1036,11 @@ fn the_plane_store_is_trimmed_by_the_accountant() { fn bytes(&self) -> usize { self.0.lock().entries.iter().map(|e| e.bytes).sum() } - fn priority(&self) -> u8 { - 1 + fn oldest(&self) -> Option { + self.0.oldest_candidate() } - fn trim_to(&self, bytes: usize) { - self.0.trim_to(bytes); + fn drop_oldest(&self) -> usize { + self.0.drop_candidate() } } let bytes: Arc<[u8]> = write(vec![part(None, &RGBA, (32, 32), Compression::ZIP1)], (32, 32)).into(); @@ -1262,7 +1262,7 @@ fn trimming_for_bytes_keeps_remembered_failures() { let index = crate::exr_index::get(p, &source).unwrap(); assert!(store.get(p, &source, &index, &all, Use::Other).is_ok()); } - store.trim_to(0); + while store.drop_candidate() > 0 {} assert_eq!(store.cached(), [path], "the planes went, the failure stays"); assert!(store.get(path, &broken, &index, &all, Use::Other).is_err()); assert_eq!(decodes_of(path).len(), 1, "not read again"); diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index f10127dcd..a3add1a4a 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -64,6 +64,10 @@ struct Entry { files: Arc, stamp: u64, bytes: usize, + /// When it was last used, for the memory accountant ([`effectcraft_render::memory::tick`]). + used: u64, + /// What decoding it took, in microseconds (a typical cost when not timed). + cost_us: u64, } /// Conformed audio files written by builds before this version are ignored (and written again): @@ -121,14 +125,22 @@ 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())) } /// Stores an image with its read files under `k` and evicts the least recently used entries until the cache fits `budget` bytes. fn insert(&mut self, k: Key, img: Arc, files: Arc, budget: usize) { + self.insert_took(k, img, files, budget, None); + } + /// [`Lru::insert`] with what decoding the frame took in microseconds (`None`: not timed, a + /// typical cost). + fn insert_took(&mut self, k: Key, img: Arc, files: Arc, budget: usize, took: Option) { let bytes = img.data.len() * std::mem::size_of::() + 64; + let cost_us = took.map_or_else(|| effectcraft_render::memory::default_cost(bytes, effectcraft_render::memory::DEFAULT_FRAME_US_PER_MB), |t| t.max(1)); self.clock += 1; - if let Some(old) = self.map.insert(k.clone(), Entry { img, files, stamp: self.clock, bytes }) { + let used = effectcraft_render::memory::tick(); + if let Some(old) = self.map.insert(k.clone(), Entry { img, files, stamp: self.clock, bytes, used, cost_us }) { self.order.remove(&old.stamp); self.bytes -= old.bytes; } @@ -142,6 +154,14 @@ impl Lru { self.bytes -= e.bytes; } } + /// The least recently used frame, if one may go (the newest frame always stays). + fn oldest(&self) -> Option<&Entry> { + if self.map.len() <= 1 { + return None; + } + let (_, k) = self.order.first_key_value()?; + self.map.get(k) + } /// Evict least-recently-used frames until within `budget` (the newest frame always stays). fn evict(&mut self, budget: usize) { while self.bytes > budget && self.map.len() > 1 { @@ -298,6 +318,11 @@ impl CacheState { } } + /// The bytes the frame cache and the whole stills beside it hold (the memory holder's). + fn held_bytes(&self) -> usize { + self.lru.bytes.saturating_add(self.recent_bytes()) + } + /// The bytes of the whole stills [`CacheState::recent`] holds. fn recent_bytes(&self) -> usize { self.recent.iter().map(|r| r.bytes).sum() @@ -526,6 +551,9 @@ struct Loc { /// picture that never comes. struct Owner<'a> { inner: &'a Inner, + /// When the decode started ([`effectcraft_render::memory::tick`]: what it took is the frame's + /// cost to the memory accountant). + started: u64, key: Key, flight: Arc, /// The picture is the one asked for (else a still's whole picture, resampled for the request). @@ -545,22 +573,25 @@ impl Drop for Owner<'_> { }); let budget = self.inner.budget(); let mut c = lock(&self.inner.cache); + let had = c.held_bytes(); if let (Ok(img), files) = &outcome && !c.stale(&self.key.path, self.generation) { if self.as_asked { - c.lru.insert(self.key.clone(), img.clone(), files.clone(), budget); + let took = effectcraft_render::memory::tick().saturating_sub(self.started); + c.lru.insert_took(self.key.clone(), img.clone(), files.clone(), budget, Some(took)); } else { c.keep_recent(self.key.clone(), img.clone(), files.clone(), budget); } } + let grown = c.held_bytes().saturating_sub(had); if c.inflight.get(&self.key).is_some_and(|f| Arc::ptr_eq(f, &self.flight)) { c.inflight.remove(&self.key); } drop(c); *lock(&self.flight.outcome) = Some(outcome); self.flight.ready.notify_all(); - effectcraft_render::memory::Accountant::global().note_growth(); + effectcraft_render::memory::Accountant::global().note_growth(grown); } } @@ -1206,7 +1237,8 @@ impl Inner { let flight = Arc::new(Flight { generation, ..Flight::default() }); c.inflight.insert(source.key.clone(), flight.clone()); drop(c); - let mut owner = Owner { inner: self, key: source.key.clone(), flight, as_asked, generation, outcome: None }; + let mut owner = + Owner { inner: self, started: effectcraft_render::memory::tick(), key: source.key.clone(), flight, as_asked, generation, outcome: None }; let (decoded, files) = self.run_decode(&source, footage); let (decoded, files) = (decoded.map(Arc::new), Arc::new(files)); reads::note_all(&files); @@ -1247,11 +1279,13 @@ impl Inner { /// since `generation`, see [`CacheState::stale`]), and return it. fn keep(&self, key: &Key, img: Arc, files: Arc, generation: u64) -> Arc { let mut c = lock(&self.cache); + let had = c.held_bytes(); if !c.stale(&key.path, generation) { c.lru.insert(key.clone(), img.clone(), files, self.budget()); } + let grown = c.held_bytes().saturating_sub(had); drop(c); - effectcraft_render::memory::Accountant::global().note_growth(); + effectcraft_render::memory::Accountant::global().note_growth(grown); img } @@ -1496,23 +1530,33 @@ impl Inner { } } -/// The decoded frames in the common memory budget (#3271): converted from decoded footage, the -/// first freed when the caches together are over it. +/// The decoded frames in the common memory budget (#3271, #3298): each frame is worth what +/// decoding it took, per byte. The whole stills kept to resample other sizes shortly after +/// ([`CacheState::recent`]) are offered first, as worth nothing: they are kept for a moment only. impl effectcraft_render::memory::MemoryHolder for Inner { fn name(&self) -> &'static str { "footage frames" } fn bytes(&self) -> usize { + lock(&self.cache).held_bytes() + } + fn oldest(&self) -> Option { let c = lock(&self.cache); - c.lru.bytes.saturating_add(c.recent_bytes()) + if let Some(r) = c.recent.front() { + return Some(effectcraft_render::memory::Candidate { used: 0, bytes: r.bytes.max(1), cost_us: 0 }); + } + let e = c.lru.oldest()?; + Some(effectcraft_render::memory::Candidate { used: e.used, bytes: e.bytes, cost_us: e.cost_us }) } - fn priority(&self) -> u8 { - 0 - } - fn trim_to(&self, bytes: usize) { + fn drop_oldest(&self) -> usize { let mut c = lock(&self.cache); - c.recent.clear(); - c.lru.evict(bytes); + if let Some(r) = c.recent.pop_front() { + return r.bytes.max(1); + } + let Some(k) = c.lru.oldest().and_then(|_| c.lru.order.first_key_value().map(|(_, k)| k.clone())) else { return 0 }; + let had = c.lru.bytes; + c.lru.remove(&k); + had.saturating_sub(c.lru.bytes) } } diff --git a/crates/render/src/cache.rs b/crates/render/src/cache.rs index a9feff5bf..2248326ed 100644 --- a/crates/render/src/cache.rs +++ b/crates/render/src/cache.rs @@ -54,10 +54,17 @@ struct Entry { files: Arc, bytes: usize, last_use: u64, + /// When it was last used, for the memory accountant ([`crate::memory::tick`]). + used: u64, + /// What it took to make (its render, or its read from the disk cache), in microseconds. + cost_us: u64, } struct Inner { map: HashMap, + /// The keys by last use (`Entry::last_use`, unique: the clock moves on every use), oldest + /// first: the memory accountant asks for the oldest often, so it is not looked for. + order: std::collections::BTreeMap, bytes: usize, budget: usize, clock: u64, @@ -78,24 +85,57 @@ struct Inner { const EVICTED_KEPT: usize = 256; impl Inner { + /// The least recently used entry's key. + fn oldest_key(&self) -> Option { + self.order.first_key_value().map(|(_, k)| *k) + } + + /// Mark the entry under `key` as used at `now` (the clock), for `order` and the accountant. + fn touch(&mut self, key: u64, now: u64) { + let Some(e) = self.map.get_mut(&key) else { return }; + self.order.remove(&e.last_use); + e.last_use = now; + e.used = crate::memory::tick(); + self.order.insert(now, key); + } + + /// Keep `e` under `key` (replacing what was there). + fn put(&mut self, key: u64, e: Entry) { + self.order.insert(e.last_use, key); + self.bytes = self.bytes.saturating_add(e.bytes); + if let Some(old) = self.map.insert(key, e) { + self.order.remove(&old.last_use); + self.bytes = self.bytes.saturating_sub(old.bytes); + } + } + + /// Take the entry under `key` out (its bytes no longer counted). + fn take(&mut self, key: u64) -> Option { + let e = self.map.remove(&key)?; + self.order.remove(&e.last_use); + self.bytes = self.bytes.saturating_sub(e.bytes); + Some(e) + } + + /// Drop the entry under `key`, remembering its key (a refill if it is asked for again); + /// the bytes freed. + fn evict(&mut self, key: u64) -> usize { + let Some(e) = self.take(key) else { return 0 }; + if self.evicted.len() >= EVICTED_KEPT { + self.evicted.pop_front(); + } + self.evicted.push_back(key); + e.bytes + } + /// Drop least recently used entries until at most `bytes` are held, remembering their keys. fn evict_to(&mut self, bytes: usize) { if self.bytes <= bytes { return; } - let mut by_age: Vec<(u64, u64)> = self.map.iter().map(|(k, e)| (e.last_use, *k)).collect(); - by_age.sort_unstable(); - for (_, k) in by_age { - if self.bytes <= bytes { - break; - } - if let Some(e) = self.map.remove(&k) { - self.bytes -= e.bytes; - if self.evicted.len() >= EVICTED_KEPT { - self.evicted.pop_front(); - } - self.evicted.push_back(k); - } + while self.bytes > bytes { + let Some(k) = self.oldest_key() else { break }; + self.evict(k); } } } @@ -121,6 +161,7 @@ impl LayerCache { LayerCache { inner: Mutex::new(Inner { map: HashMap::new(), + order: Default::default(), bytes: 0, budget, clock: 0, @@ -221,10 +262,11 @@ impl LayerCache { // Disk cache: a hit comes back into memory, read at generation `since`. let store = store?; let id = self.virtual_id(); + let read = web_time::Instant::now(); let (buf, mut files) = store.get_layer(layer_disk_key(key), id.as_deref())?; files.note_since(since); let files = Arc::new(files); - Some(self.insert_mem(key, buf, files)) + Some(self.insert_mem(key, buf, files, Some(read.elapsed()))) } } } @@ -268,7 +310,7 @@ impl LayerCache { store.put_layer(layer_disk_key(key), &buf, &entry, since); } } - self.insert_mem(key, buf, files) + self.insert_mem(key, buf, files, started.map(|t| t.elapsed())) } /// The buffer kept in memory under `key` with its files, if it is not made of a file dropped @@ -281,18 +323,18 @@ impl LayerCache { let mut g = self.inner.lock().ok()?; g.clock += 1; let now = g.clock; - let e = g.map.get_mut(&key)?; + let e = g.map.get(&key)?; + let found = (e.buf.clone(), e.files.clone()); if hit { - e.last_use = now; + g.touch(key, now); } - (e.buf.clone(), e.files.clone()) + found }; if self.made_before_a_drop(&found.1) { if let Ok(mut g) = self.inner.lock() && g.map.get(&key).is_some_and(|e| Arc::ptr_eq(&e.files, &found.1)) - && let Some(e) = g.map.remove(&key) { - g.bytes = g.bytes.saturating_sub(e.bytes); + g.take(key); } return None; } @@ -317,31 +359,35 @@ impl LayerCache { /// See [`LayerCache::insert`]: the buffer kept under `key` and its files (`buf` and `files`, /// unless one was kept first or `buf` is too large to keep). - fn insert_mem(&self, key: u64, buf: Arc, files: Arc) -> (Arc, Arc) { + /// `took`: what making it took (`None`: not timed; a typical cost is assumed, + /// [`crate::memory::DEFAULT_LAYER_US_PER_MB`]). + fn insert_mem(&self, key: u64, buf: Arc, files: Arc, took: Option) -> (Arc, Arc) { let bytes = buf.img.data.len() * std::mem::size_of::(); + let cost_us = took.map_or_else( + || crate::memory::default_cost(bytes, crate::memory::DEFAULT_LAYER_US_PER_MB), + |t| u64::try_from(t.as_micros()).unwrap_or(u64::MAX).max(1), + ); let Ok(mut g) = self.inner.lock() else { return (buf, files) }; if bytes > g.budget / 4 { return (buf, files); } g.clock += 1; let now = g.clock; - if let Some(e) = g.map.get_mut(&key) { + if let Some(e) = g.map.get(&key) { // (one made before a drop gives way: see [`LayerCache::kept`]) if !self.made_before_a_drop(&e.files) { - e.last_use = now; - return (e.buf.clone(), e.files.clone()); - } - if let Some(old) = g.map.remove(&key) { - g.bytes = g.bytes.saturating_sub(old.bytes); + let kept = (e.buf.clone(), e.files.clone()); + g.touch(key, now); + return kept; } + g.take(key); } // (checked again just before it is published: a drop may have come since `insert` // checked; one that comes later is caught when the buffer is looked up) if self.made_before_a_drop(&files) { return (buf, files); } - g.map.insert(key, Entry { buf: buf.clone(), files: files.clone(), bytes, last_use: now }); - g.bytes += bytes; + g.put(key, Entry { buf: buf.clone(), files: files.clone(), bytes, last_use: now, used: crate::memory::tick(), cost_us }); if g.bytes > g.budget { // Evict least-recently used entries down to 3/4 of the budget. let target = g.budget / 4 * 3; @@ -349,7 +395,7 @@ impl LayerCache { } drop(g); // (with the lock released: the accountant asks every cache for its bytes) - crate::memory::Accountant::global().note_growth(); + crate::memory::Accountant::global().note_growth(bytes); (buf, files) } @@ -368,6 +414,7 @@ impl LayerCache { pub fn clear(&self) { if let Ok(mut g) = self.inner.lock() { g.map.clear(); + g.order.clear(); g.bytes = 0; } } @@ -986,8 +1033,8 @@ pub fn derive(key: u64, i: u64) -> u64 { h.finish() } -/// The layer cache in the common memory budget (#3271): rendered layer buffers, costly to make -/// again, are freed after footage frames and planes. +/// The layer cache in the common memory budget (#3271, #3298): each buffer is worth what +/// rendering it took (or reading it from the disk cache), per byte. impl crate::memory::MemoryHolder for LayerCache { fn name(&self) -> &'static str { "layer cache" @@ -995,12 +1042,17 @@ impl crate::memory::MemoryHolder for LayerCache { fn bytes(&self) -> usize { self.inner.lock().map(|g| g.bytes).unwrap_or(0) } - fn priority(&self) -> u8 { - 2 + /// Its least recently used buffer, with what rendering it took. + fn oldest(&self) -> Option { + let g = self.inner.lock().ok()?; + let e = g.map.get(&g.oldest_key()?)?; + Some(crate::memory::Candidate { used: e.used, bytes: e.bytes, cost_us: e.cost_us }) } - fn trim_to(&self, bytes: usize) { - if let Ok(mut g) = self.inner.lock() { - g.evict_to(bytes); + fn drop_oldest(&self) -> usize { + let Ok(mut g) = self.inner.lock() else { return 0 }; + match g.oldest_key() { + Some(k) => g.evict(k), + None => 0, } } fn refills(&self) -> u64 { diff --git a/crates/render/src/disk_cache.rs b/crates/render/src/disk_cache.rs index 9d3dbc7a0..4c7817280 100644 --- a/crates/render/src/disk_cache.rs +++ b/crates/render/src/disk_cache.rs @@ -470,21 +470,23 @@ impl Queue { self.frames.is_empty() && self.jobs.is_empty() } - /// Drop queued layer and plane writes that alone hold their bytes, newest first, until the - /// ones left hold at most `bytes` of their own ([`Queue::unique_job_bytes`], what the memory - /// holder reports); frame writes are never dropped (a fully cached comp would come back with - /// gaps). The writes dropped. - #[cfg_attr(target_arch = "wasm32", allow(dead_code))] - fn trim(&mut self, bytes: usize) -> u64 { - let mut dropped = 0; - while self.unique_job_bytes() > bytes { - let Some(i) = self.jobs.iter().rposition(|j| matches!(j, Job::Write(_, _, _, p) if p.unique() > 0)) else { break }; - if let Some(Job::Write(_, _, _, p)) = self.jobs.remove(i) { - self.job_bytes = self.job_bytes.saturating_sub(p.held()); - } - dropped += 1; + /// The newest queued layer or plane write holding bytes only the queue holds: its index and + /// those bytes. (Frame writes are never dropped: a fully cached comp would come back with + /// gaps.) + fn newest_unique(&self) -> Option<(usize, usize)> { + self.jobs.iter().enumerate().rev().find_map(|(i, j)| match j { + Job::Write(_, _, _, p) if p.unique() > 0 => Some((i, p.unique())), + _ => None, + }) + } + + /// Drop [`Queue::newest_unique`]; the bytes only it held. + fn drop_newest_unique(&mut self) -> Option { + let (i, unique) = self.newest_unique()?; + if let Some(Job::Write(_, _, _, p)) = self.jobs.remove(i) { + self.job_bytes = self.job_bytes.saturating_sub(p.held()); } - dropped + Some(unique) } /// The bytes only the queued layer and plane writes hold (no frames: see the memory holder). @@ -1535,9 +1537,9 @@ impl effectcraft_effects::roto::SegStore for DiskCache { } } -/// The disk cache's write queue in the common memory budget (#3271): queued entries hold their -/// pixels until the writer has written them. The first freed (a dropped write only costs a -/// later render a disk miss); frames are kept over layer buffers. +/// The disk cache's write queue in the common memory budget (#3271, #3298): queued entries hold +/// their pixels until the writer has written them. They are copies (a dropped write only costs +/// a later render a disk miss), worth nothing to the accountant: the first freed. impl crate::memory::MemoryHolder for DiskCache { fn name(&self) -> &'static str { "disk write queue" @@ -1546,14 +1548,22 @@ impl crate::memory::MemoryHolder for DiskCache { fn bytes(&self) -> usize { self.queue.0.lock().map(|q| q.unique_job_bytes()).unwrap_or(0) } - fn priority(&self) -> u8 { - 0 + /// The newest queued write holding bytes only the queue holds (newest first: the oldest is + /// the next to be written); a copy, so it costs nothing. + fn oldest(&self) -> Option { + let q = self.queue.0.lock().ok()?; + let (_, bytes) = q.newest_unique()?; + Some(crate::memory::Candidate { used: 0, bytes, cost_us: 0 }) } - /// Drops queued layer and plane writes (newest first), see [`Queue::trim`]. - fn trim_to(&self, bytes: usize) { - let Ok(mut q) = self.queue.0.lock() else { return }; - let dropped = q.trim(bytes); - self.dropped.fetch_add(dropped, Ordering::Relaxed); + fn drop_oldest(&self) -> usize { + let Ok(mut q) = self.queue.0.lock() else { return 0 }; + match q.drop_newest_unique() { + Some(bytes) => { + self.dropped.fetch_add(1, Ordering::Relaxed); + bytes + } + None => 0, + } } } @@ -2154,7 +2164,8 @@ mod tests { q.jobs.push_back(Job::Write(Kind::Plane, 1, FORMAT_PLANES_FILES, job)); q.frames.push_back(Job::Write(Kind::Frame, 2, FORMAT_RGBA8_FILES, Payload::Frame8(1, 1, vec![0; 4], files.clone()))); assert_eq!(q.unique_job_bytes(), 69 * 8 * 4, "what the memory holder reports"); - assert_eq!(q.trim(0), 1, "trimmed as reported"); + assert_eq!(q.drop_newest_unique(), Some(69 * 8 * 4), "dropped as reported"); + assert_eq!(q.drop_newest_unique(), None, "frame writes are never dropped"); assert!(q.jobs.is_empty() && q.frames.len() == 1); q.frames.clear(); } diff --git a/crates/render/src/memory.rs b/crates/render/src/memory.rs index be806b507..20a031b61 100644 --- a/crates/render/src/memory.rs +++ b/crates/render/src/memory.rs @@ -1,17 +1,26 @@ -//! One memory accountant for the big caches (#3271): the layer cache, the media pool's frames, -//! the decoded footage planes and the preview frames each keep their own budget, and register -//! here as [`MemoryHolder`]s. When all of them together hold more than the one budget -//! ([`Accountant::set_budget`], the sum of Settings ▸ Memory & CPU's budgets), the accountant -//! frees from the lowest priority first, in large steps: down to [`TRIM_TO`] of the budget, so +//! One memory accountant for the big caches (#3271, #3298): the layer cache, the media pool's +//! frames, the decoded footage planes, the preview frames and the disk cache's write queue +//! register here as [`MemoryHolder`]s and share one budget ([`Accountant::set_budget`], from +//! Settings ▸ Memory & CPU ▸ RAM Reserved for Other Applications). No holder has a share of its +//! own: when they hold more than the budget together, the accountant frees the least valuable +//! item first, whichever holder keeps it, in large steps: down to [`TRIM_TO`] of the budget, so //! a cache doesn't fill up and trim again with every insert. //! +//! An item's value is what it would cost to make again per byte ([`Candidate::cost_us`], the +//! measured render or decode time where known, else a typical one: [`DEFAULT_LAYER_US_PER_MB`] +//! and its neighbours), decayed by the time since it was last used ([`HALF_LIFE_US`]): +//! [`value`]. A copy of data another cache holds (a queued disk write) costs nothing to make +//! again. Each holder offers the item it would drop next (its least recently used one, +//! [`MemoryHolder::oldest`]); the accountant drops the least valuable of those, asks that holder +//! for its next one, and repeats. +//! //! 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. +//! [`MemoryHolder::refills`]) is thrashing: its items are worth [`THRASH_BOOST`] times more +//! until it settles, so the data that is needed again is kept longest. //! //! Holders are held weakly: a cache that goes away is forgotten. The accountant never calls a -//! holder while it holds its own lock, and holders call [`Accountant::balance`] after releasing -//! theirs. +//! holder while it holds its own lock, and holders call [`Accountant::note_growth`] after +//! releasing theirs. use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex, PoisonError, Weak}; @@ -19,18 +28,67 @@ 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; +/// An item unused this long is worth half as much as one just used ([`value`]), a quarter at +/// three times this, and so on (`1 / (1 + age / HALF_LIFE_US)`). +pub const HALF_LIFE_US: u64 = 30_000_000; + +/// While a holder thrashes, its items are worth this many times more ([`value`]). +pub const THRASH_BOOST: f64 = 4.0; + +/// What it typically costs to make again a megabyte of each holder's data when its own time was +/// not measured (microseconds per MB): a processed layer buffer… +pub const DEFAULT_LAYER_US_PER_MB: u64 = 4_000; +/// …a RAM preview frame (the whole comp rendered)… +pub const DEFAULT_PREVIEW_US_PER_MB: u64 = 8_000; +/// …a decoded footage frame… +pub const DEFAULT_FRAME_US_PER_MB: u64 = 2_000; +/// …and decoded footage planes (OpenEXR channels). +pub const DEFAULT_PLANE_US_PER_MB: u64 = 2_000; + +/// The typical cost (µs) of making again `bytes` bytes at `us_per_mb`. +pub fn default_cost(bytes: usize, us_per_mb: u64) -> u64 { + let mb = u64::try_from(bytes).unwrap_or(u64::MAX) >> 20; + mb.max(1).saturating_mul(us_per_mb) +} + +/// The use stamp holders give their items: microseconds since the process's first stamp +/// (monotonic; two uses may share one). +pub fn tick() -> u64 { + static START: std::sync::OnceLock = std::sync::OnceLock::new(); + let start = START.get_or_init(web_time::Instant::now); + u64::try_from(start.elapsed().as_micros()).unwrap_or(u64::MAX) +} + +/// Inserts balance at most this often ([`Accountant::note_growth`])… +const BALANCE_EVERY: std::time::Duration = std::time::Duration::from_millis(100); + +/// …unless the holders grew by more than this part of the budget since the last balance +/// (several large decodes landing within one interval could otherwise overshoot by gigabytes). +const GROWTH_PART: usize = 16; /// How long a holder that refilled stays protected. const THRASH_FOR: std::time::Duration = std::time::Duration::from_secs(10); -/// Inserts balance at most this often ([`Accountant::note_growth`]). -const BALANCE_EVERY: std::time::Duration = std::time::Duration::from_millis(100); +/// The item a holder would drop next ([`MemoryHolder::oldest`]). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Candidate { + /// When it was last used ([`tick`]). + pub used: u64, + /// The bytes dropping it frees. + pub bytes: usize, + /// What making it again costs, in microseconds (0: a copy of data another cache holds). + pub cost_us: u64, +} + +/// What an item is worth keeping at `now` ([`tick`]): its cost to make again per byte, halved +/// after [`HALF_LIFE_US`] unused, [`THRASH_BOOST`] times more while its holder thrashes. +pub fn value(c: &Candidate, now: u64, thrashing: bool) -> f64 { + let per_byte = c.cost_us as f64 / c.bytes.max(1) as f64; + let age = now.saturating_sub(c.used) as f64; + let half = HALF_LIFE_US as f64; + let v = per_byte * half / (half + age); + if thrashing { v * THRASH_BOOST } else { v } +} /// A cache the accountant keeps within the common budget. pub trait MemoryHolder: Send + Sync { @@ -38,11 +96,11 @@ pub trait MemoryHolder: Send + Sync { fn name(&self) -> &'static str; /// The bytes it holds now. fn bytes(&self) -> usize; - /// How valuable its data is: lower is freed first (a copy of what another cache holds, - /// then what is cheap to make again, then what is expensive). - fn priority(&self) -> u8; - /// Free its least valuable data until it holds at most `bytes` (data in use may stay). - fn trim_to(&self, bytes: usize); + /// The item it would drop next, its least recently used one it may drop (`None`: nothing it + /// may drop, such as data in use). + fn oldest(&self) -> Option; + /// Drop that item; the bytes freed (0: nothing dropped). + fn drop_oldest(&self) -> usize; /// How many times so far it made again what it had let go of shortly before (0 when it /// doesn't tell). fn refills(&self) -> u64 { @@ -63,9 +121,13 @@ pub struct Accountant { holders: Mutex>, /// Bytes freed by the accountant so far (stats, tests). freed: AtomicUsize, + /// Bytes the holders said they grew by since the last balance ([`Accountant::note_growth`]). + grown: AtomicUsize, /// One balance at a time (another one asked for meanwhile is skipped), and when the last /// one ran. balancing: Mutex>, + /// The clock items' ages are measured with ([`tick`]; tests give their own). + clock: fn() -> u64, } impl Default for Accountant { @@ -77,7 +139,19 @@ 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::with_clock(budget, tick) + } + + /// An accountant with `budget` bytes measuring ages with `clock` (in [`tick`]'s units). + pub const fn with_clock(budget: usize, clock: fn() -> u64) -> Accountant { + Accountant { + budget: AtomicUsize::new(budget), + holders: Mutex::new(Vec::new()), + freed: AtomicUsize::new(0), + grown: AtomicUsize::new(0), + balancing: Mutex::new(None), + clock, + } } /// The process's accountant. @@ -108,20 +182,24 @@ impl Accountant { h.push(Registered { holder: weak, refills: holder.refills(), thrash_until: None }); } - /// 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 { + /// A holder grew by `bytes` (an insert): balance, at most every [`BALANCE_EVERY`] (the + /// holders' locks aren't taken on every insert), or at once when the holders grew by more + /// than a [`GROWTH_PART`]th of the budget since the last balance. + pub fn note_growth(&self, bytes: usize) { + let budget = self.budget(); + if budget == usize::MAX { return; } + let grown = self.grown.fetch_add(bytes, Ordering::Relaxed).saturating_add(bytes); + let burst = grown > budget / GROWTH_PART; let recent = self.balancing.try_lock().map(|g| g.is_some_and(|t| t.elapsed() < BALANCE_EVERY)).unwrap_or(true); - if !recent { + if burst || !recent { self.balance(); } } - /// The holders alive now, with their effective priority (raised while thrashing). - fn alive(&self) -> Vec<(Arc, u8)> { + /// The holders alive now, and whether each is thrashing. + fn alive(&self) -> Vec<(Arc, bool)> { let mut h = self.holders.lock().unwrap_or_else(PoisonError::into_inner); h.retain(|r| r.holder.strong_count() > 0); h.iter_mut() @@ -133,9 +211,7 @@ impl Accountant { r.thrash_until = Some(now + THRASH_FOR); } r.refills = refills; - let thrashing = r.thrash_until.is_some_and(|t| t > now); - let p = holder.priority().saturating_add(u8::from(thrashing)); - Some((holder, p)) + Some((holder, r.thrash_until.is_some_and(|t| t > now))) }) .collect() } @@ -153,8 +229,9 @@ 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 together are over the budget, free down to [`TRIM_TO`] of it, the least + /// valuable item first whichever holder keeps it ([`value`]; equal values: the least + /// recently used first, then the holder registered first). pub fn balance(&self) { let budget = self.budget(); if budget == usize::MAX { @@ -163,22 +240,32 @@ impl Accountant { // (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(); + self.grown.store(0, Ordering::Relaxed); + let holders = self.alive(); 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 { - if total <= target { - break; + let now = (self.clock)(); + // Each holder's next item (`None`: it has nothing more it may drop). + let mut next: Vec> = holders.iter().map(|(h, _)| h.oldest()).collect(); + while total > target { + let pick = next + .iter() + .enumerate() + .filter_map(|(i, c)| { + let c = (*c)?; + let thrashing = holders.get(i).is_some_and(|(_, t)| *t); + Some((i, value(&c, now, thrashing), c.used)) + }) + .min_by(|a, b| a.1.total_cmp(&b.1).then(a.2.cmp(&b.2)).then(a.0.cmp(&b.0))); + let Some((i, _, _)) = pick else { break }; + let Some((h, _)) = holders.get(i) else { break }; + let freed = h.drop_oldest(); + if let Some(slot) = next.get_mut(i) { + *slot = if freed == 0 { None } else { h.oldest() }; } - let had = h.bytes(); - let over = total - target; - h.trim_to(had.saturating_sub(over)); - let now = h.bytes(); - let freed = had.saturating_sub(now); total = total.saturating_sub(freed); self.freed.fetch_add(freed, Ordering::Relaxed); } @@ -189,24 +276,26 @@ impl Accountant { mod tests { use super::*; - /// A holder of `bytes` in blocks of `step`, trimmed whole blocks at a time. + /// A holder of items `(used, bytes, cost_us)`, least recently used first. struct Fake { name: &'static str, - bytes: Mutex, - step: usize, - priority: u8, + items: Mutex>, refills: Mutex, } 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) }) + fn new(name: &'static str, items: &[(u64, usize, u64)]) -> Arc { + let items = items.iter().map(|&(used, bytes, cost_us)| Candidate { used, bytes, cost_us }).collect(); + Arc::new(Fake { name, items: Mutex::new(items), refills: Mutex::new(0) }) } fn held(&self) -> usize { - *self.bytes.lock().unwrap() + self.items.lock().unwrap().iter().map(|c| c.bytes).sum() } - fn fill(&self, more: usize) { - *self.bytes.lock().unwrap() += more; + fn count(&self) -> usize { + self.items.lock().unwrap().len() + } + fn push(&self, used: u64, bytes: usize, cost_us: u64) { + self.items.lock().unwrap().push_back(Candidate { used, bytes, cost_us }); } } @@ -217,14 +306,11 @@ mod tests { fn bytes(&self) -> usize { self.held() } - fn priority(&self) -> u8 { - self.priority + fn oldest(&self) -> Option { + self.items.lock().unwrap().front().copied() } - fn trim_to(&self, bytes: usize) { - let mut b = self.bytes.lock().unwrap(); - while *b > bytes { - *b = b.saturating_sub(self.step); - } + fn drop_oldest(&self) -> usize { + self.items.lock().unwrap().pop_front().map_or(0, |c| c.bytes) } fn refills(&self) -> u64 { *self.refills.lock().unwrap() @@ -235,64 +321,139 @@ mod tests { f.clone() } - /// Over the budget, the lowest priority is freed first, down to 90% of the budget; under it, - /// nothing is touched. - #[test] - fn lowest_priority_goes_first_down_to_the_trim_line() { - let acc = Accountant::new(1000); - let (planes, frames, layers) = (Fake::new("planes", 300, 0), Fake::new("frames", 400, 1), Fake::new("layers", 300, 2)); - let held: Vec> = vec![holder(&layers), holder(&planes), holder(&frames)]; - for h in &held { - acc.register(h); - } - acc.balance(); - assert_eq!((planes.held(), frames.held(), layers.held()), (300, 400, 300), "at the budget: nothing freed"); - frames.fill(200); - acc.balance(); - // 1200 held, down to 900: the planes go first (300), then frames give the rest. - assert_eq!((planes.held(), frames.held(), layers.held()), (0, 600, 300)); - assert_eq!(acc.total(), 900); - assert_eq!(acc.freed(), 300); - // A little over again: freed in one step down to 900, not by the few bytes over. - layers.fill(110); - acc.balance(); - assert_eq!(acc.total(), 900); + /// The tests' clock: everything is measured at 100 s. + fn at_100s() -> u64 { + 100_000_000 } - /// A holder that makes again what it let go of is kept longer: its priority goes up while it - /// thrashes. - #[test] - fn a_thrashing_holder_is_protected() { - let acc = Accountant::new(1000); - let (cheap, other) = (Fake::new("cheap", 600, 0), Fake::new("other", 600, 0)); - let held: Vec> = vec![holder(&cheap), holder(&other)]; + fn accountant(budget: usize, held: &[&Arc]) -> (Accountant, Vec>) { + let acc = Accountant::with_clock(budget, at_100s); + let held: Vec> = held.iter().map(|f| holder(f)).collect(); for h in &held { acc.register(h); } + (acc, held) + } + + const S: u64 = 1_000_000; + + /// #3298: an expensive preview frame outlives a cheap footage plane of the same age and size. + #[test] + fn an_expensive_item_outlives_a_cheap_one_of_the_same_age() { + let preview = Fake::new("preview frames", &[(90 * S, 100, 50_000)]); + let planes = Fake::new("footage planes", &[(90 * S, 100, 5_000)]); + let (acc, _held) = accountant(150, &[&preview, &planes]); + acc.balance(); + assert_eq!((preview.count(), planes.count()), (1, 0)); + } + + /// #3298: a recent, costly plane outlives an older, cheap preview frame (by kind alone the + /// preview frames were freed last, whatever they cost and however long unused). + #[test] + fn a_recent_costly_plane_outlives_an_old_cheap_preview_frame() { + let preview = Fake::new("preview frames", &[(0, 100, 5_000)]); + let planes = Fake::new("footage planes", &[(99 * S, 100, 50_000)]); + let (acc, _held) = accountant(150, &[&planes, &preview]); + acc.balance(); + assert_eq!((preview.count(), planes.count()), (0, 1)); + } + + /// Value decays with time unused: of two items that cost the same, the older goes first, and + /// one unused for long enough goes before a costlier one used just now. + #[test] + fn value_falls_with_time_unused() { + let c = |used, cost_us| Candidate { used, bytes: 100, cost_us }; + assert!(value(&c(0, 1000), at_100s(), false) < value(&c(99 * S, 1000), at_100s(), false)); + let half = value(&c(at_100s() - HALF_LIFE_US, 1000), at_100s(), false); + assert!((half - 5.0).abs() < 1e-9, "{half}"); + assert!(value(&c(0, 2000), at_100s(), false) < value(&c(at_100s(), 1000), at_100s(), false)); + assert_eq!(value(&c(at_100s(), 0), at_100s(), false), 0.0, "a copy is worth nothing"); + assert_eq!(value(&c(0, 1000), at_100s(), true), THRASH_BOOST * value(&c(0, 1000), at_100s(), false)); + } + + /// Over the budget, items go least valuable first across holders, down to 90 % of the + /// budget in one step; under it nothing is touched, and a copy goes first. + #[test] + fn least_valuable_first_down_to_the_trim_line() { + let queue = Fake::new("disk write queue", &[(99 * S, 300, 0)]); + let frames = Fake::new("footage frames", &[(10 * S, 100, 2_000), (20 * S, 100, 2_000), (30 * S, 100, 2_000), (40 * S, 100, 2_000)]); + let layers = Fake::new("layer cache", &[(10 * S, 100, 40_000), (20 * S, 100, 40_000), (30 * S, 100, 40_000)]); + let (acc, _held) = accountant(1000, &[&layers, &frames, &queue]); + acc.balance(); + assert_eq!((queue.held(), frames.held(), layers.held()), (300, 400, 300), "at the budget: nothing freed"); + frames.push(50 * S, 200, 4_000); + acc.balance(); + // 1200 held, down to 900: the queued copy goes first (300), nothing more is needed. + assert_eq!((queue.held(), frames.held(), layers.held()), (0, 600, 300)); + assert_eq!(acc.total(), 900); + assert_eq!(acc.freed(), 300); + // A little over again: freed in one step to 900 or below, the cheap frames first. + layers.push(60 * S, 300, 120_000); + acc.balance(); + assert!(acc.total() <= 900, "{}", acc.total()); + assert_eq!(layers.held(), 600, "the costly layers stay"); + } + + /// A holder that makes again what it let go of is kept longer: its items are worth more + /// while it thrashes. + #[test] + fn a_thrashing_holder_is_protected() { + let cheap = Fake::new("cheap", &[(50 * S, 300, 1_000), (50 * S, 300, 1_000)]); + let other = Fake::new("other", &[(50 * S, 300, 2_000), (50 * S, 300, 2_000)]); + let (acc, _held) = accountant(1000, &[&cheap, &other]); *cheap.refills.lock().unwrap() += 3; acc.balance(); assert_eq!(cheap.held(), 600, "thrashing: kept"); assert_eq!(other.held(), 300); } + /// One holder alone may fill the whole budget: no holder has a share of its own (#3298: a + /// workload of footage only was held to the Footage Frame Cache). + #[test] + fn one_holder_may_use_the_whole_budget() { + let frames = Fake::new("footage frames", &[]); + let layers = Fake::new("layer cache", &[]); + let (acc, _held) = accountant(1000, &[&frames, &layers]); + for i in 0..10 { + frames.push(i * S, 100, 2_000); + } + acc.balance(); + assert_eq!(frames.held(), 1000); + } + + /// Growth within the balance interval still balances once it passes a part of the budget: + /// a burst of large inserts can't run far past the budget before the next interval. + #[test] + fn a_burst_of_growth_balances_at_once() { + let frames = Fake::new("footage frames", &[]); + let (acc, _held) = accountant(1600, &[&frames]); + acc.balance(); + // (within the interval of the balance just run) + for i in 0..10 { + frames.push(i * S, 200, 2_000); + acc.note_growth(200); + } + assert!(frames.held() <= 1600 + 200, "{}", frames.held()); + } + /// Holders that went away are forgotten; no budget means nothing is freed. #[test] fn holders_are_held_weakly() { let acc = Accountant::new(10); { - let gone = Fake::new("gone", 500, 0); + let gone = Fake::new("gone", &[(0, 500, 1)]); acc.register(&holder(&gone)); } assert_eq!(acc.total(), 0); let free = Accountant::default(); - let kept = Fake::new("kept", 500, 0); + let kept = Fake::new("kept", &[(0, 500, 1)]); free.register(&holder(&kept)); free.balance(); assert_eq!(kept.held(), 500); } - /// The layer cache as a holder: trimmed least recently used first, and a buffer asked for - /// again after the accountant let it go counts as a refill, which protects the cache. + /// The layer cache as a holder: its least recently used buffer goes first, and a buffer + /// asked for again after the accountant let it go counts as a refill. #[test] fn the_layer_cache_is_trimmed_and_tells_refills() { use effectcraft_effects::Buf; @@ -309,10 +470,36 @@ mod tests { assert!(layers.bytes() <= (2.0 * one as f64 * TRIM_TO) as usize, "{}", layers.bytes()); assert!(cache.get(0).is_none(), "the oldest went first"); assert_eq!(layers.refills(), 1, "asked for again after it was let go of"); - // Thrashing: a holder of the same priority is freed before it. - let other = Fake::new("other", 2 * one, 2); - acc.register(&holder(&other)); - acc.balance(); - assert!(other.held() < 2 * one, "the other holder gave way"); + let next = layers.oldest().unwrap(); + assert!(next.cost_us > 0 && next.bytes == one, "{next:?}"); + } + + /// Halving a full budget (Reduce Cache Size When System Is Low on Memory) frees thousands of + /// items in one balance, on the thread that inserted last: each step must be cheap (the + /// layer cache keeps its buffers ordered by use instead of looking for the oldest). + #[test] + fn halving_a_full_budget_is_quick() { + use effectcraft_effects::Buf; + use effectcraft_raster::Image; + let cache = Arc::new(crate::cache::LayerCache::new(1 << 40)); + for k in 0..5000u64 { + cache.insert(k, Arc::new(Buf { img: Image::new(8, 8), offset: [0.0; 2], scale: 1.0 }), Default::default()); + } + let frames = Fake::new("footage frames", &[]); + for i in 0..5000 { + frames.push(i, 1024, 2_000); + } + let acc = Accountant::new(usize::MAX); + let held: Vec> = vec![cache.clone(), holder(&frames)]; + for h in &held { + acc.register(h); + } + let full = acc.total(); + let t = web_time::Instant::now(); + acc.set_budget(full / 2); + let took = t.elapsed(); + eprintln!("halving {full} bytes of 10000 items took {took:?}"); + assert!(acc.total() <= (full as f64 / 2.0 * TRIM_TO) as usize + 1024); + assert!(took < std::time::Duration::from_secs(2), "{took:?}"); } } diff --git a/crates/ui-egui/src/app/prefs.rs b/crates/ui-egui/src/app/prefs.rs index 6fc424e01..218760337 100644 --- a/crates/ui-egui/src/app/prefs.rs +++ b/crates/ui-egui/src/app/prefs.rs @@ -23,7 +23,9 @@ impl EffectcraftApp { ctx.set_zoom_factor(p.appearance.ui_scale as f32 / 100.0); self.ui.cache_when_idle = p.previews.cache_frames_when_idle; self.ui.viewer.fast_preview = p.previews.fast_previews; - self.frames.set_budget(p.cache_budgets(self.session.sys_memory).preview); + // No limit of its own below the common budget on the desktop, the browser's cap there: + // the memory accountant keeps it (#3298). + self.frames.set_budget(self.session.memory_budget().caps(cfg!(target_arch = "wasm32")).preview); } /// Change settings from the UI (applied next frame and saved). diff --git a/crates/ui-egui/src/frames.rs b/crates/ui-egui/src/frames.rs index 75f364ab6..0a633afa8 100644 --- a/crates/ui-egui/src/frames.rs +++ b/crates/ui-egui/src/frames.rs @@ -173,6 +173,10 @@ struct Cached { /// (`None`: rendered without a disk cache, or elsewhere; never written from here). #[cfg_attr(target_arch = "wasm32", allow(dead_code))] since: Option, + /// When it was last shown or stored, for the memory accountant (`memory::tick`). + used: u64, + /// What rendering it took, in microseconds (a typical cost when not timed). + cost_us: u64, } /// A disk cache and its drop generation ([`DiskCache::drop_generation`]) when a render started. @@ -218,9 +222,20 @@ impl Cache { } /// [`Cache::insert`] with the source files the frame was made of and the disk cache's drop /// generation when its render started. + #[cfg(test)] fn insert_with(&mut self, k: FrameKey, img: FrameImage, files: Option>, since: Option) { + self.insert_took(k, img, files, since, None); + } + /// Keep a frame with the source files it was made of, the disk cache drop generation when its + /// render started, and what rendering it took (`None`: not timed). + fn insert_took(&mut self, k: FrameKey, img: FrameImage, files: Option>, since: Option, took: Option) { let (sz, gpu) = (img.bytes(), img.is_gpu()); - if let Some(old) = self.map.insert(k, Cached { img, files, since }) { + let cost_us = took.map_or_else( + || effectcraft_engine::render::memory::default_cost(sz, effectcraft_engine::render::memory::DEFAULT_PREVIEW_US_PER_MB), + |t| u64::try_from(t.as_micros()).unwrap_or(u64::MAX).max(1), + ); + let used = effectcraft_engine::render::memory::tick(); + if let Some(old) = self.map.insert(k, Cached { img, files, since, used, cost_us }) { self.forget(&old.img); } else { self.order.push_back(k); @@ -311,6 +326,9 @@ impl Cache { } /// Shown again: last in line for eviction. fn touch(&mut self, k: &FrameKey) { + if let Some(c) = self.map.get_mut(k) { + c.used = effectcraft_engine::render::memory::tick(); + } if let Some(i) = self.order.iter().rposition(|o| o == k) && let Some(k) = self.order.remove(i) { @@ -347,7 +365,9 @@ impl Cache { } /// The preview frames (in RAM; GPU frames have their own budget) in the common memory budget -/// (#3271): what the user looks at, freed last. +/// (#3271, #3298): each frame is worth what rendering it took, per byte. The frame offered is +/// the one the cache would evict next ([`Cache::cpu_victim`]: it spares the frames a playing +/// preview shows next). struct PreviewMemory(Arc>); impl effectcraft_engine::render::memory::MemoryHolder for PreviewMemory { @@ -357,16 +377,27 @@ impl effectcraft_engine::render::memory::MemoryHolder for PreviewMemory { fn bytes(&self) -> usize { self.0.lock().map(|c| c.bytes.saturating_sub(c.gpu_bytes)).unwrap_or(0) } - fn priority(&self) -> u8 { - 3 + // (only frames in RAM: dropping a GPU frame frees no RAM, and loses a viewer frame) + /// A frame a playing preview shows next counts as just used: it is about to be (#3298 + /// review: one rendered from cached layers costs little, and older footage outlived it, so + /// the preview played into live renders). A frame costs at least a typical render + /// ([`effectcraft_engine::render::memory::DEFAULT_PREVIEW_US_PER_MB`]): a quick one still stands for a whole comp. + fn oldest(&self) -> Option { + let c = self.0.lock().ok()?; + let k = c.order.get(c.cpu_victim()?)?; + let f = c.map.get(k)?; + let bytes = f.img.bytes(); + let ahead = c.playhead.as_ref().is_some_and(|p| p.ahead(k).is_some()); + let used = if ahead { effectcraft_engine::render::memory::tick() } else { f.used }; + let cost_us = f.cost_us.max(effectcraft_engine::render::memory::default_cost(bytes, effectcraft_engine::render::memory::DEFAULT_PREVIEW_US_PER_MB)); + Some(effectcraft_engine::render::memory::Candidate { used, bytes, cost_us }) } - 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.cpu_victim().and_then(|i| c.order.remove(i)) else { break }; - c.remove(&old); - } + fn drop_oldest(&self) -> usize { + let Ok(mut c) = self.0.lock() else { return 0 }; + let Some(old) = c.cpu_victim().and_then(|i| c.order.remove(i)) else { return 0 }; + let bytes = c.map.get(&old).map_or(0, |f| f.img.bytes()); + c.remove(&old); + bytes } } @@ -1770,11 +1801,14 @@ impl Worker { if urgent && let Ok(mut l) = self.last.lock() { *l = t0.elapsed().as_secs_f64() * 1000.0; } + let ram = if img.is_gpu() { 0 } else { img.bytes() }; if let Ok(mut c) = self.cache.lock() && (!img.is_gpu() || !self.gpu_retired.load(Ordering::Acquire)) { - c.insert_with(key, img, files, since); + c.insert_took(key, img, files, since, Some(t0.elapsed())); } + // (with the lock released: the accountant asks every cache for its bytes) + effectcraft_engine::render::memory::Accountant::global().note_growth(ram); self.release(key, urgent); } diff --git a/crates/ui-egui/src/frames/tests.rs b/crates/ui-egui/src/frames/tests.rs index 80950e9a9..22013fa5d 100644 --- a/crates/ui-egui/src/frames/tests.rs +++ b/crates/ui-egui/src/frames/tests.rs @@ -2989,10 +2989,64 @@ fn the_accountant_trims_only_frames_in_ram() { let memory = PreviewMemory(frames.cache.clone()); use effectcraft_engine::render::memory::MemoryHolder; assert_eq!(memory.bytes(), 2 * 16); - memory.trim_to(16); + let next = memory.oldest().unwrap(); + assert_eq!(next.bytes, 16, "a frame in RAM is offered"); + assert!(next.cost_us > 0); + assert_eq!(memory.drop_oldest(), 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); } + +/// #3298 review: preview frames compete with the other caches by value. A frame a playing +/// preview shows next counts as just used, and a frame rendered quickly (from cached layers) +/// still costs a typical render: an old, cheap footage frame goes before it. +#[test] +fn preview_frames_ahead_of_the_playhead_outlive_old_footage() { + use effectcraft_engine::render::memory::{self, Accountant, Candidate, MemoryHolder}; + struct Footage(Mutex>); + impl MemoryHolder for Footage { + fn name(&self) -> &'static str { + "footage frames" + } + fn bytes(&self) -> usize { + self.0.lock().unwrap().iter().map(|c| c.bytes).sum() + } + fn oldest(&self) -> Option { + self.0.lock().unwrap().first().copied() + } + fn drop_oldest(&self) -> usize { + let mut v = self.0.lock().unwrap(); + if v.is_empty() { 0 } else { v.remove(0).bytes } + } + } + let frames = Frames::default(); + { + let mut c = frames.cache.lock().unwrap(); + for i in 10..12 { + c.insert_took(key(1, i, 0), img(), None, None, Some(std::time::Duration::from_micros(100))); + // (as old as the footage frame below) + c.map.get_mut(&key(1, i, 0)).unwrap().used = 0; + } + } + let preview: Arc = Arc::new(PreviewMemory(frames.cache.clone())); + // Stopped: the oldest frame is offered as it is, with at least a typical render's cost. + let offered = preview.oldest().unwrap(); + assert_eq!(offered.used, 0); + assert_eq!(offered.cost_us, memory::default_cost(16, memory::DEFAULT_PREVIEW_US_PER_MB)); + // Playing from frame 10: the frames ahead count as just used. + frames.set_playhead(Some(playhead(10, false))); + let before = memory::tick(); + assert!(preview.oldest().unwrap().used >= before); + // An old footage frame that cost less per byte goes first. + let footage = Arc::new(Footage(Mutex::new(vec![Candidate { used: 0, bytes: 16, cost_us: 6_000 }]))); + let footage_holder: Arc = footage.clone(); + let acc = Accountant::new(40); + acc.register(&preview); + acc.register(&footage_holder); + acc.balance(); + assert_eq!(footage.bytes(), 0, "the footage frame went"); + assert_eq!(preview.bytes(), 32, "the preview frames stay"); +} diff --git a/crates/ui-egui/src/i18n/ui.rs b/crates/ui-egui/src/i18n/ui.rs index c08863be9..a67479e6d 100644 --- a/crates/ui-egui/src/i18n/ui.rs +++ b/crates/ui-egui/src/i18n/ui.rs @@ -897,10 +897,8 @@ const JAPANESE: &[(&str, &str)] = &[ ("Folders or drives, separated by ;", "フォルダーまたはドライブ(; で区切る)"), ("Memory & CPU", "メモリとCPU"), ("RAM Reserved for Other Applications", "他のアプリケーション用に確保するRAM"), - ("Cache Budgets", "キャッシュの割り当て"), - ("Layer Cache", "レイヤーキャッシュ"), - ("Footage Frame Cache", "フッテージフレームキャッシュ"), - ("Preview (RAM) Cache", "プレビュー(RAM)キャッシュ"), + ("EffectCraft may use: {} of {}", "EffectCraftが使用できるメモリ:{}/{}"), + ("EffectCraft may use: {} (installed RAM unknown)", "EffectCraftが使用できるメモリ:{}(搭載RAMは不明)"), ("Reduce Cache Size When System Is Low on Memory", "システムのメモリが不足しているときにキャッシュサイズを減らす"), ("Video", "ビデオ"), ("Enable Video Preview Output", "ビデオプレビュー出力を有効にする"), @@ -2039,10 +2037,8 @@ const SIMPLIFIED_CHINESE: &[(&str, &str)] = &[ ("Folders or drives, separated by ;", "文件夹或驱动器,以 ; 分隔"), ("Memory & CPU", "内存和 CPU"), ("RAM Reserved for Other Applications", "为其他应用程序保留的内存"), - ("Cache Budgets", "缓存预算"), - ("Layer Cache", "图层缓存"), - ("Footage Frame Cache", "素材帧缓存"), - ("Preview (RAM) Cache", "预览(内存)缓存"), + ("EffectCraft may use: {} of {}", "EffectCraft 可使用:{},共 {}"), + ("EffectCraft may use: {} (installed RAM unknown)", "EffectCraft 可使用:{}(已安装内存未知)"), ("Reduce Cache Size When System Is Low on Memory", "系统内存不足时减少缓存大小"), ("Video", "视频"), ("Enable Video Preview Output", "启用视频预览输出"), @@ -3161,10 +3157,8 @@ const SWEDISH: &[(&str, &str)] = &[ ("Folders or drives, separated by ;", "Mappar eller enheter, åtskilda med ;"), ("Memory & CPU", "Minne och processor"), ("RAM Reserved for Other Applications", "RAM reserverat för andra program"), - ("Cache Budgets", "Cachebudgetar"), - ("Layer Cache", "Lagercache"), - ("Footage Frame Cache", "Bildrutecache för material"), - ("Preview (RAM) Cache", "Förhandsgranskningscache (RAM)"), + ("EffectCraft may use: {} of {}", "EffectCraft får använda: {} av {}"), + ("EffectCraft may use: {} (installed RAM unknown)", "EffectCraft får använda: {} (installerat RAM okänt)"), ("Reduce Cache Size When System Is Low on Memory", "Minska cachestorleken när systemet har ont om minne"), ("Video", "Video"), ("Enable Video Preview Output", "Aktivera videoutgång för förhandsgranskning"), @@ -4389,10 +4383,8 @@ const TRADITIONAL_CHINESE: &[(&str, &str)] = &[ ("Folders or drives, separated by ;", "資料夾或磁碟機,以 ; 分隔"), ("Memory & CPU", "記憶體和 CPU"), ("RAM Reserved for Other Applications", "為其他應用程式保留的記憶體"), - ("Cache Budgets", "快取預算"), - ("Layer Cache", "圖層快取"), - ("Footage Frame Cache", "素材影格快取"), - ("Preview (RAM) Cache", "預覽(記憶體)快取"), + ("EffectCraft may use: {} of {}", "EffectCraft 可使用:{},共 {}"), + ("EffectCraft may use: {} (installed RAM unknown)", "EffectCraft 可使用:{}(已安裝記憶體未知)"), ("Reduce Cache Size When System Is Low on Memory", "系統記憶體不足時減少快取大小"), ("Video", "視訊"), ("Enable Video Preview Output", "啟用視訊預覽輸出"), @@ -5871,7 +5863,6 @@ const PORTUGUESE: &[(&str, &str)] = &[ ("Automatically Reload Footage", "Recarregar mídias automaticamente"), ("Automatically Save Projects", "Salvar projetos automaticamente"), ("Brightness", "Brilho"), - ("Cache Budgets", "Orçamentos de cache"), ("Conformed Audio Folder", "Pasta de áudio conformado"), ("Network Footage", "Mídia de rede"), ("Localize Network Footage", "Copiar mídia de rede localmente"), @@ -5904,6 +5895,8 @@ const PORTUGUESE: &[(&str, &str)] = &[ "EffectCraft always uses its own colour picker (the same on every platform and the web).", "O EffectCraft sempre usa o próprio seletor de cores (o mesmo em todas as plataformas e na web).", ), + ("EffectCraft may use: {} of {}", "O EffectCraft pode usar: {} de {}"), + ("EffectCraft may use: {} (installed RAM unknown)", "O EffectCraft pode usar: {} (RAM instalada desconhecida)"), ("Empty Disk Cache", "Esvaziar cache do disco"), ("Enable Disk Cache", "Ativar cache do disco"), ("Enable JavaScript Debugger", "Ativar depurador JavaScript"), @@ -5924,7 +5917,6 @@ const PORTUGUESE: &[(&str, &str)] = &[ ("Fit", "Ajustar"), ("Fit up to 100%", "Ajustar até 100%"), ("Floating Window", "Janela flutuante"), - ("Footage Frame Cache", "Cache de quadros de mídia"), ("GPU Information...", "Informações da GPU..."), ("Gridline Every", "Grade a cada"), ("Hardware Accelerate Composition, Layer and Footage Panels", "Aceleração por hardware dos painéis de composição, camada e mídia"), @@ -5932,7 +5924,6 @@ const PORTUGUESE: &[(&str, &str)] = &[ ("Interpret Unlabeled Alpha As", "Interpretar alfa não rotulado como"), ("Keyframes", "Keyframes"), ("Labels and Colors", "Rótulos e cores"), - ("Layer Cache", "Cache de camada"), ("Length of Composition", "Duração da composição"), ("Levels of Undo", "Níveis de desfazer"), ("Light Theme", "Tema claro"), @@ -5958,7 +5949,6 @@ const PORTUGUESE: &[(&str, &str)] = &[ ("OpenGL (compatibility)", "OpenGL (compatibilidade)"), ("Panels are always drawn by the GPU (wgpu).", "Os painéis são sempre desenhados pela GPU (wgpu)."), ("Preserve Constant Vertex and Feather Point Count when Editing Masks", "Preservar o número constante de vértices e pontos de feather ao editar máscaras"), - ("Preview (RAM) Cache", "Cache de pré-visualização (RAM)"), ("RAM Reserved for Other Applications", "RAM reservada para outros aplicativos"), ("RGB Straight", "RGB direto"), ("Realtime Shadows in Draft", "Sombras em tempo real no rascunho"), @@ -6873,10 +6863,8 @@ const POLISH: &[(&str, &str)] = &[ ("Folders or drives, separated by ;", "Foldery lub dyski, rozdzielone znakiem ;"), ("Memory & CPU", "Pamięć i procesor"), ("RAM Reserved for Other Applications", "Pamięć RAM zarezerwowana dla innych aplikacji"), - ("Cache Budgets", "Budżety pamięci podręcznej"), - ("Layer Cache", "Pamięć warstw"), - ("Footage Frame Cache", "Pamięć klatek materiałów"), - ("Preview (RAM) Cache", "Pamięć podglądu (RAM)"), + ("EffectCraft may use: {} of {}", "EffectCraft może użyć: {} z {}"), + ("EffectCraft may use: {} (installed RAM unknown)", "EffectCraft może użyć: {} (zainstalowana pamięć RAM nieznana)"), ("Reduce Cache Size When System Is Low on Memory", "Zmniejsz pamięć podręczną, gdy w systemie brakuje pamięci"), ("Video", "Wideo"), ("Enable Video Preview Output", "Włącz wyjście podglądu wideo"), @@ -8786,10 +8774,8 @@ const GEORGIAN: &[(&str, &str)] = &[ ("Folders or drives, separated by ;", "საქაღალდეები ან დისკები, გამოყოფილი ;-ით"), ("Memory & CPU", "მეხსიერება და CPU"), ("RAM Reserved for Other Applications", "სხვა აპლიკაციებისთვის დაჯავშნული RAM"), - ("Cache Budgets", "ქეშის ლიმიტები"), - ("Layer Cache", "შრეების ქეში"), - ("Footage Frame Cache", "ვიდეომასალის კადრების ქეში"), - ("Preview (RAM) Cache", "წინასწარი ნახვის (RAM) ქეში"), + ("EffectCraft may use: {} of {}", "EffectCraft-ს შეუძლია გამოიყენოს: {} {}-დან"), + ("EffectCraft may use: {} (installed RAM unknown)", "EffectCraft-ს შეუძლია გამოიყენოს: {} (დაყენებული RAM უცნობია)"), ("Reduce Cache Size When System Is Low on Memory", "ქეშის ზომის შემცირება, როცა სისტემას მეხსიერება აკლია"), ("Video", "ვიდეო"), ("Enable Video Preview Output", "ვიდეოს წინასწარი ნახვის გამოტანის ჩართვა"), @@ -9779,10 +9765,8 @@ const FRENCH: &[(&str, &str)] = &[ ("Folders or drives, separated by ;", "Dossiers ou lecteurs, séparés par\u{a0};"), ("Memory & CPU", "Mémoire et CPU"), ("RAM Reserved for Other Applications", "Mémoire vive réservée aux autres applications"), - ("Cache Budgets", "Budgets de cache"), - ("Layer Cache", "Cache des calques"), - ("Footage Frame Cache", "Cache des images du métrage"), - ("Preview (RAM) Cache", "Cache de prévisualisation (RAM)"), + ("EffectCraft may use: {} of {}", "EffectCraft peut utiliser : {} sur {}"), + ("EffectCraft may use: {} (installed RAM unknown)", "EffectCraft peut utiliser : {} (mémoire vive installée inconnue)"), ("Reduce Cache Size When System Is Low on Memory", "Réduire la taille du cache quand le système manque de mémoire"), ("Video", "Vidéo"), ("Enable Video Preview Output", "Activer la sortie de prévisualisation vidéo"), diff --git a/crates/ui-egui/src/panels/settings.rs b/crates/ui-egui/src/panels/settings.rs index a77228edd..ba9af77f1 100644 --- a/crates/ui-egui/src/panels/settings.rs +++ b/crates/ui-egui/src/panels/settings.rs @@ -73,6 +73,7 @@ fn page_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, page: &Page, cur: &Value } Item::Labels => labels_ui(app, ui, cur, acts), Item::BrowserStorage => browser_storage_ui(app, ui, t, acts), + Item::MemoryUse => memory_use_ui(app, ui, t), Item::Models(task) => models_ui(app, ui, t, acts, task), Item::PluginLibrary => library_ui(app, ui, t, acts), Item::AudioDevices { key } => { @@ -296,6 +297,26 @@ fn localize_status_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, t: &Tokens, s app.auto.add("settings.disk.localizeStatus", r.rect, &label); } +/// Settings ▸ Memory & CPU, under RAM Reserved for Other Applications: the memory EffectCraft's +/// caches may use (one budget they share, `Session::memory_budget`), of the installed RAM. +/// Automation id `settings.memory.use`, labelled `budgetMb=… installedMb=… reduced=…` (the same +/// in every interface language; `installedMb=unknown` without a reading). +fn memory_use_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, t: &Tokens) { + let b = app.session.memory_budget(); + let r = ui.label(RichText::new(memory_use_line(b.bytes as u64, b.installed)).color(t.text_dim)); + let installed = b.installed.map_or_else(|| "unknown".to_string(), |i| (i >> 20).to_string()); + app.auto.add("settings.memory.use", r.rect, &format!("budgetMb={} installedMb={installed} reduced={}", b.bytes >> 20, b.reduced)); +} + +/// The line [`memory_use_ui`] shows for a budget of `budget` bytes of `installed` bytes of RAM. +pub(crate) fn memory_use_line(budget: u64, installed: Option) -> String { + let gb = |b: u64| format!("{:.1} GB", b as f64 / (1u64 << 30) as f64); + match installed { + Some(i) => tr_args("EffectCraft may use: {} of {}", &[&gb(budget), &gb(i)]), + None => tr_args("EffectCraft may use: {} (installed RAM unknown)", &[&gb(budget)]), + } +} + /// Settings ▸ Disk, under Maximum Disk Cache Size: the limit in effect, and why it is lower when /// free disk space holds the cache below the setting (it never fills the disk). Automation id /// `settings.disk.cacheLimit`, labelled `limitMb=… maxMb=… reserveMb=… spaceLimited=… writes=…` @@ -1325,6 +1346,29 @@ mod tests { assert!(actions.is_empty()); } + /// Settings ▸ Memory & CPU shows what EffectCraft's caches may use, of the installed RAM + /// (#3298: one budget instead of three cache sizes), machine-readable for agents. + #[test] + fn the_memory_page_shows_what_the_caches_may_use() { + assert_eq!(memory_use_line((464u64 << 30) / 10, Some(64 << 30)), "EffectCraft may use: 46.4 GB of 64.0 GB"); + assert_eq!(memory_use_line(5 << 30, None), "EffectCraft may use: 5.0 GB (installed RAM unknown)"); + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + app.session.sys_memory = Some(effectcraft_engine::sysinfo::SysMemory { total: 64 << 30, available: 60 << 30 }); + let page = pages().into_iter().find(|p| p.id == "memory").unwrap(); + let ctx = egui::Context::default(); + let tokens = app.tokens; + let cur = serde_json::to_value(&app.session.prefs).unwrap(); + let mut actions = vec![]; + let mut output = ctx + .run_ui(egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(900.0, 900.0))), ..Default::default() }, |ui| { + page_ui(&mut app, ui, &page, &cur, &tokens, &mut actions) + }); + output.textures_delta.clear(); + let budget = (58u64 << 30) * 80 / 100; + assert_eq!(app.auto.find("settings.memory.use").unwrap().label, format!("budgetMb={} installedMb=65536 reduced=false", budget >> 20)); + assert!(actions.is_empty()); + } + /// Settings ▸ Disk shows the disk cache's limit in effect under Maximum Disk Cache Size, and /// says when free disk space holds it lower (a 20 GB cache held at 0 showed nothing). #[test] diff --git a/docs/architecture.md b/docs/architecture.md index 83c03f14f..0b97c1786 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -208,8 +208,9 @@ and undo can reuse earlier content. During rapid edits at the same time and view shows the newest completed revision while the exact requested revision renders; out-of-order completions cannot move it backwards. This avoids freezing until a slider or layer drag stops. Frames at other times or with other render options are never substituted. Obsolete queued jobs -are dropped; running jobs finish into the cache. When the budget (Settings ▸ Memory & CPU) is -full the least recently shown frames go first. The timeline's green bar counts cached frames +are dropped; running jobs finish into the cache. The frames share one memory budget with +the other big caches (Settings ▸ Memory & CPU); when it is full the memory accountant frees the +least valuable data first, by cost to make again per byte and time since last use. The timeline's green bar counts cached frames of the current content and viewer settings. Resolution Auto renders the pixels the magnification needs, as in After Effects (Full above 50 %, Half down to 33.3 %, Third down to 25 %, then Quarter). The Resolution is the comp's diff --git a/docs/preferences.md b/docs/preferences.md index 82dcafb6d..16c11bd2e 100644 --- a/docs/preferences.md +++ b/docs/preferences.md @@ -79,9 +79,6 @@ Commands (CLI, MCP, control channel): - `audio.outputDevice`: Default Output (device list from the desktop host) - `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.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, the timeline, project panel, render queue and viewer handles @@ -162,9 +159,30 @@ Commands (CLI, MCP, control channel): mapped to a share: `P:\`). The copies take at most `disk.localizeMaxGb` and leave the disk cache's reserve free on their disk; earlier sessions' copies are adopted. The Disk page shows the local copies, what is queued and being copied, and the network locations -- `memory.ramReservedGb`: the cache budgets together leave this much physical memory free -- `memory.reduceCacheWhenLow`: cache budgets halve while the system is low on memory (checked - every 30 s by the desktop app, on a background thread) +- `memory.ramReservedGb`: RAM Reserved for Other Applications, the one memory setting (After + Effects has a setting of this name too). EffectCraft's big caches (processed layers, decoded + footage frames, decoded OpenEXR planes, RAM preview frames, and the disk cache's write queue) + share one budget: 80 % of the installed RAM left after the reserve. The other 20 % is room for + EffectCraft's own working memory (decoders, render buffers, undo, GPU staging, the interface). + With the default 6 GB reserved that is 8.0 GB on a 16 GB machine, 46.4 GB on 64 GB and 97.6 GB + on 128 GB. The budget is at least 192 MB for all caches together; while the installed RAM is + unknown (a sandbox, the CLI, or the desktop before its first reading) it is 5 GB, in the browser 3.5 GB + (with the RAM preview at most 3 GB there). No cache has a share of its own: when they + hold more than the budget together, the memory accountant frees the least valuable data first, + whichever cache holds it, down to 90 % of the budget. An item's value is what it would cost to + make again (its measured render or decode time; when not measured, a typical 4 ms per MB for a + layer, 8 ms for a preview frame, 2 ms for footage frames and planes) per byte, halved after + 30 s unused, a quarter after 90 s, and so on; a copy of data another cache holds (a queued disk write) is worth + nothing, and a cache that had to make again what it let go of shortly before is protected for + a while. In the browser the layer cache also stays within 512 MB and the footage frames within + 256 MB (see [web.md](web.md)). The Memory & CPU page shows what EffectCraft may use, of the + installed RAM. Settings saved before this (version 4 and earlier) had Layer Cache, Footage + Frame Cache and Preview (RAM) Cache budgets (`memory.layerCacheMb`, `memory.mediaCacheMb`, + `memory.previewCacheMb`); they are dropped when the settings are read +- `memory.reduceCacheWhenLow`: the budget halves while the system is low on memory (less than + 10 % of the RAM, or less than 1 GB, available; checked every 30 s by the desktop app, on a + background thread). It stays halved until twice that much is available again, so freeing the + caches doesn't end the episode at once, and the toast saying so shows once per episode - `video.enableOutput`, `video.device`, `video.outputDuringPlayback`, `video.mirrorOnMonitor`, `video.disableWhenBackground`: Video Preview, a second window (or full screen on the display it is on) showing the composition frame, Mercury Transmit-style diff --git a/docs/web.md b/docs/web.md index 666ffe37b..6c8fe6d37 100644 --- a/docs/web.md +++ b/docs/web.md @@ -61,7 +61,7 @@ device of their own), `?nosw` (don't register the service worker). | config directory: settings, shortcut presets, recent projects, the recovery sentinel | `store::WebConfig` in browser storage | | auto-save folder | `/EffectCraft Auto-Save/` in browser storage (`ConfigStore::files`) | | media reads (`MediaPool`, `probe`) | `files::WebImporter`: bytes from the file table, handed to `MediaPool::add_bytes` / `probe_bytes` | -| Memory & CPU cache budgets, scaled to the system's RAM | the page's memory, at most 4 GiB (wasm32), which the caches share with the decoders' own decoded frames (FilmCraft keeps up to 384 MB per open movie): the Layer Cache stays within 512 MB and the Footage Frame Cache within 256 MB (`Prefs::cache_budgets`) | +| One cache budget from the system's RAM | the page's memory, at most 4 GiB (wasm32), which the caches share with the decoders' own decoded frames (FilmCraft keeps up to 384 MB per open movie): the installed RAM is unknown, so the caches share 3.5 GB, and within that the RAM preview stays within 3 GB, the layer cache within 512 MB and the footage frames within 256 MB (`Prefs::memory_budget`, `MemoryBudget::caps`) | | export writes (`effectcraft-export`) | the job's `sink` (`effectcraft_host::FileExporter { sink }`): files land in the table and download after the render; several files (an image sequence) download as one stored `.zip` | | rfd file dialogs | `` for File ▸ Open / Import; drop files anywhere on the page (`.ecproj` opens, everything else imports); "Save As" / "Output To" pick a download name | | Media Browser on the file system | browser storage and folders opened with the File System Access API (below) |