From 07103eb809fe52c06d3508372b8cc128ece2c4e1 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 11 Nov 2026 06:13:02 -0800 Subject: [PATCH] Runtime: a pool's blocks made ahead are summed in place, so a creation call's keep_room allocates no list of them Memory::keep_ahead, which a creation call's keep_room runs for a pool that grew lately or is near its next block, read the pool's blocks made, being made and still to make as a Vec of their sizes (memory/ready.rs's Readying::held) only to sum them. The heap's kept-block budget read the same Vec through outstanding_bytes. outstanding_bytes now sums them under the readying's lock, and both callers ask it. held is gone, and lacking takes the sum. Why: once host_ab settles the device's checks before its workloads, each of the RTX 5080's texture_256_shared calls found a pool growing (memory.rs's ready_if_growing) and read the list: one allocation a call. texture_256_shared allocs went from 25.312 to 26.312 a call in every run (bound 32). An allocation trace of the group's counted window (base against the settled tree, 2 runs each) named held <- keep_ahead <- ready_if_growing <- keep_room <- new_texture_now as the only site that differed, 1.000 a call. Verified (release host_ab against development 58c3147007): - RTX 5080 Laptop, 16 runs each: texture_256_shared allocs read 25.312 in every run, as at base (26.312 without this commit). Every row is the same as at base, apart from heap_texture_256 allocs (15.25 -> 12.25, the previous commit) and buffer_texture_256 (within its bound in 16 of 16 runs; base over it in 4 of 16). - lavapipe: texture_256_shared allocs 31.312 -> 29.312 (base 32.312); every row within its bound, 6 of 6 runs. - 780M PHOENIX: texture_256_shared 29.312 (base 30.312); no row moved up. - tests::memory, tests::host_memory, tests::host_image_room, tests::device_memory_overflow, tests::blocks_ahead and tests::zeroed_start: RTX 3090 56 passed (2 ignored), PHOENIX 56 passed (2 ignored), lavapipe 57 passed. - clippy (plain, --cfg m2v_gpu, --cfg m2v_cpu_icd) and rustdoc -D warnings clean on Linux. --- crates/metal2vulkan-runtime/src/memory.rs | 14 +++++-------- .../metal2vulkan-runtime/src/memory/ready.rs | 20 ++++++------------- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index 77762717b..ed058beba 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -2713,16 +2713,15 @@ impl Memory { } /// The block `pool`, its blocks of `bytes`, lacks of the one it keeps made ahead - /// (`memory/ready.rs`), `held` the bytes of each block made, being made and still to make: + /// (`memory/ready.rs`), `held` the bytes of the blocks made, being made and still to make: /// counted in bytes together with its room ([`Pool::room`]), one where those held and its /// room come to less than a block. So a block just taken from them, which leaves the pool a /// whole block of room, is made again as soon as a placement takes any of that room, not only /// once the pool lacks room for a whole block: the room left then is less than the next /// creation calls' placements may take before the making ends (a 31.6 MiB texture after a /// 64 MiB block was taken). - fn lacking(&mut self, pool: (Holds, u32), bytes: u64, held: &[u64]) -> Vec { - let have: u64 = held.iter().sum(); - match self.pool(pool.0, pool.1).room().saturating_add(have) < bytes { + fn lacking(&mut self, pool: (Holds, u32), bytes: u64, held: u64) -> Vec { + match self.pool(pool.0, pool.1).room().saturating_add(held) < bytes { true => vec![bytes], false => Vec::new(), } @@ -2743,11 +2742,8 @@ impl Memory { /// [`lacking`](Self::lacking)): one it lacks made after the blocks its thread still makes, or /// on a new thread, once a burst of the device's queue uses ended. fn keep_ahead(&mut self, pool: (Holds, u32), bytes: u64) { - let held = match self.readying_at(pool) { - Some(at) => self.readying[at].held(), - None => Vec::new(), - }; - let lacking = self.lacking(pool, bytes, &held); + let held = (self.readying_at(pool)).map_or(0, |at| self.readying[at].outstanding_bytes()); + let lacking = self.lacking(pool, bytes, held); if lacking.is_empty() { return; } diff --git a/crates/metal2vulkan-runtime/src/memory/ready.rs b/crates/metal2vulkan-runtime/src/memory/ready.rs index 9121e4c60..3f5937af7 100644 --- a/crates/metal2vulkan-runtime/src/memory/ready.rs +++ b/crates/metal2vulkan-runtime/src/memory/ready.rs @@ -362,16 +362,6 @@ impl Readying { lock(&self.shared.state).will_hold(least) } - /// The bytes of each of its blocks made and not yet added, being made, and still to make. - pub(super) fn held(&self) -> Vec { - let state = lock(&self.shared.state); - let made = state.made.iter().map(|made| made.block.size); - (made - .chain(state.making) - .chain(state.to_make.iter().copied())) - .collect() - } - /// The bytes of each of its blocks made and not yet added, for the crate's device tests. #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] pub(super) fn made(&self) -> Vec { @@ -379,11 +369,13 @@ impl Readying { state.made.iter().map(|made| made.block.size).collect() } - /// What it holds against its heap's budget of kept empty blocks: the bytes of [`held`]. - /// - /// [`held`]: Self::held + /// What it holds against its heap's budget of kept empty blocks: the bytes of its blocks made + /// and not yet added, being made, and still to make. Summed under its lock, no list of them + /// made: every creation call that keeps room in its pool asks it (`Memory::keep_ahead`). pub(super) fn outstanding_bytes(&self) -> u64 { - self.held().iter().sum() + let state = lock(&self.shared.state); + let made: u64 = state.made.iter().map(|made| made.block.size).sum(); + made + state.making.unwrap_or(0) + state.to_make.iter().sum::() } /// Its smallest block made and not yet added of `least` bytes or more, taken.