diff --git a/crates/engine-core/src/lib.rs b/crates/engine-core/src/lib.rs index d3aa9a715a..2e4ee4c413 100644 --- a/crates/engine-core/src/lib.rs +++ b/crates/engine-core/src/lib.rs @@ -309,6 +309,9 @@ pub struct Session { next_pending_preflight: u64, /// Shared native memory supervision, owned and stopped by the last session using it. scratch_runtime: Option, + /// The scratch disk and memory settings [`Session::apply_prefs`] last configured: an edit of + /// other preferences configures nothing again. + scratch_applied: Option, /// [`photocraft_raster::spill::epoch`] when History's byte limits were last applied: a /// scratch disk that pauses or resumes changes the limit ([`Session::sync_scratch_state`]). scratch_epoch: u64, diff --git a/crates/engine-core/src/prefs.rs b/crates/engine-core/src/prefs.rs index 76d893ed93..8d56154bb3 100644 --- a/crates/engine-core/src/prefs.rs +++ b/crates/engine-core/src/prefs.rs @@ -1902,7 +1902,11 @@ impl Session { /// missing-glyph protection. pub fn apply_prefs(&mut self) { self.scratch_runtime.get_or_insert_with(crate::scratch::Runtime::default); - crate::scratch::configure(self.prefs()); + let scratch = crate::scratch::Settings::of(self.prefs()); + if self.scratch_applied.as_ref() != Some(&scratch) { + crate::scratch::configure(&scratch); + self.scratch_applied = Some(scratch); + } self.scratch_epoch = photocraft_raster::spill::epoch(); let n = self.prefs().performance.history_states.max(1) as usize; let bytes = self.prefs().performance.history_cap_bytes(); diff --git a/crates/engine-core/src/scratch.rs b/crates/engine-core/src/scratch.rs index 9133f76f03..6c46f15883 100644 --- a/crates/engine-core/src/scratch.rs +++ b/crates/engine-core/src/scratch.rs @@ -129,42 +129,101 @@ mod native { // If configure uses this query even once, it pauses for lack of room. This catches an // override that is missing or is installed after configuring the scratch directory. photocraft_raster::spill::set_volume_space_for_tests(|_| Some((0, 64 << 30))); - super::super::configure(&crate::prefs::Preferences::default()); + super::super::configure(&super::super::Settings::of(&crate::prefs::Preferences::default())); assert!(!photocraft_raster::spill::paused()); let room = (32_u64 << 30) - (64_u64 << 30) / 20; assert_eq!(photocraft_raster::spill::scratch_capacity(), Some(room - room / 64)); } + + /// A preference edit that changes neither Memory Usage nor the scratch disks configures + /// neither again: a dragged dialog remembers its place through a preference edit every + /// frame, and configuring took milliseconds each time. + #[test] + fn unrelated_preference_edits_leave_the_scratch_disk_alone() { + const CHILD: &str = "PHOTOCRAFT_SCRATCH_UNRELATED_EDIT_TEST_CHILD"; + if std::env::var_os(CHILD).is_none() { + let status = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "scratch::native::tests::unrelated_preference_edits_leave_the_scratch_disk_alone"]) + .env(CHILD, "1") + .status() + .unwrap(); + assert!(status.success(), "isolated scratch test failed: {status}"); + return; + } + + let mut session = crate::Session::new(); + // As at startup (`prefs_ui::startup`). + session.apply_prefs(); + let configured = photocraft_raster::memory::configuration_generation; + let before = configured(); + session.edit_prefs(|p| { + p.sections.insert("photocraft.test.window".into(), serde_json::json!({"x": 1})); + }); + assert_eq!(configured(), before, "a section edit configured the scratch disk again"); + session.edit_prefs(|p| p.performance.memory_usage_percent = Some(p.performance.memory_usage_percent.unwrap_or(70) - 10)); + assert!(configured() > before, "a Memory Usage edit must reach the memory budgets"); + } } } -pub(crate) fn configure(prefs: &Preferences) { +/// What [`configure`] reads from the preferences. An edit that leaves it the same leaves the +/// scratch disk and the memory budgets as they are ([`crate::Session::apply_prefs`]): configuring +/// them again re-measures memory, probes the scratch folder and restarts a paused disk's backoff, +/// which takes milliseconds, and preference edits come once per frame while a remembered window +/// is dragged. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct Settings { + percent: Option, + absolute_mb: u32, + /// The first enabled scratch disk. + #[cfg(not(target_arch = "wasm32"))] + dir: Option, + #[cfg(not(target_arch = "wasm32"))] + size_limit_mb: u32, + /// The tile budget's override (`PHOTOCRAFT_TILE_BUDGET_MB`), when set. + #[cfg(not(target_arch = "wasm32"))] + tile_override: Option, +} + +impl Settings { + pub(crate) fn of(prefs: &Preferences) -> Self { + Settings { + percent: prefs.performance.memory_usage_percent, + // The web build's cap applies here too (effective_memory_usage_mb). + absolute_mb: prefs.performance.effective_memory_usage_mb(), + #[cfg(not(target_arch = "wasm32"))] + dir: prefs + .scratch_disks + .disks + .iter() + .find(|d| d.enabled && !d.path.trim().is_empty()) + .map(|d| if d.path.trim() == "(system temp)" { std::env::temp_dir() } else { std::path::PathBuf::from(d.path.trim()) }), + #[cfg(not(target_arch = "wasm32"))] + size_limit_mb: prefs.scratch_disks.size_limit_mb, + #[cfg(not(target_arch = "wasm32"))] + tile_override: std::env::var_os("PHOTOCRAFT_TILE_BUDGET_MB").is_some().then(|| prefs.performance.tile_budget_bytes()), + } + } +} + +/// Configures the memory budgets and the scratch disk as `settings` say. +pub(crate) fn configure(settings: &Settings) { #[cfg(all(test, not(target_arch = "wasm32")))] photocraft_raster::spill::use_test_volume_space(); let budget = photocraft_raster::memory::configure(photocraft_raster::memory::Policy { - percent: prefs.performance.memory_usage_percent, - // The web build's cap applies here too (effective_memory_usage_mb). - absolute_bytes: u64::from(prefs.performance.effective_memory_usage_mb()).saturating_mul(1 << 20), + percent: settings.percent, + absolute_bytes: u64::from(settings.absolute_mb).saturating_mul(1 << 20), }); #[cfg(not(target_arch = "wasm32"))] { - let dir = prefs - .scratch_disks - .disks - .iter() - .find(|d| d.enabled && !d.path.trim().is_empty()) - .map(|d| if d.path.trim() == "(system temp)" { std::env::temp_dir() } else { std::path::PathBuf::from(d.path.trim()) }); // An unusable or full directory pauses eviction without dropping resident pixels and // is retried with backoff; History's byte limit applies meanwhile (sync_scratch_state). // The reason is exposed in ui.inspect and the application's status bar. - let tiles = if std::env::var_os("PHOTOCRAFT_TILE_BUDGET_MB").is_some() { - prefs.performance.tile_budget_bytes() - } else { - usize::try_from(budget.tile_bytes).unwrap_or(usize::MAX) - }; - let limit = prefs.scratch_disks.size_limit_mb; + let tiles = settings.tile_override.unwrap_or_else(|| usize::try_from(budget.tile_bytes).unwrap_or(usize::MAX)); + let limit = settings.size_limit_mb; photocraft_raster::spill::set_size_cap((limit > 0).then(|| u64::from(limit).saturating_mul(1 << 20))); - let _ = photocraft_raster::spill::configure(dir.as_deref(), tiles); + let _ = photocraft_raster::spill::configure(settings.dir.as_deref(), tiles); } #[cfg(target_arch = "wasm32")] - let _ = (prefs, budget); + let _ = budget; }