From 7efa59be1676b82d1c969ccb0dcf06c5b2049092 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Sat, 29 Jul 2023 10:20:10 -0700 Subject: [PATCH] Reduce allocations by re-using vecs --- src/plugin/systems.rs | 84 +++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 30 deletions(-) diff --git a/src/plugin/systems.rs b/src/plugin/systems.rs index c9c41a92..63213803 100644 --- a/src/plugin/systems.rs +++ b/src/plugin/systems.rs @@ -132,6 +132,35 @@ pub fn apply_scale( } } +fn find_child_colliders( + base_entity: Entity, + colliders: &Query<&Collider>, + rigid_bodies: &Query<&RigidBody>, + childrens: &Query<&Children>, + + found: &mut Vec, + possibilities: &mut Vec, +) { + found.clear(); + possibilities.clear(); + possibilities.push(base_entity); + while let Some(entity) = possibilities.pop() { + if rigid_bodies.contains(entity) { + continue; + } + + if colliders.contains(entity) { + found.push(entity); + } + + if let Ok(children) = childrens.get(entity) { + possibilities.extend(children.iter()); + } else { + continue; + }; + } +} + /// System responsible for detecting changes in the hierarchy that would /// affect the collider's parent rigid body. pub fn collect_collider_hierarchy_changes( @@ -143,29 +172,10 @@ pub fn collect_collider_hierarchy_changes( rigid_bodies: Query<&RigidBody>, colliders: Query<&Collider>, mut collider_parents: Query<&mut ColliderParent>, -) { - let child_colliders = |entity: Entity| -> Vec { - let mut found = Vec::new(); - let mut possibilities = vec![entity]; - while let Some(entity) = possibilities.pop() { - if rigid_bodies.contains(entity) { - continue; - } - - if colliders.contains(entity) { - found.push(entity); - } - - if let Ok(children) = childrens.get(entity) { - possibilities.extend(children.iter()); - } else { - continue; - }; - } - - found - }; + mut found: Local>, + mut possibilities: Local>, +) { let parent_rigid_body = |mut entity: Entity| -> Option { loop { if rigid_bodies.contains(entity) { @@ -183,25 +193,39 @@ pub fn collect_collider_hierarchy_changes( for event in hierarchy_events.iter() { match event { HierarchyEvent::ChildAdded { child, .. } | HierarchyEvent::ChildMoved { child, .. } => { - let colliders = child_colliders(*child); let Some(rigid_body) = parent_rigid_body(*child) else { continue; }; + find_child_colliders( + *child, + &colliders, + &rigid_bodies, + &childrens, + &mut found, + &mut possibilities, + ); - for collider in colliders { + for collider in &found { let new_collider_parent = ColliderParent(rigid_body); - if let Ok(mut collider_parent) = collider_parents.get_mut(collider) { + if let Ok(mut collider_parent) = collider_parents.get_mut(*collider) { *collider_parent = new_collider_parent; } else { - commands.entity(collider).insert(new_collider_parent); + commands.entity(*collider).insert(new_collider_parent); } } } HierarchyEvent::ChildRemoved { child, .. } => { - let colliders = child_colliders(*child); - for collider in colliders { - if collider_parents.contains(collider) { - commands.entity(collider).remove::(); + find_child_colliders( + *child, + &colliders, + &rigid_bodies, + &childrens, + &mut found, + &mut possibilities, + ); + for collider in &found { + if collider_parents.contains(*collider) { + commands.entity(*collider).remove::(); } } }