Skip to content

Commit e65dc4e

Browse files
kpreidEriKWDev
authored andcommitted
Reduce string allocations related to labels and logging. (gfx-rs#5690)
1 parent 351705a commit e65dc4e

File tree

7 files changed

+22
-30
lines changed

7 files changed

+22
-30
lines changed

wgpu-core/src/binding_model.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,8 @@ impl<A: HalApi> Resource for BindGroupLayout<A> {
514514
&mut self.info
515515
}
516516

517-
fn label(&self) -> String {
518-
self.label.clone()
517+
fn label(&self) -> &str {
518+
&self.label
519519
}
520520
}
521521
impl<A: HalApi> BindGroupLayout<A> {

wgpu-core/src/command/mod.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,7 @@ impl<A: HalApi> CommandBuffer<A> {
333333
device: device.clone(),
334334
limits: device.limits.clone(),
335335
support_clear_texture: device.features.contains(wgt::Features::CLEAR_TEXTURE),
336-
info: ResourceInfo::new(
337-
label
338-
.as_ref()
339-
.unwrap_or(&String::from("<CommandBuffer>"))
340-
.as_str(),
341-
None,
342-
),
336+
info: ResourceInfo::new(label.as_deref().unwrap_or("<CommandBuffer>"), None),
343337
data: Mutex::new(
344338
rank::COMMAND_BUFFER_DATA,
345339
Some(CommandBufferMutable {
@@ -477,14 +471,6 @@ impl<A: HalApi> Resource for CommandBuffer<A> {
477471
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
478472
&mut self.info
479473
}
480-
481-
fn label(&self) -> String {
482-
let str = match self.data.lock().as_ref().unwrap().encoder.label.as_ref() {
483-
Some(label) => label.clone(),
484-
_ => String::new(),
485-
};
486-
str
487-
}
488474
}
489475

490476
#[derive(Copy, Clone, Debug)]

wgpu-core/src/device/global.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,9 +1363,7 @@ impl Global {
13631363
&device,
13641364
#[cfg(feature = "trace")]
13651365
device.trace.lock().is_some(),
1366-
desc.label
1367-
.to_hal(device.instance_flags)
1368-
.map(|s| s.to_string()),
1366+
desc.label.to_hal(device.instance_flags).map(str::to_owned),
13691367
);
13701368

13711369
let (id, _) = fid.assign(Arc::new(command_buffer));

wgpu-core/src/instance.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ impl Resource for Surface {
159159
&mut self.info
160160
}
161161

162-
fn label(&self) -> String {
163-
String::from("<Surface>")
162+
fn label(&self) -> &str {
163+
"<Surface>"
164164
}
165165
}
166166

wgpu-core/src/pipeline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ impl<A: HalApi> Resource for ShaderModule<A> {
8484
&mut self.info
8585
}
8686

87-
fn label(&self) -> String {
88-
self.label.clone()
87+
fn label(&self) -> &str {
88+
&self.label
8989
}
9090
}
9191

wgpu-core/src/registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl<T: Resource> Registry<T> {
182182
if label.is_empty() {
183183
format!("<{}-{:?}>", type_name, id.unzip())
184184
} else {
185-
label
185+
label.to_owned()
186186
}
187187
}
188188
Err(_) => format!(

wgpu-core/src/resource.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ impl<T: Resource> Drop for ResourceInfo<T> {
8484
}
8585

8686
impl<T: Resource> ResourceInfo<T> {
87-
#[allow(unused_variables)]
87+
// Note: Abstractly, this function should take `label: String` to minimize string cloning.
88+
// But as actually used, every input is a literal or borrowed `&str`, so this is convenient.
8889
pub(crate) fn new(
8990
label: &str,
9091
tracker_indices: Option<Arc<SharedTrackerIndexAllocator>>,
@@ -149,9 +150,16 @@ pub(crate) trait Resource: 'static + Sized + WasmNotSendSync {
149150
const TYPE: ResourceType;
150151
fn as_info(&self) -> &ResourceInfo<Self>;
151152
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self>;
152-
fn label(&self) -> String {
153-
self.as_info().label.clone()
153+
154+
/// Returns a string identifying this resource for logging and errors.
155+
///
156+
/// It may be a user-provided string or it may be a placeholder from wgpu.
157+
///
158+
/// It is non-empty unless the user-provided string was empty.
159+
fn label(&self) -> &str {
160+
&self.as_info().label
154161
}
162+
155163
fn ref_count(self: &Arc<Self>) -> usize {
156164
Arc::strong_count(self)
157165
}
@@ -718,8 +726,8 @@ impl<A: HalApi> Resource for StagingBuffer<A> {
718726
&mut self.info
719727
}
720728

721-
fn label(&self) -> String {
722-
String::from("<StagingBuffer>")
729+
fn label(&self) -> &str {
730+
"<StagingBuffer>"
723731
}
724732
}
725733

0 commit comments

Comments
 (0)