From 271aaffad2b246c3be86b0d10aa9aa4651cce974 Mon Sep 17 00:00:00 2001 From: james7132 Date: Wed, 11 May 2022 15:40:03 -0700 Subject: [PATCH 1/3] Don't allocate for non-dynamic component types --- crates/bevy_ecs/src/component.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index 97adb2f5beb3e..4cc009959da9f 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -10,6 +10,7 @@ use bevy_ptr::OwningPtr; use std::{ alloc::Layout, any::{Any, TypeId}, + borrow::Cow, }; /// A component is data associated with an [`Entity`](crate::entity::Entity). Each entity can have @@ -157,7 +158,7 @@ impl SparseSetIndex for ComponentId { } pub struct ComponentDescriptor { - name: String, + name: Cow<'static, str>, // SAFETY: This must remain private. It must match the statically known StorageType of the // associated rust component type if one exists. storage_type: StorageType, @@ -175,7 +176,7 @@ pub struct ComponentDescriptor { impl std::fmt::Debug for ComponentDescriptor { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("ComponentDescriptor") - .field("name", &self.name) + .field("name", self.name.as_ref()) .field("storage_type", &self.storage_type) .field("is_send_and_sync", &self.is_send_and_sync) .field("type_id", &self.type_id) @@ -192,7 +193,7 @@ impl ComponentDescriptor { pub fn new() -> Self { Self { - name: std::any::type_name::().to_string(), + name: Cow::Borrowed(std::any::type_name::()), storage_type: T::Storage::STORAGE_TYPE, is_send_and_sync: true, type_id: Some(TypeId::of::()), @@ -206,7 +207,7 @@ impl ComponentDescriptor { /// The [`StorageType`] for resources is always [`TableStorage`]. pub fn new_resource() -> Self { Self { - name: std::any::type_name::().to_string(), + name: Cow::Borrowed(std::any::type_name::()), // PERF: `SparseStorage` may actually be a more // reasonable choice as `storage_type` for resources. storage_type: StorageType::Table, @@ -219,7 +220,7 @@ impl ComponentDescriptor { fn new_non_send(storage_type: StorageType) -> Self { Self { - name: std::any::type_name::().to_string(), + name: Cow::Borrowed(std::any::type_name::()), storage_type, is_send_and_sync: false, type_id: Some(TypeId::of::()), @@ -240,7 +241,7 @@ impl ComponentDescriptor { #[inline] pub fn name(&self) -> &str { - &self.name + self.name.as_ref() } } From ae9a7fa7f2ef0aab629cfb6241f7e80a96e91314 Mon Sep 17 00:00:00 2001 From: james7132 Date: Wed, 11 May 2022 18:47:38 -0700 Subject: [PATCH 2/3] Fix compilation error --- crates/bevy_ecs/src/component.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index 4cc009959da9f..e6d00cb23b991 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -176,7 +176,7 @@ pub struct ComponentDescriptor { impl std::fmt::Debug for ComponentDescriptor { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("ComponentDescriptor") - .field("name", self.name.as_ref()) + .field("name", &self.name) .field("storage_type", &self.storage_type) .field("is_send_and_sync", &self.is_send_and_sync) .field("type_id", &self.type_id) From fb1cc34b6b1c079c313724e7f3e38172aef1b2ef Mon Sep 17 00:00:00 2001 From: james7132 Date: Mon, 30 May 2022 13:58:08 -0700 Subject: [PATCH 3/3] Fix build --- crates/bevy_ecs/src/component.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index 3dfd41803f584..319c4185283c0 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -217,13 +217,13 @@ impl ComponentDescriptor { /// - the `drop` fn must be usable on a pointer with a value of the layout `layout` /// - the component type must be safe to access from any thread (Send + Sync in rust terms) pub unsafe fn new_with_layout( - name: String, + name: impl Into>, storage_type: StorageType, layout: Layout, drop: Option unsafe fn(OwningPtr<'a>)>, ) -> Self { Self { - name, + name: name.into(), storage_type, is_send_and_sync: true, type_id: None,