Reduce string allocations related to labels and logging. (#5690)

This commit is contained in:
Kevin Reid 2024-05-14 01:39:28 -07:00 committed by GitHub
parent ffd96a0a53
commit 9b70254437
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 22 additions and 30 deletions

View File

@ -514,8 +514,8 @@ impl<A: HalApi> Resource for BindGroupLayout<A> {
&mut self.info
}
fn label(&self) -> String {
self.label.clone()
fn label(&self) -> &str {
&self.label
}
}
impl<A: HalApi> BindGroupLayout<A> {

View File

@ -333,13 +333,7 @@ impl<A: HalApi> CommandBuffer<A> {
device: device.clone(),
limits: device.limits.clone(),
support_clear_texture: device.features.contains(wgt::Features::CLEAR_TEXTURE),
info: ResourceInfo::new(
label
.as_ref()
.unwrap_or(&String::from("<CommandBuffer>"))
.as_str(),
None,
),
info: ResourceInfo::new(label.as_deref().unwrap_or("<CommandBuffer>"), None),
data: Mutex::new(
rank::COMMAND_BUFFER_DATA,
Some(CommandBufferMutable {
@ -477,14 +471,6 @@ impl<A: HalApi> Resource for CommandBuffer<A> {
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
fn label(&self) -> String {
let str = match self.data.lock().as_ref().unwrap().encoder.label.as_ref() {
Some(label) => label.clone(),
_ => String::new(),
};
str
}
}
#[derive(Copy, Clone, Debug)]

View File

@ -1363,9 +1363,7 @@ impl Global {
&device,
#[cfg(feature = "trace")]
device.trace.lock().is_some(),
desc.label
.to_hal(device.instance_flags)
.map(|s| s.to_string()),
desc.label.to_hal(device.instance_flags).map(str::to_owned),
);
let (id, _) = fid.assign(Arc::new(command_buffer));

View File

@ -159,8 +159,8 @@ impl Resource for Surface {
&mut self.info
}
fn label(&self) -> String {
String::from("<Surface>")
fn label(&self) -> &str {
"<Surface>"
}
}

View File

@ -84,8 +84,8 @@ impl<A: HalApi> Resource for ShaderModule<A> {
&mut self.info
}
fn label(&self) -> String {
self.label.clone()
fn label(&self) -> &str {
&self.label
}
}

View File

@ -182,7 +182,7 @@ impl<T: Resource> Registry<T> {
if label.is_empty() {
format!("<{}-{:?}>", type_name, id.unzip())
} else {
label
label.to_owned()
}
}
Err(_) => format!(

View File

@ -84,7 +84,8 @@ impl<T: Resource> Drop for ResourceInfo<T> {
}
impl<T: Resource> ResourceInfo<T> {
#[allow(unused_variables)]
// Note: Abstractly, this function should take `label: String` to minimize string cloning.
// But as actually used, every input is a literal or borrowed `&str`, so this is convenient.
pub(crate) fn new(
label: &str,
tracker_indices: Option<Arc<SharedTrackerIndexAllocator>>,
@ -149,9 +150,16 @@ pub(crate) trait Resource: 'static + Sized + WasmNotSendSync {
const TYPE: ResourceType;
fn as_info(&self) -> &ResourceInfo<Self>;
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self>;
fn label(&self) -> String {
self.as_info().label.clone()
/// Returns a string identifying this resource for logging and errors.
///
/// It may be a user-provided string or it may be a placeholder from wgpu.
///
/// It is non-empty unless the user-provided string was empty.
fn label(&self) -> &str {
&self.as_info().label
}
fn ref_count(self: &Arc<Self>) -> usize {
Arc::strong_count(self)
}
@ -718,8 +726,8 @@ impl<A: HalApi> Resource for StagingBuffer<A> {
&mut self.info
}
fn label(&self) -> String {
String::from("<StagingBuffer>")
fn label(&self) -> &str {
"<StagingBuffer>"
}
}