ICB: a buffer slot's write takes edit_slot, edit the locked write alone

The unlocked branch at the top of edit slowed the setters that take the
lock: on lavapipe (msi, jobs m3-max-25314, 25351, 25384) a draw took 45.8
ns a command on development and 50.4 to 51.9 with the lock-free buffer
setters. The buffer's set_buffer now calls edit_slot (unlocked on a buffer
the GPU may not have written, else edit), and edit is development's
locked write.

Verified: cargo fmt; measured next.
This commit is contained in:
Anees Iqbal committed 2026-11-13 08:52:22 -08:00
1 parent 3a7eea4cf5
commit 67c98e656c
1 file changed
+15 -6
@@ -1876,12 +1876,9 @@ impl IndirectCommandBuffer {
/// pipeline state, `set` as the state the CPU set on it: the field's words alone, in place, as
/// Metal's setter writes its field, the rest of the record as the GPU and the CPU left it
/// (`CommandRecord::write_field`); then what the record holds joins what its records may
/// (`grown_writes`, `add_written`). All of it under one hold of `pipelines`, but a buffer
/// slot's on a buffer the GPU may not have written ([`edit_unlocked`](Self::edit_unlocked)).
/// (`grown_writes`, `add_written`). All of it under one hold of `pipelines`; a buffer slot's
/// takes [`edit_slot`](Self::edit_slot).
fn edit(&self, index: u32, field: RecordField, set: Option<CommandState>) -> Result<(), Error> {
if set.is_none() && matches!(field, RecordField::Buffer { .. }) && !self.gpu_written() {
return self.edit_unlocked(index, field);
}
let mut commands = lock(&self.0.pipelines);
// SAFETY: the record of a command below the size lies inside the mapped storage, and the
// CPU's writes of the records are one at a time (`pipelines`, held while the slice lives),
@@ -1910,6 +1907,18 @@ impl IndirectCommandBuffer {
Ok(())
}
/// [`edit`](Self::edit) of `field`, a buffer slot: without `pipelines`
/// ([`edit_unlocked`](Self::edit_unlocked)) on a buffer the GPU may not have written, else
/// under it. Apart from `edit`, whose other writes then compile as they did before the
/// unlocked write: beside it in `edit`, a draw took about 5 ns more
/// (`tests::icb_encode_bench`).
fn edit_slot(&self, index: u32, field: RecordField) -> Result<(), Error> {
match self.gpu_written() {
false => self.edit_unlocked(index, field),
true => self.edit(index, field, None),
}
}
/// [`edit`](Self::edit) of `field`, a buffer slot, into command `index`'s record (below the
/// size) without `pipelines`, on a buffer the GPU may not have written: there such an edit
/// names no state and adds nothing to what the records may hold (`grown_writes` reads no record
@@ -2078,7 +2087,7 @@ impl IndirectCommandBuffer {
slot: slot as usize,
binding: Some(binding),
};
self.edit(index, field, None)
self.edit_slot(index, field)
}
/// Where Metal keeps threadgroup length `slot` of command `index` of a compute buffer: at