Skip to content

Commit 3bcfe84

Browse files
authored
Avoid cloning Arcs unnecessarily when iterating trackers (#6721)
* Avoid cloning Arcs unnecessarily when iterating trackers * Changelog entry
1 parent 3918a09 commit 3bcfe84

File tree

6 files changed

+11
-12
lines changed

6 files changed

+11
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ By @ErichDonGubler in [#6456](https://github.com/gfx-rs/wgpu/pull/6456), [#6148]
166166
- Check that at least one index is specified.
167167
- Reject destroyed buffers in query set resolution. By @ErichDonGubler in [#6579](https://github.com/gfx-rs/wgpu/pull/6579).
168168
- Fix panic when dropping `Device` on some environments. By @Dinnerbone in [#6681](https://github.com/gfx-rs/wgpu/pull/6681).
169+
- Reduced the overhead of command buffer validation. By @nical in [#6721](https://github.com/gfx-rs/wgpu/pull/6721).
169170

170171
#### Naga
171172

wgpu-core/src/device/queue.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,16 +1568,15 @@ fn validate_command_buffer(
15681568
TextureInner::Native { .. } => false,
15691569
TextureInner::Surface { .. } => {
15701570
// Compare the Arcs by pointer as Textures don't implement Eq.
1571-
submit_surface_textures_owned
1572-
.insert(Arc::as_ptr(&texture), texture.clone());
1571+
submit_surface_textures_owned.insert(Arc::as_ptr(texture), texture.clone());
15731572

15741573
true
15751574
}
15761575
};
15771576
if should_extend {
15781577
unsafe {
15791578
used_surface_textures
1580-
.merge_single(&texture, None, hal::TextureUses::PRESENT)
1579+
.merge_single(texture, None, hal::TextureUses::PRESENT)
15811580
.unwrap();
15821581
};
15831582
}

wgpu-core/src/device/resource.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3637,12 +3637,12 @@ impl Device {
36373637
// During these iterations, we discard all errors. We don't care!
36383638
let trackers = self.trackers.lock();
36393639
for buffer in trackers.buffers.used_resources() {
3640-
if let Some(buffer) = Weak::upgrade(&buffer) {
3640+
if let Some(buffer) = Weak::upgrade(buffer) {
36413641
let _ = buffer.destroy();
36423642
}
36433643
}
36443644
for texture in trackers.textures.used_resources() {
3645-
if let Some(texture) = Weak::upgrade(&texture) {
3645+
if let Some(texture) = Weak::upgrade(texture) {
36463646
let _ = texture.destroy();
36473647
}
36483648
}

wgpu-core/src/track/buffer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ impl BufferTracker {
311311
}
312312

313313
/// Returns a list of all buffers tracked.
314-
pub fn used_resources(&self) -> impl Iterator<Item = Arc<Buffer>> + '_ {
314+
pub fn used_resources(&self) -> impl Iterator<Item = &Arc<Buffer>> + '_ {
315315
self.metadata.owned_resources()
316316
}
317317

@@ -559,7 +559,7 @@ impl DeviceBufferTracker {
559559
}
560560

561561
/// Returns a list of all buffers tracked.
562-
pub fn used_resources(&self) -> impl Iterator<Item = Weak<Buffer>> + '_ {
562+
pub fn used_resources(&self) -> impl Iterator<Item = &Weak<Buffer>> + '_ {
563563
self.metadata.owned_resources()
564564
}
565565

wgpu-core/src/track/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ impl<T: Clone> ResourceMetadata<T> {
111111
}
112112

113113
/// Returns an iterator over the resources owned by `self`.
114-
pub(super) fn owned_resources(&self) -> impl Iterator<Item = T> + '_ {
114+
pub(super) fn owned_resources(&self) -> impl Iterator<Item = &T> + '_ {
115115
if !self.owned.is_empty() {
116116
self.tracker_assert_in_bounds(self.owned.len() - 1)
117117
};
118118
iterate_bitvec_indices(&self.owned).map(move |index| {
119119
let resource = unsafe { self.resources.get_unchecked(index) };
120-
resource.as_ref().unwrap().clone()
120+
resource.as_ref().unwrap()
121121
})
122122
}
123123

wgpu-core/src/track/texture.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,9 @@ impl TextureTracker {
429429
}
430430

431431
/// Returns a list of all textures tracked.
432-
pub fn used_resources(&self) -> impl Iterator<Item = Arc<Texture>> + '_ {
432+
pub fn used_resources(&self) -> impl Iterator<Item = &Arc<Texture>> + '_ {
433433
self.metadata.owned_resources()
434434
}
435-
436435
/// Drain all currently pending transitions.
437436
pub fn drain_transitions<'a>(
438437
&'a mut self,
@@ -672,7 +671,7 @@ impl DeviceTextureTracker {
672671
}
673672

674673
/// Returns a list of all textures tracked.
675-
pub fn used_resources(&self) -> impl Iterator<Item = Weak<Texture>> + '_ {
674+
pub fn used_resources(&self) -> impl Iterator<Item = &Weak<Texture>> + '_ {
676675
self.metadata.owned_resources()
677676
}
678677

0 commit comments

Comments
 (0)