From 52061b102904a323ca536ec5ee32b277afb5c8bc Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 1 Nov 2026 06:52:08 -0800 Subject: [PATCH] Undo: tests for the default history budget with no memory reading, 0, small and large memory history_budget_for takes the memory reading itself, so the 1 GiB fallback for no reading or 0 is tested with the quarter, the 64 MiB floor and a reading of 2 GiB, as a cgroup limit lower than the installed memory gives. The budget is unchanged. --- crates/engine/src/history.rs | 11 ++++++----- crates/engine/src/tests/history_mem.rs | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/history.rs b/crates/engine/src/history.rs index 3f981f531..ea07ec8cc 100644 --- a/crates/engine/src/history.rs +++ b/crates/engine/src/history.rs @@ -154,11 +154,12 @@ pub fn default_history_budget() -> usize { return 256 << 20; } static BUDGET: std::sync::OnceLock = std::sync::OnceLock::new(); - *BUDGET.get_or_init(|| vectorcraft_render::physical_memory().filter(|&m| m > 0).map_or(1 << 30, history_budget_for)) + *BUDGET.get_or_init(|| history_budget_for(vectorcraft_render::physical_memory())) } -/// The default [`History::byte_budget`] on a machine with `memory` bytes installed: a quarter of it, -/// at least 64 MiB, and at most a quarter of the address space (on 32-bit builds). -pub(crate) fn history_budget_for(memory: u64) -> usize { - usize::try_from(memory / 4).unwrap_or(usize::MAX).clamp(64 << 20, usize::MAX / 4) +/// The default [`History::byte_budget`] for a reading of `memory` bytes installed: a quarter of it, +/// at least 64 MiB, and at most a quarter of the address space (on 32-bit builds); 1 GiB when the +/// reading is `None` or 0. +pub(crate) fn history_budget_for(memory: Option) -> usize { + memory.filter(|&m| m > 0).map_or(1 << 30, |m| usize::try_from(m / 4).unwrap_or(usize::MAX).clamp(64 << 20, usize::MAX / 4)) } diff --git a/crates/engine/src/tests/history_mem.rs b/crates/engine/src/tests/history_mem.rs index ac1a29e17..80e005929 100644 --- a/crates/engine/src/tests/history_mem.rs +++ b/crates/engine/src/tests/history_mem.rs @@ -78,18 +78,24 @@ fn within_the_budget_the_step_limit_still_counts() { #[test] #[cfg(target_pointer_width = "64")] fn the_default_budget_is_a_quarter_of_installed_memory() { - assert_eq!(history_budget_for(16 << 30), 4 << 30); - assert_eq!(history_budget_for(64 << 30), 16 << 30, "no fixed ceiling"); - assert_eq!(history_budget_for(2 << 30), 512 << 20); - assert_eq!(history_budget_for(128 << 20), 64 << 20, "at least 64 MiB"); + assert_eq!(history_budget_for(Some(16 << 30)), 4 << 30); + assert_eq!(history_budget_for(Some(64 << 30)), 16 << 30, "no fixed ceiling"); + // On Linux `physical_memory` returns a cgroup memory limit lower than the installed memory in + // its place (the `cgroup_limit_*` tests in vectorcraft-render), so a 2 GiB limit on a 64 GiB + // machine gives the budget of 2 GiB installed. + assert_eq!(history_budget_for(Some(2 << 30)), 512 << 20, "2 GiB, or a 2 GiB cgroup limit"); + assert_eq!(history_budget_for(Some(128 << 20)), 64 << 20, "at least 64 MiB"); + assert_eq!(history_budget_for(Some(1)), 64 << 20, "at least 64 MiB"); + assert_eq!(history_budget_for(Some(0)), 1 << 30, "a reading of 0 is the 1 GiB fallback"); + assert_eq!(history_budget_for(None), 1 << 30, "no reading is the 1 GiB fallback"); } #[test] #[cfg(any(target_os = "linux", windows, target_os = "macos", target_os = "freebsd"))] fn the_default_budget_follows_the_installed_memory_read() { // Where the memory can't be read (a sandbox that hides it), the budget is the 1 GiB fallback. - let memory = vectorcraft_render::physical_memory().filter(|&m| m > 0); - assert_eq!(default_history_budget(), memory.map_or(1 << 30, history_budget_for), "{memory:?}"); + let memory = vectorcraft_render::physical_memory(); + assert_eq!(default_history_budget(), history_budget_for(memory), "{memory:?}"); assert_eq!(History::default().byte_budget, default_history_budget()); }