Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Don't allocate for ComponentDescriptors of non-dynamic component types #4725

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -192,7 +193,7 @@ impl ComponentDescriptor {

pub fn new<T: Component>() -> Self {
Self {
name: std::any::type_name::<T>().to_string(),
name: Cow::Borrowed(std::any::type_name::<T>()),
storage_type: T::Storage::STORAGE_TYPE,
is_send_and_sync: true,
type_id: Some(TypeId::of::<T>()),
Expand All @@ -206,7 +207,7 @@ impl ComponentDescriptor {
/// The [`StorageType`] for resources is always [`TableStorage`].
pub fn new_resource<T: Resource>() -> Self {
Self {
name: std::any::type_name::<T>().to_string(),
name: Cow::Borrowed(std::any::type_name::<T>()),
// PERF: `SparseStorage` may actually be a more
// reasonable choice as `storage_type` for resources.
storage_type: StorageType::Table,
Expand All @@ -219,7 +220,7 @@ impl ComponentDescriptor {

fn new_non_send<T: Any>(storage_type: StorageType) -> Self {
Self {
name: std::any::type_name::<T>().to_string(),
name: Cow::Borrowed(std::any::type_name::<T>()),
storage_type,
is_send_and_sync: false,
type_id: Some(TypeId::of::<T>()),
Expand All @@ -240,7 +241,7 @@ impl ComponentDescriptor {

#[inline]
pub fn name(&self) -> &str {
&self.name
self.name.as_ref()
}
}

Expand Down