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.
This commit is contained in:
Khawar Zamman Wani committed 2026-10-29 11:02:50 +05:30
1 parent 0eaf976088
commit dafbff2b05
2 files changed
+25 -2

No files matched your search

+7 -1
View File
@@ -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
+18 -1
View File
@@ -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");
}
}