rt: a borrow is an uncounted Arc of the kept handle's pointer, never a bitwise copy; the kept lists are boxed per command buffer

A Lent was a ptr::read of the handle, which the readback rule (every raw read is a read of
GPU-written memory, in readback.rs) rightly refuses outside readback.rs. A handle now makes its
uncounted twin itself (Handle::uncounted: Arc::from_raw of its own Arc's pointer, in a
ManuallyDrop), so Handle is a safe trait and the one unsafe step is the call, whose contract is
the borrow's.

The queue keeps its spare lists unboxed (clippy's vec_box), and a command buffer boxes the one
it takes outside the queue's lock: one allocation per command buffer, where each of the five
lists before E2 was a new Vec per command buffer.
This commit is contained in:
Anees Iqbal committed 2026-11-18 11:51:55 -08:00
1 parent a044cc211d
commit 339f818983
8 files changed
+65 -27

No files matched your search

+8 -2
View File
@@ -367,11 +367,17 @@ impl Device {
}
}
// SAFETY: the handle is its one `Arc` and nothing else, and its address is that `Arc`'s pointer.
unsafe impl crate::kept::Handle for Buffer {
impl crate::kept::Handle for Buffer {
fn address(&self) -> usize {
Arc::as_ptr(&self.inner) as *const () as usize
}
unsafe fn uncounted(&self) -> std::mem::ManuallyDrop<Buffer> {
// SAFETY: the pointer is a live `Arc`'s; the handle made of it is never dropped, and the
// caller keeps the object alive past its last use.
let inner = unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) };
std::mem::ManuallyDrop::new(Buffer { inner })
}
}
impl Buffer {
+14 -13
View File
@@ -16,16 +16,19 @@ use std::hash::{BuildHasherDefault, Hasher};
use std::mem::ManuallyDrop;
use std::ops::Deref;
/// A handle to a shared object that is one `Arc` and nothing else: a copy of it made bit for bit
/// is the same object, and dropping no copy but one keeps the count right.
///
/// # Safety
///
/// The type holds exactly one `Arc` and no other state, and `address` is that `Arc`'s pointer.
pub(crate) unsafe trait Handle: Clone {
/// The object's address: the same for every handle to it, and no other object's while it
/// lives.
/// A handle to a shared object that is one `Arc` and nothing else.
pub(crate) trait Handle: Clone {
/// The object's address: its `Arc`'s pointer, the same for every handle to it, and no other
/// object's while it lives.
fn address(&self) -> usize;
/// Another handle to the object, made of the same pointer, that took no count: dropping it
/// would give back one it never took, so it is never dropped.
///
/// # Safety
///
/// The object lives past the handle's last use.
unsafe fn uncounted(&self) -> ManuallyDrop<Self>;
}
/// A handle borrowed from what a recording keeps: the same object, which this copy neither
@@ -40,10 +43,8 @@ impl<T: Handle> Lent<T> {
///
/// The recording the borrow is used in keeps `object` until after the borrow's last use.
unsafe fn of(object: &T) -> Lent<T> {
// SAFETY: `T: Handle` is one `Arc`, so the copy is the same object; it is never dropped,
// so the count stays the kept handle's, and the caller keeps that one alive past the
// copy's last use.
Lent(ManuallyDrop::new(unsafe { std::ptr::read(object) }))
// SAFETY: the caller keeps the object alive past the borrow's last use.
Lent(unsafe { object.uncounted() })
}
}
+8 -2
View File
@@ -1409,9 +1409,15 @@ impl Drop for Compiled {
}
}
// SAFETY: the handle is its one `Arc` and nothing else, and its address is that `Arc`'s pointer.
unsafe impl crate::kept::Handle for ComputePipelineState {
impl crate::kept::Handle for ComputePipelineState {
fn address(&self) -> usize {
Arc::as_ptr(&self.inner) as *const () as usize
}
unsafe fn uncounted(&self) -> std::mem::ManuallyDrop<ComputePipelineState> {
// SAFETY: the pointer is a live `Arc`'s; the handle made of it is never dropped, and the
// caller keeps the object alive past its last use.
let inner = unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) };
std::mem::ManuallyDrop::new(ComputePipelineState { inner })
}
}
+3 -2
View File
@@ -47,7 +47,7 @@ struct Slots {
free: Vec<u32>,
/// The kept-object lists of retired command buffers, empty, with their capacity
/// (`crate::kept`): at most one per slot.
objects: Vec<Box<Objects>>,
objects: Vec<Objects>,
}
impl Slots {
@@ -124,7 +124,7 @@ impl CommandQueue {
Arc::clone(&self.inner),
self.device.clone(),
slot,
kept,
Box::new(kept),
))
}
@@ -179,6 +179,7 @@ impl QueueInner {
/// holds.
pub(crate) fn release(&self, slot: u32, kept: Box<Objects>) {
self.counters.fall(Gauge::CommandBuffers);
let kept = *kept;
let mut slots = lock(&self.slots);
slots.free.push(slot);
slots.objects.push(kept);
+8 -2
View File
@@ -850,9 +850,15 @@ impl Drop for RenderCompiled {
}
}
// SAFETY: the handle is its one `Arc` and nothing else, and its address is that `Arc`'s pointer.
unsafe impl crate::kept::Handle for RenderPipelineState {
impl crate::kept::Handle for RenderPipelineState {
fn address(&self) -> usize {
Arc::as_ptr(&self.inner) as *const () as usize
}
unsafe fn uncounted(&self) -> std::mem::ManuallyDrop<RenderPipelineState> {
// SAFETY: the pointer is a live `Arc`'s; the handle made of it is never dropped, and the
// caller keeps the object alive past its last use.
let inner = unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) };
std::mem::ManuallyDrop::new(RenderPipelineState { inner })
}
}
+8 -2
View File
@@ -392,9 +392,15 @@ impl SamplerState {
}
}
// SAFETY: the handle is its one `Arc` and nothing else, and its address is that `Arc`'s pointer.
unsafe impl crate::kept::Handle for SamplerState {
impl crate::kept::Handle for SamplerState {
fn address(&self) -> usize {
Arc::as_ptr(&self.inner) as *const () as usize
}
unsafe fn uncounted(&self) -> std::mem::ManuallyDrop<SamplerState> {
// SAFETY: the pointer is a live `Arc`'s; the handle made of it is never dropped, and the
// caller keeps the object alive past its last use.
let inner = unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) };
std::mem::ManuallyDrop::new(SamplerState { inner })
}
}
+8 -2
View File
@@ -2,6 +2,7 @@
//! taken; a later use and every borrow of it change no count; clearing releases every object.
use crate::kept::{Handle, Kept};
use std::mem::ManuallyDrop;
use std::sync::Arc;
/// A handle of one `Arc`, as the runtime's objects are.
@@ -10,11 +11,16 @@ struct Probe {
inner: Arc<u32>,
}
// SAFETY: the handle is its one `Arc` and nothing else, and its address is that `Arc`'s pointer.
unsafe impl Handle for Probe {
impl Handle for Probe {
fn address(&self) -> usize {
Arc::as_ptr(&self.inner) as usize
}
unsafe fn uncounted(&self) -> ManuallyDrop<Probe> {
// SAFETY: as the runtime's handles do.
let inner = unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) };
ManuallyDrop::new(Probe { inner })
}
}
/// An object kept, then used again, held again and borrowed many times, has one reference of the
+8 -2
View File
@@ -3105,9 +3105,15 @@ pub(crate) struct OverCopy {
pub(crate) shared: bool,
}
// SAFETY: the handle is its one `Arc` and nothing else, and its address is that `Arc`'s pointer.
unsafe impl crate::kept::Handle for Texture {
impl crate::kept::Handle for Texture {
fn address(&self) -> usize {
Arc::as_ptr(&self.inner) as *const () as usize
}
unsafe fn uncounted(&self) -> std::mem::ManuallyDrop<Texture> {
// SAFETY: the pointer is a live `Arc`'s; the handle made of it is never dropped, and the
// caller keeps the object alive past its last use.
let inner = unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) };
std::mem::ManuallyDrop::new(Texture { inner })
}
}