From a162ae7f9facefa6b23ed897da12a96b4dac9c40 Mon Sep 17 00:00:00 2001 From: harrisotiropoulos-jpg <360338094+harrisotiropoulos-jpg@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:14:28 +0200 Subject: [PATCH] Pipelines: the whole library is not saved again mid-burst or under memory pressure (stutters on a 32 GB PC) Every save serialises the whole host pipeline library (over a gigabyte once warm) and holds off every store while it does. Two cases made that a hitch: - In a burst of new pipelines (a new area, a cutscene) a worker saved it every 20 s, exactly while the title waited on those pipelines and the write competed with texture streaming for the disk. Saves in a burst now come at most every 180 s (VICTORY_LIBRARY_SAVE_BUSY_S; 20 as before). - Under host memory pressure the serialised library is written through the PC's memory and pushes the title's own pages out. On an RTX 4080 with 32 GB the layer ran pressured for a whole session (commit headroom 3.4 GB) and the title's threads stalled; the periodic save now waits 600 s while pressured. The save when the queue runs dry and the save at exit are unchanged, so nothing made is lost unless the process ends without one. Measured: release build, victory-gpu lib tests 414 passed; the two indirect::tests failures are the same on main (fixed by #67). Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/pipeline/library.rs | 43 +++++++++++++++++++++- crates/victory-gpu/src/pipeline/make.rs | 2 +- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/crates/victory-gpu/src/pipeline/library.rs b/crates/victory-gpu/src/pipeline/library.rs index f81db5772e..f778ffcc93 100644 --- a/crates/victory-gpu/src/pipeline/library.rs +++ b/crates/victory-gpu/src/pipeline/library.rs @@ -618,15 +618,56 @@ pub(crate) fn pipeline( Ok(pso) } +/// As [`save`], from a worker while more pipelines are still queued. +/// +/// Every save serialises the whole library (over a gigabyte once it is warm) +/// and, while it does, holds off every store; in the middle of a burst of new +/// pipelines (a new area, a cutscene) that is exactly when the title is +/// waiting on them, and the write competes with texture streaming for the +/// disk. Saves in a burst are kept to one every `VICTORY_LIBRARY_SAVE_BUSY_S` +/// seconds (default 180; 20 as before); the save when the queue runs dry and +/// the one at exit are as they were. +pub(crate) fn save_while_busy() { + static LAST: Mutex> = Mutex::new(None); + static EVERY: OnceLock = OnceLock::new(); + let every = *EVERY.get_or_init(|| { + let secs = std::env::var("VICTORY_LIBRARY_SAVE_BUSY_S").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(180); + std::time::Duration::from_secs(secs) + }); + { + let mut last = LAST.lock().unwrap_or_else(|e| e.into_inner()); + match *last { + // The first busy save of the run waits a full interval too. + None => { + *last = Some(std::time::Instant::now()); + return; + } + Some(t) if t.elapsed() < every => return, + _ => *last = Some(std::time::Instant::now()), + } + } + save(false); +} + /// Writes the library and the early list if pipelines were added since the /// last save and `SAVE_EVERY` has passed (or `now`). +/// +/// Under host memory pressure ([`crate::residency::pressured`]) the interval +/// is `SAVE_EVERY_PRESSURED` instead: the serialised library (over a gigabyte +/// once warm) is written through the PC's memory, and on a 32 GB PC that ran +/// pressured for a whole session (log of 2026-11-27, commit headroom 3.4 GB) +/// those pages pushed the title's own out, and its threads stalled on page +/// faults. The save at exit (`now`) is unchanged, so nothing made is lost +/// unless the process ends without one. pub(crate) fn save(now: bool) { static LAST: Mutex> = Mutex::new(None); const SAVE_EVERY: std::time::Duration = std::time::Duration::from_secs(20); + const SAVE_EVERY_PRESSURED: std::time::Duration = std::time::Duration::from_secs(600); let (Some(dir), Some(library)) = (DIR.get(), library()) else { return }; { let mut last = LAST.lock().unwrap_or_else(|e| e.into_inner()); - if !now && last.is_some_and(|t| t.elapsed() < SAVE_EVERY) { + let every = if crate::residency::pressured() { SAVE_EVERY_PRESSURED } else { SAVE_EVERY }; + if !now && last.is_some_and(|t| t.elapsed() < every) { return; } *last = Some(std::time::Instant::now()); diff --git a/crates/victory-gpu/src/pipeline/make.rs b/crates/victory-gpu/src/pipeline/make.rs index 9b7b095846..4598cc06bb 100644 --- a/crates/victory-gpu/src/pipeline/make.rs +++ b/crates/victory-gpu/src/pipeline/make.rs @@ -581,7 +581,7 @@ fn workers() -> bool { if WAITING.fetch_sub(1, Ordering::Relaxed) == 1 { drained(); } else { - super::library::save(false); + super::library::save_while_busy(); } } });