From 9d966c54df41ecb2b016e4438f2ff6e350f88cfd Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 18 Nov 2026 10:54:34 -0800 Subject: [PATCH] rt: a tally adds only the metrics it counted The encoder change's tally visited all 25 metrics at each add, at every encoder's end and again at every commit: on a burst of command buffers of one dispatch each that is two 25-step loops per command buffer. On the Iris Xe host (job m3-max-47513, i9-13900H pinned to its performance cores) burst_2000 took 29,285 user-space instructions per command buffer at bfaf8ca194 against 28,581 at 69af14bd22, with the encode time the same (5,852 against 5,788 ns). The tally now keeps a bit per metric counted, and the add visits those alone: one atomic add for each metric counted, none for a tally that counted nothing (a commit with no command between its encoders). Expected to take the per-command-buffer instructions back under the tip's; to be measured. --- crates/metal2vulkan-rt/src/counters.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/crates/metal2vulkan-rt/src/counters.rs b/crates/metal2vulkan-rt/src/counters.rs index f2e046b51..a657bbf2c 100644 --- a/crates/metal2vulkan-rt/src/counters.rs +++ b/crates/metal2vulkan-rt/src/counters.rs @@ -124,12 +124,18 @@ const _: () = { #[derive(Default)] pub(crate) struct Tally { counts: [u64; METRICS], + /// The metrics counted since the tally was last added, a bit each at its index. + counted: u32, } +// Each metric has its bit in a tally's `counted`. +const _: () = assert!(METRICS <= u32::BITS as usize); + impl Tally { /// Counts one more of `metric`. pub(crate) fn count(&mut self, metric: Metric) { self.counts[metric as usize] += 1; + self.counted |= 1 << metric as usize; } } @@ -307,13 +313,15 @@ impl Counters { self.metric(metric).fetch_add(1, Relaxed); } - /// Adds what `tally` counted, and leaves it at zero. + /// Adds what `tally` counted, and leaves it at zero: one atomic add for each metric it + /// counted, and none where it counted nothing. pub(crate) fn add(&self, tally: &mut Tally) { - for (metric, count) in METRIC_ORDER.iter().zip(&mut tally.counts) { - if *count > 0 { - self.metric(*metric).fetch_add(*count, Relaxed); - *count = 0; - } + while tally.counted != 0 { + let at = tally.counted.trailing_zeros() as usize; + tally.counted &= tally.counted - 1; + let count = tally.counts[at]; + tally.counts[at] = 0; + self.metric(METRIC_ORDER[at]).fetch_add(count, Relaxed); } }