diff options
author | HampusM <hampus@hampusmat.com> | 2024-05-24 19:00:38 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-05-24 19:11:11 +0200 |
commit | b8417d20765755cfa2cecacb11c77e3abbafd546 (patch) | |
tree | 5f62502eeb846faba66bd204f8dc028ef5f7aa5e /ecs | |
parent | 36886e343781bf0ddf7458d5c6db5b5724c918e4 (diff) |
fix(ecs): prevent unnecessary locking in Sequence::from_components
Diffstat (limited to 'ecs')
-rw-r--r-- | ecs/src/component.rs | 19 | ||||
-rw-r--r-- | ecs/src/lib.rs | 9 | ||||
-rw-r--r-- | ecs/src/query.rs | 2 |
3 files changed, 17 insertions, 13 deletions
diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 604f54d..9310995 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -3,9 +3,10 @@ use std::fmt::Debug; use seq_macro::seq; -use crate::lock::{Lock, WriteGuard}; +use crate::lock::WriteGuard; use crate::system::{ComponentRefMut, Input as SystemInput}; use crate::type_name::TypeName; +use crate::EntityComponent; pub mod local; @@ -112,7 +113,7 @@ pub trait Sequence fn type_ids() -> Vec<(TypeId, IsOptional)>; fn from_components<'component>( - components: impl Iterator<Item = &'component Lock<Box<dyn Component>>>, + components: impl Iterator<Item = &'component EntityComponent>, ) -> Self::Refs<'component>; } @@ -180,7 +181,7 @@ macro_rules! inner { } fn from_components<'component>( - components: impl Iterator<Item = &'component Lock<Box<dyn Component>>>, + components: impl Iterator<Item = &'component EntityComponent>, ) -> Self::Refs<'component> { #( @@ -188,12 +189,14 @@ macro_rules! inner { )* for comp in components { - let comp_ref = comp - .write_nonblock() - .expect("Failed to acquire read-write component lock"); - #( - if comp_ref.is::<Comp~I::Component>() { + if comp.id == TypeId::of::<Comp~I::Component>() { + let comp_ref = comp.component + .write_nonblock() + .expect( + "Failed to acquire read-write component lock" + ); + comp_~I = Some(comp_ref); continue; } diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index c2fc9c7..fbcc451 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -44,11 +44,12 @@ struct Entity } #[derive(Debug)] -struct EntityComponent +#[non_exhaustive] +pub struct EntityComponent { - id: TypeId, - component: Lock<Box<dyn Component>>, - drop_last: bool, + pub id: TypeId, + pub component: Lock<Box<dyn Component>>, + pub drop_last: bool, } #[derive(Debug, Default)] diff --git a/ecs/src/query.rs b/ecs/src/query.rs index 61e2797..73792b3 100644 --- a/ecs/src/query.rs +++ b/ecs/src/query.rs @@ -211,7 +211,7 @@ where matching_entity .components .iter() - .map(|component| &component.component), + .map(|component| &**component), )) } } |