Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't keep a strong ref in storage for destroyed resources #4786

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions wgpu-core/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(crate) enum Element<T> {

/// Like `Occupied`, but the resource has been marked as destroyed
/// and hasn't been dropped yet.
Destroyed(Arc<T>, Epoch),
Destroyed(Epoch),

/// Like `Occupied`, but an error occurred when creating the
/// resource.
Expand Down Expand Up @@ -80,7 +80,7 @@ where
Some(&Element::Vacant) => false,
Some(
&Element::Occupied(_, storage_epoch)
| &Element::Destroyed(_, storage_epoch)
| &Element::Destroyed(storage_epoch)
| &Element::Error(storage_epoch, _),
) => storage_epoch == epoch,
None => false,
Expand Down Expand Up @@ -145,7 +145,7 @@ where
}
match std::mem::replace(&mut self.map[index], element) {
Element::Vacant => {}
Element::Destroyed(_, storage_epoch) => {
Element::Destroyed(storage_epoch) => {
assert_ne!(
epoch,
storage_epoch,
Expand Down Expand Up @@ -213,15 +213,12 @@ where
std::mem::replace(slot, Element::Vacant)
gents83 marked this conversation as resolved.
Show resolved Hide resolved
{
debug_assert_eq!(storage_epoch, epoch);
*slot = Element::Destroyed(value, storage_epoch);
*slot = Element::Destroyed(storage_epoch);
return Ok(value);
}
}

if let Element::Destroyed(ref value, ..) = *slot {
Ok(value.clone())
} else {
Err(InvalidId)
}
Err(InvalidId)
}

pub(crate) fn force_replace(&mut self, id: I, value: T) {
Expand All @@ -234,10 +231,14 @@ where
log::trace!("User is removing {}{:?}", T::TYPE, id);
let (index, epoch, _) = id.unzip();
match std::mem::replace(&mut self.map[index as usize], Element::Vacant) {
Element::Occupied(value, storage_epoch) | Element::Destroyed(value, storage_epoch) => {
Element::Occupied(value, storage_epoch) => {
assert_eq!(epoch, storage_epoch);
Some(value)
}
Element::Destroyed(storage_epoch) => {
assert_eq!(epoch, storage_epoch);
None
}
Element::Error(..) => None,
Element::Vacant => panic!("Cannot remove a vacant resource"),
}
Expand Down
Loading