diff options
author | HampusM <hampus@hampusmat.com> | 2024-12-20 20:19:12 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-12-20 21:33:13 +0100 |
commit | 36bfd4159cb1bd8b3d47a834824465728dfb5bd8 (patch) | |
tree | 89efb158591ffacce09e72b2a0e02208e12f31b9 /ecs/src/component.rs | |
parent | be7d20f0c57cc834e943426090fe2debf76ca98d (diff) |
perf(ecs): use component index map when creating component sequences
Diffstat (limited to 'ecs/src/component.rs')
-rw-r--r-- | ecs/src/component.rs | 77 |
1 files changed, 32 insertions, 45 deletions
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<Uid>; fn from_components_mut<'component>( - components: impl Iterator<Item = &'component EntityComponent>, + components: &'component [EntityComponent], + component_index_lookup: impl Fn(Uid) -> Option<usize>, world: &'component World, - lock_component: fn( - entity_component: &EntityComponent, - ) -> WriteGuard<'_, Box<dyn Component>>, + lock_component: fn(&EntityComponent) -> WriteGuard<'_, Box<dyn Component>>, ) -> Self::MutRefs<'component>; fn from_components<'component>( - components: impl Iterator<Item = &'component EntityComponent>, + components: &'component [EntityComponent], + component_index_lookup: impl Fn(Uid) -> Option<usize>, world: &'component World, - lock_component: fn( - entity_component: &EntityComponent, - ) -> ReadGuard<'_, Box<dyn Component>>, + lock_component: fn(&EntityComponent) -> ReadGuard<'_, Box<dyn Component>>, ) -> Self::Refs<'component>; } @@ -304,56 +302,45 @@ macro_rules! inner { ] } + #[inline] fn from_components_mut<'component>( - components: impl Iterator<Item = &'component EntityComponent>, + components: &'component [EntityComponent], + component_index_lookup: impl Fn(Uid) -> Option<usize>, world: &'component World, - lock_component: fn( - entity_component: &EntityComponent, - ) -> WriteGuard<'_, Box<dyn Component>>, + lock_component: + fn(&EntityComponent) -> WriteGuard<'_, Box<dyn Component>>, ) -> Self::MutRefs<'component> { - #( - let mut comp_~I: Option<WriteGuard<Box<dyn Component>>> = 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<Item = &'component EntityComponent>, + components: &'component [EntityComponent], + component_index_lookup: impl Fn(Uid) -> Option<usize>, world: &'component World, - lock_component: fn( - entity_component: &EntityComponent, - ) -> ReadGuard<'_, Box<dyn Component>>, + lock_component: + fn(&EntityComponent) -> ReadGuard<'_, Box<dyn Component>>, ) -> Self::Refs<'component> { - - #( - let mut comp_~I: Option<ReadGuard<Box<dyn Component>>> = 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 + ), )*) } } |