diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index 63a110b8fce..5b437d2014b 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -569,25 +569,29 @@ pub const DEFAULT_MEMORY_USAGE_MB: u32 = 8192; /// The most Memory Usage the web build honours, in MB: a wasm module's linear memory tops out at /// 4 GiB, so a larger allowance (the desktop default is 8 GiB) would never trim anything. The -/// effect cache budget is capped the same way, or the cache could grow until allocation fails. +/// effect cache is capped at it on every 32-bit build ([`SMALL_ADDRESS_SPACE`]). const WASM_MEMORY_USAGE_CAP_MB: u32 = 1536; +/// A 32-bit address space (wasm32, 32-bit Windows): an effect cache budget beyond +/// [`WASM_MEMORY_USAGE_CAP_MB`] would never evict before allocation fails. +const SMALL_ADDRESS_SPACE: bool = cfg!(target_pointer_width = "32"); + /// The Memory Usage a build honours for a `stored` preference: capped on the web build (`web`). /// Separate from the target check so native tests exercise both. fn honoured_memory_usage_mb(stored: u32, web: bool) -> u32 { if web { stored.min(WASM_MEMORY_USAGE_CAP_MB) } else { stored } } -/// The largest Effect Cache the Preferences dialog and `prefs.set` accept, in MB: the web build's -/// cap there, so the field shows the budget the cache really gets. -fn effect_cache_max_mb(web: bool) -> u32 { - if web { WASM_MEMORY_USAGE_CAP_MB } else { 65536 } +/// 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: capped on -/// the web build (`web`), and saturating rather than wrapping a 32-bit `usize`. -fn effect_cache_budget_bytes_for(stored: u32, web: bool) -> usize { - (honoured_memory_usage_mb(stored, web) as usize).saturating_mul(1 << 20) +/// 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. +fn effect_cache_budget_bytes_for(stored: u32, small: bool) -> u64 { + u64::from(stored.min(effect_cache_max_mb(small))) << 20 } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -680,9 +684,9 @@ impl Performance { } /// The layer-effect cache budget the build honours, in bytes: [`Performance::effect_cache_mb`], - /// capped on the web like Memory Usage. + /// capped at 1536 MB on 32-bit builds (the web and 32-bit Windows). pub fn effect_cache_budget_bytes(&self) -> usize { - effect_cache_budget_bytes_for(self.effect_cache_mb, cfg!(target_arch = "wasm32")) + usize::try_from(effect_cache_budget_bytes_for(self.effect_cache_mb, SMALL_ADDRESS_SPACE)).unwrap_or(usize::MAX) } } diff --git a/crates/engine/src/prefs/tests.rs b/crates/engine/src/prefs/tests.rs index 8004022cb36..c3f3fd3156d 100644 --- a/crates/engine/src/prefs/tests.rs +++ b/crates/engine/src/prefs/tests.rs @@ -1082,40 +1082,38 @@ fn the_web_build_caps_memory_usage() { } } -/// The effect cache budget is the stored MB in bytes, capped on the web like Memory Usage, and -/// never wraps: on wasm32 an uncapped 4096 MB shifted into `usize` was 0. Both builds are checked -/// on every target. +/// 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`. #[test] -fn the_effect_cache_budget_is_capped_on_the_web_and_never_wraps() { - const MB: usize = 1 << 20; - for (stored, web_mb) in [ - (16, 16), - (768, 768), - (WASM_MEMORY_USAGE_CAP_MB, WASM_MEMORY_USAGE_CAP_MB), - (WASM_MEMORY_USAGE_CAP_MB + 1, WASM_MEMORY_USAGE_CAP_MB), - (4096, WASM_MEMORY_USAGE_CAP_MB), - (u32::MAX, WASM_MEMORY_USAGE_CAP_MB), - ] { - assert_eq!(effect_cache_budget_bytes_for(stored, true), web_mb as usize * MB, "web, {stored} MB"); - assert_eq!(effect_cache_budget_bytes_for(stored, false), (stored as usize).saturating_mul(MB), "desktop, {stored} MB"); +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)] + { + 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"); } let p = Performance { effect_cache_mb: 4096, ..Performance::default() }; - assert_eq!(p.effect_cache_budget_bytes(), effect_cache_budget_bytes_for(4096, cfg!(target_arch = "wasm32"))); + let expected = effect_cache_budget_bytes_for(4096, cfg!(target_pointer_width = "32")); + assert_eq!(u64::try_from(p.effect_cache_budget_bytes()).ok(), Some(expected), "fits this target's usize"); } -/// The Effect Cache field accepts only what the build honours: up to the web cap on the web, so -/// the dialog never shows a value the cache doesn't get. +/// The Effect Cache field accepts only what the build honours, so the dialog never shows a value +/// the cache doesn't get. #[test] fn the_effect_cache_range_matches_the_honoured_budget() { assert_eq!(effect_cache_max_mb(true), WASM_MEMORY_USAGE_CAP_MB); assert_eq!(effect_cache_max_mb(false), 65536); - for web in [true, false] { - let max = effect_cache_max_mb(web); - assert_eq!(effect_cache_budget_bytes_for(max, web), max as usize * (1 << 20), "web = {web}: the largest accepted value is honoured in full"); + for small in [true, false] { + 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"); } - let web = cfg!(target_arch = "wasm32"); - assert_eq!(range("performance.effectCacheMb"), Some((16.0, f64::from(effect_cache_max_mb(web))))); - assert!(check_value("performance.effectCacheMb", &json!(effect_cache_max_mb(web))).is_ok()); - assert!(check_value("performance.effectCacheMb", &json!(effect_cache_max_mb(web) + 1)).is_err()); - assert!(u64::from(Performance::default().effect_cache_mb) <= u64::from(effect_cache_max_mb(true)), "the default fits the web cap"); + let small = cfg!(target_pointer_width = "32"); + assert_eq!(range("performance.effectCacheMb"), Some((16.0, 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 145cd635281..20848094d87 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(cfg!(target_arch = "wasm32")))), + "performance.effectCacheMb" => (16.0, 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),