From 321a4bc1d7cf288fbef3a6ddca9cc0e43ecd9347 Mon Sep 17 00:00:00 2001 From: Kevin <98597979+KevinPratap@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:23:05 +0530 Subject: [PATCH] GPU: a pipeline library save the host has no memory for waits, not aborts (#2007) The library is serialised into one buffer of its whole size, allocated infallibly. On NVIDIA the library holds each pipeline's machine code, about 1.7 GB after translate --cache --pipelines, and a save in the world aborted the process: 'memory allocation of 2650101862 bytes failed' (RTX 4050 Laptop, 32 GB). The buffer is now reserved fallibly; without memory the save is left for later (the pipelines stay counted as added). Co-authored-by: Claude Opus 5.5 --- crates/victory-gpu/src/pipeline/library.rs | 30 +++++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/crates/victory-gpu/src/pipeline/library.rs b/crates/victory-gpu/src/pipeline/library.rs index 252e4dc..28ab46a 100644 --- a/crates/victory-gpu/src/pipeline/library.rs +++ b/crates/victory-gpu/src/pipeline/library.rs @@ -307,11 +307,15 @@ pub(crate) fn save(now: bool) { if l.added == 0 && !l.prebuilt_changed && !USED_CHANGED.swap(false, Ordering::Relaxed) { return; } - // SAFETY: a buffer of the size the library asks for. - let bytes = unsafe { - let mut bytes = vec![0u8; l.library.GetSerializedSize()]; - l.library.Serialize(&mut bytes).map(|()| bytes) + // SAFETY: a plain size query on a live library. + let size = unsafe { l.library.GetSerializedSize() }; + let Some(mut bytes) = zeroed(size) else { + // Left for a later save: the pipelines stay counted as added. + tracing::warn!(bytes = size, "no memory to serialise the host pipeline library; saved later"); + return; }; + // SAFETY: a buffer of the size the library asks for. + let bytes = unsafe { l.library.Serialize(&mut bytes) }.map(|()| bytes); match bytes { Ok(bytes) => { l.added = 0; @@ -354,6 +358,17 @@ pub(crate) fn save(now: bool) { } } +/// `n` zero bytes, or `None` when the host has no memory for them. The +/// library is about 1.7 GB on NVIDIA (each pipeline's machine code), and an +/// infallible allocation of that size aborted the whole process mid-run +/// ("memory allocation of 2650101862 bytes failed", RTX 4050 Laptop). +fn zeroed(n: usize) -> Option> { + let mut v = Vec::new(); + v.try_reserve_exact(n).ok()?; + v.resize(n, 0); + Some(v) +} + /// Writes `bytes` to a temporary file beside `path`, then renames it over /// `path`: a reader never sees a part. Serialised so concurrent worker and /// shutdown saves do not race on the same staging file or target rename. @@ -459,6 +474,13 @@ pub(crate) fn note_used(key: &str) { mod tests { use super::*; + /// A size the host cannot give is refused, not fatal; others are zeros. + #[test] + fn a_save_buffer_the_host_cannot_give_is_refused() { + assert!(zeroed(usize::MAX / 2).is_none()); + assert_eq!(zeroed(100).as_deref(), Some(&[0u8; 100][..])); + } + #[test] fn names_change_with_what_goes_into_the_pipeline() { let a = name("graphics", b"root", &[b"vs", b"ps"], "state");