⚡ A gather dispatch binds its pipeline once per draw and builds its run table without reallocating, because the instrument put 76% of the cost in those two columns and 9% in the one the plan named

x86 macOS / Linux Vulkan, `backend/vulkan/engine`. Byte-identical output on
every rail: this changes only how the guest-scatter kernel's dispatches are
recorded, and both rails that use it — the render writeback and the draw-time
buffer gather — take the same two changes.

`gather_phase` (f6420526) measured a dispatch at 0.959 us and split it
`record` 39 % / `plan` 37 % / `stage` 16 % / `dset` 9 %. This is the two large
columns:

- **`vkCmdBindPipeline` is hoisted out of the dispatch loop.** It was one of
  `record`'s four driver calls and the handle never changes, so a draw's 1.4
  dispatches paid for 1.4 binds of one pipeline and the writeback's ~200-region
  windows paid one per table. `ScatterPipeline::bind` is now separate from
  `dispatch`, and its doc states the obligation the split creates: no other
  pipeline may be bound to the compute bind point between them. Both call sites
  record their dispatches in one uninterrupted loop, which is what makes that
  hold.
- **The run table's word vector is built at its exact capacity.** It grew from
  `Vec::new()` by `push`, four words at a time, to ~50 words for a ~13-run
  gather — about six reallocations and copies per table, ~21 000 tables a
  second. Every run contributes exactly `WORDS_PER_RUN` words and a window holds
  at most all of them, so `runs.len() * WORDS_PER_RUN` is the exact upper bound
  and the loop cannot reallocate at all. `tables` reserves for one window, which
  is what a boot produces on 94 % of writebacks.

Neither touches `dset`, which is the column `env::COMPUTE_GATHER` had named as
the thing to fix and which the instrument found at 9 %.

Tests: unchanged and unchanged deliberately — this is the same table, the same
bytes and the same dispatches, so the ~40 existing `guest_scatter` cases are the
regression gate and all of them pass. A test that asserted a `Vec`'s capacity
would be testing spelling. Full suite 1931 passed / 0 failed, clippy
`-D warnings` clean on all three arms.

Not verified: no boot yet. The next reading is `gather_phase` from one driven
macos-13 boot — `plan` and `record` are what should move, and by how much is
what says whether the destination arena (the remaining ~30 % of `record`, by
merging a draw's 1.4 dispatches into one) is worth building.
This commit is contained in:
Anees Iqbal committed 2026-08-11 10:27:34 +03:00
1 parent f64205262c
commit 71102e6a6f
3 files changed
+39 -6

No files matched your search

@@ -3319,6 +3319,10 @@ pub(crate) unsafe fn execute_draw_inner(
Some(groups) => {
let pipeline = unsafe { pools.scatter_pipeline(ctx) }
.expect("planned only after the pipeline was created");
// Bound once for the run: `record` is 39 % of a dispatch's
// cost and this was one of its four driver calls, repeated for
// a handle that never changes.
unsafe { pipeline.bind(&ctx.device, cb) };
for g in &groups {
let _r = super::gather_phase::Span::open(super::gather_phase::Part::Record);
unsafe { pipeline.dispatch(&ctx.device, cb, g.set, g.run_count) };
@@ -243,8 +243,17 @@ pub(crate) fn build_run_tables(
order.sort_unstable_by_key(|r| r.dst);
let align = bind_align.max(1);
let mut tables: Vec<RunTable> = Vec::new();
// One window is the overwhelmingly common case — the partition below only
// splits at a `max_range` boundary, which a driven boot met on 6 % of its
// writebacks — so this reserves for that and grows on the rare split.
let mut tables: Vec<RunTable> = Vec::with_capacity(1);
let mut open: Option<(u64, Vec<u32>, u64)> = None;
// Every run contributes exactly [`WORDS_PER_RUN`] words and a window holds
// at most all of them, so this is the exact upper bound and the push loop
// below cannot reallocate. It was reallocating six times per table on a
// ~13-run gather, growing a `Vec::new()` to fifty words, and this rail plans
// ~21 000 tables a second — see [`super::gather_phase`].
let words_cap = runs.len() * WORDS_PER_RUN;
for run in order {
let end = run.dst.saturating_add(run.len);
// `align` is at least 16 and always a power of two, so the rounded-down
@@ -270,7 +279,7 @@ pub(crate) fn build_run_tables(
}
None => fresh_base,
};
let (_, words, hi) = open.get_or_insert_with(|| (base, Vec::new(), 0));
let (_, words, hi) = open.get_or_insert_with(|| (base, Vec::with_capacity(words_cap), 0));
// Every one of these divisions is exact and every result is bounded by
// the `max_range` check above, so the truncating casts cannot lose a bit.
words.push((run.src / SCATTER_WORD) as u32);
@@ -534,12 +543,31 @@ impl ScatterPipeline {
unsafe { device.update_descriptor_sets(&writes, &[]) };
}
/// Bind, push the run count and dispatch one workgroup per run.
/// Bind the kernel, once, ahead of a run of [`Self::dispatch`]es.
///
/// Split out because the handle is the same on every dispatch and this rail
/// issues ~21 000 of them a second: `record` is 39 % of a dispatch's CPU
/// cost and this is one of its four driver calls, so a draw's 1.4 dispatches
/// paid for 1.4 binds of one pipeline. Whoever binds is also responsible for
/// there being no *other* pipeline bound to
/// [`vk::PipelineBindPoint::COMPUTE`] between the bind and the last
/// dispatch — which holds at both call sites, because each records its
/// dispatches in one uninterrupted loop.
///
/// # Safety
///
/// `cb` must be recording, and `set` must name buffers live for the whole
/// of the submission `cb` belongs to.
/// `cb` must be recording.
pub(crate) unsafe fn bind(&self, device: &ash::Device, cb: vk::CommandBuffer) {
unsafe { device.cmd_bind_pipeline(cb, vk::PipelineBindPoint::COMPUTE, self.pipeline) };
}
/// Bind the set, push the run count and dispatch one workgroup per run.
///
/// # Safety
///
/// `cb` must be recording with this pipeline bound by [`Self::bind`], and
/// `set` must name buffers live for the whole of the submission `cb`
/// belongs to.
pub(crate) unsafe fn dispatch(
&self,
device: &ash::Device,
@@ -548,7 +576,6 @@ impl ScatterPipeline {
run_count: u32,
) {
unsafe {
device.cmd_bind_pipeline(cb, vk::PipelineBindPoint::COMPUTE, self.pipeline);
device.cmd_bind_descriptor_sets(
cb,
vk::PipelineBindPoint::COMPUTE,
@@ -3274,6 +3274,8 @@ unsafe fn copy_image_level0_to_buffer(
// pipeline is a fixture of the device. It is already built —
// the plan could not have been made otherwise.
if let Some(pipeline) = pools.scatter_pipeline(ctx) {
// One bind for the whole run; the handle never changes.
pipeline.bind(&ctx.device, cb);
for group in groups {
pipeline.dispatch(&ctx.device, cb, group.set, group.run_count);
}