From 0d70a7361d653c396e11f0df60c716db75e3080d Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Fri, 3 May 2024 15:36:13 -0700 Subject: [PATCH] [core] Properly recycle `Device::temp_suspected`. Change `Device::untrack` to properly reuse the `ResourceMap` allocated for prior calls. The prior code tries to do this but always leaves `Device::temp_suspected` set to a new empty `ResourceMap`, leaving the previous value to be dropped by `ResourceMap::extend`. Change `ResourceMap::extend` to take `other` by reference, rather than taking it by value and dropping it. --- wgpu-core/src/device/life.rs | 2 +- wgpu-core/src/device/resource.rs | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/wgpu-core/src/device/life.rs b/wgpu-core/src/device/life.rs index 23f09682e..acef23419 100644 --- a/wgpu-core/src/device/life.rs +++ b/wgpu-core/src/device/life.rs @@ -93,7 +93,7 @@ impl ResourceMaps { destroyed_textures.clear(); } - pub(crate) fn extend(&mut self, mut other: Self) { + pub(crate) fn extend(&mut self, other: &mut Self) { let ResourceMaps { buffers, staging_buffers, diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index aa5576b82..2541af7c7 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -485,12 +485,14 @@ impl Device { } pub(crate) fn untrack(&self, trackers: &Tracker) { + // If we have a previously allocated `ResourceMap`, just use that. let mut temp_suspected = self .temp_suspected .lock() - .replace(ResourceMaps::new()) - .unwrap(); + .take() + .unwrap_or_else(|| ResourceMaps::new()); temp_suspected.clear(); + // As the tracker is cleared/dropped, we need to consider all the resources // that it references for destruction in the next GC pass. { @@ -551,7 +553,11 @@ impl Device { } } } - self.lock_life().suspected_resources.extend(temp_suspected); + self.lock_life() + .suspected_resources + .extend(&mut temp_suspected); + // Save this resource map for later reuse. + *self.temp_suspected.lock() = Some(temp_suspected); } pub(crate) fn create_buffer(