From 36bfd4159cb1bd8b3d47a834824465728dfb5bd8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 20 Dec 2024 20:19:12 +0100 Subject: perf(ecs): use component index map when creating component sequences --- ecs/src/component.rs | 77 ++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 45 deletions(-) (limited to 'ecs/src/component.rs') diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 5da510a..85c556f 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -179,19 +179,17 @@ pub trait Sequence fn removed_event_ids() -> Vec; fn from_components_mut<'component>( - components: impl Iterator, + components: &'component [EntityComponent], + component_index_lookup: impl Fn(Uid) -> Option, world: &'component World, - lock_component: fn( - entity_component: &EntityComponent, - ) -> WriteGuard<'_, Box>, + lock_component: fn(&EntityComponent) -> WriteGuard<'_, Box>, ) -> Self::MutRefs<'component>; fn from_components<'component>( - components: impl Iterator, + components: &'component [EntityComponent], + component_index_lookup: impl Fn(Uid) -> Option, world: &'component World, - lock_component: fn( - entity_component: &EntityComponent, - ) -> ReadGuard<'_, Box>, + lock_component: fn(&EntityComponent) -> ReadGuard<'_, Box>, ) -> Self::Refs<'component>; } @@ -304,56 +302,45 @@ macro_rules! inner { ] } + #[inline] fn from_components_mut<'component>( - components: impl Iterator, + components: &'component [EntityComponent], + component_index_lookup: impl Fn(Uid) -> Option, world: &'component World, - lock_component: fn( - entity_component: &EntityComponent, - ) -> WriteGuard<'_, Box>, + lock_component: + fn(&EntityComponent) -> WriteGuard<'_, Box>, ) -> Self::MutRefs<'component> { - #( - let mut comp_~I: Option>> = None; - )* - - for comp in components { - #( - if comp.id == Comp~I::Component::id() { - comp_~I = Some(lock_component(comp)); - continue; - } - )* - } - (#( - Comp~I::RefMut::from_optional_mut_component(comp_~I, world), + Comp~I::RefMut::from_optional_mut_component( + component_index_lookup(Comp~I::Component::id()) + .and_then(|component_index| { + components.get(component_index) + .map(lock_component) + }), + world + ), )*) } + #[inline] fn from_components<'component>( - components: impl Iterator, + components: &'component [EntityComponent], + component_index_lookup: impl Fn(Uid) -> Option, world: &'component World, - lock_component: fn( - entity_component: &EntityComponent, - ) -> ReadGuard<'_, Box>, + lock_component: + fn(&EntityComponent) -> ReadGuard<'_, Box>, ) -> Self::Refs<'component> { - - #( - let mut comp_~I: Option>> = None; - )* - - for comp in components { - #( - if comp.id == Comp~I::Component::id() { - comp_~I = Some(lock_component(comp)); - continue; - } - )* - } - (#( - Comp~I::Ref::from_optional_component(comp_~I, world), + Comp~I::Ref::from_optional_component( + component_index_lookup(Comp~I::Component::id()) + .and_then(|component_index| { + components.get(component_index) + .map(lock_component) + }), + world + ), )*) } } -- cgit v1.2.3-18-g5258