From 2a8718f7c671ab1fc5e38340b467e2bd77f16cc0 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 9 Feb 2025 18:41:26 +0100 Subject: perf(ecs): replace component::Sequence::into_vec fn with into_array --- ecs/src/actions.rs | 4 ++-- ecs/src/component.rs | 16 +++++++++++----- ecs/src/lib.rs | 2 +- ecs/src/util.rs | 1 + 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/ecs/src/actions.rs b/ecs/src/actions.rs index 3988a77..89fd84a 100644 --- a/ecs/src/actions.rs +++ b/ecs/src/actions.rs @@ -24,7 +24,7 @@ impl<'world> Actions<'world> pub fn spawn(&mut self, components: Comps) { self.action_queue.push(Action::Spawn( - components.into_vec(), + components.into_array().into(), EventIds { ids: Comps::added_event_ids() }, )); } @@ -50,7 +50,7 @@ impl<'world> Actions<'world> self.action_queue.push(Action::AddComponents( entity_uid, - components.into_vec(), + components.into_array().into(), EventIds { ids: Comps::added_event_ids() }, )); } diff --git a/ecs/src/component.rs b/ecs/src/component.rs index f5aa0d1..b2ecf80 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -167,7 +167,9 @@ pub trait Sequence /// The number of components in this component sequence. const COUNT: usize; - fn into_vec(self) -> Vec>; + type Array: Array>; + + fn into_array(self) -> Self::Array; fn metadata() -> impl Array; @@ -277,9 +279,11 @@ macro_rules! inner { { const COUNT: usize = $c + 1; - fn into_vec(self) -> Vec> + type Array = [Box; $c + 1]; + + fn into_array(self) -> Self::Array { - Vec::from_iter([#(Box::new(self.I) as Box,)*]) + [#(Box::new(self.I) as Box,)*] } fn metadata() -> impl Array @@ -356,11 +360,13 @@ seq!(C in 0..=16 { impl Sequence for () { + type Array = [Box; 0]; + const COUNT: usize = 0; - fn into_vec(self) -> Vec> + fn into_array(self) -> Self::Array { - Vec::new() + [] } fn metadata() -> impl Array diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index ae3e191..72b5cf9 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -115,7 +115,7 @@ impl World .component_storage .write_nonblock() .expect("Failed to acquire read-write component storage lock") - .push_entity(entity_uid, components.into_vec()) + .push_entity(entity_uid, components.into_array().into()) { tracing::error!("Failed to create entity: {err}"); diff --git a/ecs/src/util.rs b/ecs/src/util.rs index 4ba8597..0273b18 100644 --- a/ecs/src/util.rs +++ b/ecs/src/util.rs @@ -4,6 +4,7 @@ pub trait Array: AsRef<[Item]> + AsMut<[Item]> + IntoIterator + + Into> + Sortable + sealed::Sealed { -- cgit v1.2.3-18-g5258