From 3bcfe8437c80c368b7fc8097b7c43ab1451900b3 Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Fri, 13 Dec 2024 18:15:22 +0000 Subject: [PATCH] Avoid cloning Arcs unnecessarily when iterating trackers (#6721) * Avoid cloning Arcs unnecessarily when iterating trackers * Changelog entry --- CHANGELOG.md | 1 + wgpu-core/src/device/queue.rs | 5 ++--- wgpu-core/src/device/resource.rs | 4 ++-- wgpu-core/src/track/buffer.rs | 4 ++-- wgpu-core/src/track/metadata.rs | 4 ++-- wgpu-core/src/track/texture.rs | 5 ++--- 6 files changed, 11 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index beae54b7a7..6810ee3173 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -166,6 +166,7 @@ By @ErichDonGubler in [#6456](https://github.com/gfx-rs/wgpu/pull/6456), [#6148] - Check that at least one index is specified. - Reject destroyed buffers in query set resolution. By @ErichDonGubler in [#6579](https://github.com/gfx-rs/wgpu/pull/6579). - Fix panic when dropping `Device` on some environments. By @Dinnerbone in [#6681](https://github.com/gfx-rs/wgpu/pull/6681). +- Reduced the overhead of command buffer validation. By @nical in [#6721](https://github.com/gfx-rs/wgpu/pull/6721). #### Naga diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 6ba9728e9e..a44b3241a6 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -1568,8 +1568,7 @@ fn validate_command_buffer( TextureInner::Native { .. } => false, TextureInner::Surface { .. } => { // Compare the Arcs by pointer as Textures don't implement Eq. - submit_surface_textures_owned - .insert(Arc::as_ptr(&texture), texture.clone()); + submit_surface_textures_owned.insert(Arc::as_ptr(texture), texture.clone()); true } @@ -1577,7 +1576,7 @@ fn validate_command_buffer( if should_extend { unsafe { used_surface_textures - .merge_single(&texture, None, hal::TextureUses::PRESENT) + .merge_single(texture, None, hal::TextureUses::PRESENT) .unwrap(); }; } diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index d41156f0b9..e80708c695 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -3637,12 +3637,12 @@ impl Device { // During these iterations, we discard all errors. We don't care! let trackers = self.trackers.lock(); for buffer in trackers.buffers.used_resources() { - if let Some(buffer) = Weak::upgrade(&buffer) { + if let Some(buffer) = Weak::upgrade(buffer) { let _ = buffer.destroy(); } } for texture in trackers.textures.used_resources() { - if let Some(texture) = Weak::upgrade(&texture) { + if let Some(texture) = Weak::upgrade(texture) { let _ = texture.destroy(); } } diff --git a/wgpu-core/src/track/buffer.rs b/wgpu-core/src/track/buffer.rs index 0cdc9dc966..cfd166070d 100644 --- a/wgpu-core/src/track/buffer.rs +++ b/wgpu-core/src/track/buffer.rs @@ -311,7 +311,7 @@ impl BufferTracker { } /// Returns a list of all buffers tracked. - pub fn used_resources(&self) -> impl Iterator> + '_ { + pub fn used_resources(&self) -> impl Iterator> + '_ { self.metadata.owned_resources() } @@ -559,7 +559,7 @@ impl DeviceBufferTracker { } /// Returns a list of all buffers tracked. - pub fn used_resources(&self) -> impl Iterator> + '_ { + pub fn used_resources(&self) -> impl Iterator> + '_ { self.metadata.owned_resources() } diff --git a/wgpu-core/src/track/metadata.rs b/wgpu-core/src/track/metadata.rs index 7ecb2773e3..8f03caf7b0 100644 --- a/wgpu-core/src/track/metadata.rs +++ b/wgpu-core/src/track/metadata.rs @@ -111,13 +111,13 @@ impl ResourceMetadata { } /// Returns an iterator over the resources owned by `self`. - pub(super) fn owned_resources(&self) -> impl Iterator + '_ { + pub(super) fn owned_resources(&self) -> impl Iterator + '_ { if !self.owned.is_empty() { self.tracker_assert_in_bounds(self.owned.len() - 1) }; iterate_bitvec_indices(&self.owned).map(move |index| { let resource = unsafe { self.resources.get_unchecked(index) }; - resource.as_ref().unwrap().clone() + resource.as_ref().unwrap() }) } diff --git a/wgpu-core/src/track/texture.rs b/wgpu-core/src/track/texture.rs index 8ea1e04cd1..82c6442095 100644 --- a/wgpu-core/src/track/texture.rs +++ b/wgpu-core/src/track/texture.rs @@ -429,10 +429,9 @@ impl TextureTracker { } /// Returns a list of all textures tracked. - pub fn used_resources(&self) -> impl Iterator> + '_ { + pub fn used_resources(&self) -> impl Iterator> + '_ { self.metadata.owned_resources() } - /// Drain all currently pending transitions. pub fn drain_transitions<'a>( &'a mut self, @@ -672,7 +671,7 @@ impl DeviceTextureTracker { } /// Returns a list of all textures tracked. - pub fn used_resources(&self) -> impl Iterator> + '_ { + pub fn used_resources(&self) -> impl Iterator> + '_ { self.metadata.owned_resources() }