From 0c25e65bbda2f1ec4deb2ea4515af5f9e2fdb985 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Mon, 2 Nov 2026 21:44:57 +0100 Subject: [PATCH] Preferences: the effect cache budget honours the field's 16 MB minimum Review follow-up (CodeRabbit on #10921). A hand-edited effectCacheMb of 0 made Performance::effect_cache_budget_bytes report 0 bytes, though the cache floors it at 1 MiB. The budget now clamps to the range the field accepts (EFFECT_CACHE_MIN_MB..effect_cache_max_mb), and prefs::range takes its minimum from the same constant. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/prefs.rs | 10 +++++++--- crates/engine/src/prefs/tests.rs | 26 ++++++++++++++++++-------- crates/engine/src/prefs/validate.rs | 2 +- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index 5b437d2014..ca26a804ce 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -582,16 +582,20 @@ fn honoured_memory_usage_mb(stored: u32, web: bool) -> u32 { if web { stored.min(WASM_MEMORY_USAGE_CAP_MB) } else { stored } } +/// The smallest Effect Cache the Preferences dialog and `prefs.set` accept, in MB. +const EFFECT_CACHE_MIN_MB: u32 = 16; + /// The largest Effect Cache the Preferences dialog and `prefs.set` accept, in MB: on a build with /// a 32-bit address space (`small`) the cap there, so the field shows the budget the cache gets. fn effect_cache_max_mb(small: bool) -> u32 { if small { WASM_MEMORY_USAGE_CAP_MB } else { 65536 } } -/// The layer-effect cache budget a build honours for a `stored` preference, in bytes: at most -/// [`effect_cache_max_mb`]. Separate from the target check so native tests exercise both. +/// The layer-effect cache budget a build honours for a `stored` preference, in bytes: within the +/// range the field accepts, so a hand-edited 0 reports 16 MB, not 0. Separate from the target +/// check so native tests exercise both. fn effect_cache_budget_bytes_for(stored: u32, small: bool) -> u64 { - u64::from(stored.min(effect_cache_max_mb(small))) << 20 + u64::from(stored.clamp(EFFECT_CACHE_MIN_MB, effect_cache_max_mb(small))) << 20 } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] diff --git a/crates/engine/src/prefs/tests.rs b/crates/engine/src/prefs/tests.rs index c3f3fd3156..22091654e8 100644 --- a/crates/engine/src/prefs/tests.rs +++ b/crates/engine/src/prefs/tests.rs @@ -1082,17 +1082,26 @@ fn the_web_build_caps_memory_usage() { } } -/// The effect cache budget is the stored MB in bytes, at most 1536 MB on a 32-bit address space -/// (the web, 32-bit Windows) and 65536 MB elsewhere, and never wraps: on wasm32 an uncapped -/// 4096 MB shifted into `usize` was 0. Both kinds of build are checked on every target, in `u64` -/// so the expectations themselves can't overflow a 32-bit `usize`. +/// The effect cache budget is the stored MB in bytes, at least 16 MB (a hand-edited 0 included), +/// at most 1536 MB on a 32-bit address space (the web, 32-bit Windows) and 65536 MB elsewhere, +/// and never wraps: on wasm32 an uncapped 4096 MB shifted into `usize` was 0. Both kinds of +/// build are checked on every target, in `u64` so the expectations themselves can't overflow a +/// 32-bit `usize`. #[test] fn the_effect_cache_budget_is_capped_on_32_bit_builds_and_never_wraps() { const MB: u64 = 1 << 20; let cap = WASM_MEMORY_USAGE_CAP_MB; - for (stored, small_mb, large_mb) in - [(16, 16, 16), (768, 768, 768), (cap, cap, cap), (cap + 1, cap, cap + 1), (4096, cap, 4096), (65536, cap, 65536), (u32::MAX, cap, 65536)] - { + for (stored, small_mb, large_mb) in [ + (0, 16, 16), + (15, 16, 16), + (16, 16, 16), + (768, 768, 768), + (cap, cap, cap), + (cap + 1, cap, cap + 1), + (4096, cap, 4096), + (65536, cap, 65536), + (u32::MAX, cap, 65536), + ] { assert_eq!(effect_cache_budget_bytes_for(stored, true), u64::from(small_mb) * MB, "32-bit, {stored} MB"); assert_eq!(effect_cache_budget_bytes_for(stored, false), u64::from(large_mb) * MB, "64-bit, {stored} MB"); } @@ -1111,8 +1120,9 @@ fn the_effect_cache_range_matches_the_honoured_budget() { let max = effect_cache_max_mb(small); assert_eq!(effect_cache_budget_bytes_for(max, small), u64::from(max) << 20, "small = {small}: the largest accepted value is honoured"); } + assert_eq!(effect_cache_budget_bytes_for(EFFECT_CACHE_MIN_MB, false), u64::from(EFFECT_CACHE_MIN_MB) << 20, "the smallest accepted value is honoured"); let small = cfg!(target_pointer_width = "32"); - assert_eq!(range("performance.effectCacheMb"), Some((16.0, f64::from(effect_cache_max_mb(small))))); + assert_eq!(range("performance.effectCacheMb"), Some((f64::from(EFFECT_CACHE_MIN_MB), f64::from(effect_cache_max_mb(small))))); assert!(check_value("performance.effectCacheMb", &json!(effect_cache_max_mb(small))).is_ok()); assert!(check_value("performance.effectCacheMb", &json!(effect_cache_max_mb(small) + 1)).is_err()); assert!(Performance::default().effect_cache_mb <= effect_cache_max_mb(true), "the default fits the 32-bit cap"); diff --git a/crates/engine/src/prefs/validate.rs b/crates/engine/src/prefs/validate.rs index 20848094d8..5fb31253b1 100644 --- a/crates/engine/src/prefs/validate.rs +++ b/crates/engine/src/prefs/validate.rs @@ -122,7 +122,7 @@ pub fn range(path: &str) -> Option<(f64, f64)> { "performance.historyStates" => (1.0, 1000.0), "performance.cacheLevels" => (1.0, 8.0), "performance.cacheTileSize" => (256.0, 16384.0), - "performance.effectCacheMb" => (16.0, f64::from(super::effect_cache_max_mb(super::SMALL_ADDRESS_SPACE))), + "performance.effectCacheMb" => (f64::from(super::EFFECT_CACHE_MIN_MB), f64::from(super::effect_cache_max_mb(super::SMALL_ADDRESS_SPACE))), "transparencyAndGamut.gamutWarningOpacity" => (1.0, 100.0), "unitsAndRulers.columnWidth" | "unitsAndRulers.gutter" => (0.0, 10_000.0), "unitsAndRulers.printResolution" | "unitsAndRulers.screenResolution" | "rawDefaults.resolution" => (1.0, 10_000.0),