From 376d835a4f105c0fa01ca32585934cf1ea7aa8eb Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 3 Nov 2026 04:18:46 -0800 Subject: [PATCH] Take no lock of a command buffer's fresh-texture log at a commit that logged nothing What: FreshLog keeps a flag, set where FreshLog::add pushes an event. FreshUnwritten::commit returns before it takes the log's lock where the flag is clear, as it already did where no texture is noted. A commit that logged something takes the log as before. Why: host_ab percall's commit rows failed on gate/stack11: commit_one_dispatch 5 allocations a call (held to 4) and commit_no_copy_dispatch 10 (held to 9). e3b963bdc0 measured 4 and 9 on 9f56956ef3, before the pattern reads moved to the commit (95d7bdf6dc). Since then, while any noted texture is live in the process, every commit locks its command buffer's FreshLog mutex, in these rows for the first time: their dispatches bind only buffers and log nothing. On macOS a std Mutex makes its pthread mutex at its first lock, one allocation (Rust 1.98.1 on the M3: 4 allocations for the first locks of 4 new mutexes, 0 for the second locks). The trace put that allocation in FreshUnwritten::commit, the fifth of the commit's sites. The other four (Predication::end_unit, close_tail, the BatchSync list, gate::Hold::new) are the ones the rows were measured with. Verified: - host_ab --only percall --rounds 1, release, Apple M3 Max (MoltenVK 1.4.2), on 4f0a1043b1 with this branch's two commits before this one, the gate made to print: commit_one_dispatch 4 and commit_no_copy_dispatch 9 allocations a call (stack11 with those two commits: 5 and 10). No row of the gate fails in that run: end_render 24, end_compute 7, end_blit 7, heap_texture_256 17.25, the draws 0.0390625, dispatch 8.13. - The traced commit_one_dispatch lists Predication::end_unit, close_tail, the BatchSync list and gate::Hold::new, the four a trace of 9f56956ef3 lists, and no FreshUnwritten::commit. - tests::fresh_unwritten (8), tests::tracker and tests::heap pass under --cfg m2v_gpu on the M3 Max (93 of 93). - cargo clippy -p metal2vulkan-runtime --all-targets --all-features -D warnings, plain and under --cfg m2v_gpu: clean. The rows already hold the counts this restores, so the gate itself is the check: it fails without this change. No answer, announcement or submission changes (a log with no events gave the record nothing), so RUNTIME_VERSION stays; the runtime sources' pin moves with fresh_unwritten.rs. (cherry picked from commit b878f5b569662b4fcf45c23ef84e619e5e212ea6) (cherry picked from commit 6c70d5b7e01a874aa2cfde501b024cea394209d4) --- crates/metal2vulkan-runtime/src/fresh_unwritten.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/fresh_unwritten.rs b/crates/metal2vulkan-runtime/src/fresh_unwritten.rs index c29b82b95..0cbbb3589 100644 --- a/crates/metal2vulkan-runtime/src/fresh_unwritten.rs +++ b/crates/metal2vulkan-runtime/src/fresh_unwritten.rs @@ -108,6 +108,9 @@ pub(super) struct FreshUnwritten { #[derive(Default)] pub(crate) struct FreshLog { events: Mutex>, + /// Whether an event was logged: a commit of a command buffer that logged none takes not even + /// the lock, whose first take allocates where the platform's mutex is made lazily (macOS). + logged: AtomicBool, } /// One entry of a [`FreshLog`]. @@ -162,6 +165,7 @@ impl FreshLog { let last = (events.iter().rev().take(REPEATS)).find(|logged| logged.key() == event.key()); if last != Some(&event) { events.push(event); + self.logged.store(true, Ordering::Release); } } } @@ -570,11 +574,11 @@ impl FreshUnwritten { /// but a refused commit, whose commands run nothing), its reads and writes of noted textures /// apply to the record in the order they were encoded, as one, and each read that reached an /// unwritten texel is announced once the record's locks are free, in the committing call (the - /// module docs' **Order**). One atomic read where no texture is noted; the log goes with the - /// command buffer. + /// module docs' **Order**). One atomic read where no texture is noted, two where the command + /// buffer logged nothing; the log goes with the command buffer. pub(crate) fn commit(device: &Device, log: &FreshLog, runs: bool) { let fresh = &device.shared().fresh; - if !fresh.any() { + if !fresh.any() || !log.logged.load(Ordering::Acquire) { return; } // Committed, the command buffer encodes nothing more: its log goes to the record whole.