From dafbff2b057e563c34903363e9bad3da155cebbf Mon Sep 17 00:00:00 2001 From: Khawar Zamman Wani Date: Thu, 29 Oct 2026 11:02:50 +0530 Subject: [PATCH] Test the web build's Memory Usage cap The cap is now a function of the stored value and the target, so native tests check values below, at and above it; the native-only assertions are gated to native targets. --- crates/engine/src/prefs.rs | 8 +++++++- crates/engine/src/prefs/tests.rs | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index f44c11c4a1..38dc5908f1 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -566,6 +566,12 @@ pub const DEFAULT_MEMORY_USAGE_MB: u32 = 8192; /// 4 GiB, so a larger allowance (the desktop default is 8 GiB) would never trim anything. const WASM_MEMORY_USAGE_CAP_MB: u32 = 1536; +/// 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 } +} + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] pub struct Performance { @@ -624,7 +630,7 @@ impl Performance { /// [`Performance::memory_usage_mb`] as the build can honour it: capped on the web, where the /// stored preference (a saved 8192 still loads) is larger than the module's address space. pub fn effective_memory_usage_mb(&self) -> u32 { - if cfg!(target_arch = "wasm32") { self.memory_usage_mb.min(WASM_MEMORY_USAGE_CAP_MB) } else { self.memory_usage_mb } + honoured_memory_usage_mb(self.memory_usage_mb, cfg!(target_arch = "wasm32")) } /// Pixel memory a document and its History may hold (Memory Usage), in bytes: beyond it diff --git a/crates/engine/src/prefs/tests.rs b/crates/engine/src/prefs/tests.rs index 020c41fdf5..fc96369ee4 100644 --- a/crates/engine/src/prefs/tests.rs +++ b/crates/engine/src/prefs/tests.rs @@ -1035,7 +1035,8 @@ fn a_wrongly_typed_section_or_colour_setting_resets_only_that_part() { } /// A saved Memory Usage is kept as stored, and means the same on desktop; only the web build -/// caps what it honours (the cap itself is exercised by `cargo xtask wasm` compiling it). +/// caps what it honours. +#[cfg(not(target_arch = "wasm32"))] #[test] fn the_history_budget_is_the_stored_memory_usage_on_native() { for mb in [0u32, 1, 1536, 8192, 65536, u32::MAX] { @@ -1048,3 +1049,19 @@ fn the_history_budget_is_the_stored_memory_usage_on_native() { assert_eq!(s.prefs().performance.memory_usage_mb, 8192); assert_eq!(s.prefs().performance.history_budget_bytes(), 8192 << 20); } + +/// The web build honours Memory Usage up to its cap: below and at it as stored, above it the cap. +#[test] +fn the_web_build_caps_memory_usage() { + for (stored, honoured) in [ + (0, 0), + (1024, 1024), + (WASM_MEMORY_USAGE_CAP_MB, WASM_MEMORY_USAGE_CAP_MB), + (WASM_MEMORY_USAGE_CAP_MB + 1, WASM_MEMORY_USAGE_CAP_MB), + (8192, WASM_MEMORY_USAGE_CAP_MB), + (u32::MAX, WASM_MEMORY_USAGE_CAP_MB), + ] { + assert_eq!(honoured_memory_usage_mb(stored, true), honoured, "web, {stored} MB"); + assert_eq!(honoured_memory_usage_mb(stored, false), stored, "desktop, {stored} MB"); + } +}