Skip to content

Commit bbbaa5e

Browse files
committed
Reduce allocations by re-using vecs
1 parent e77d3eb commit bbbaa5e

File tree

1 file changed

+54
-30
lines changed

1 file changed

+54
-30
lines changed

src/plugin/systems.rs

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,35 @@ pub fn apply_scale(
119119
}
120120
}
121121

122+
fn find_child_colliders(
123+
base_entity: Entity,
124+
colliders: &Query<&Collider>,
125+
rigid_bodies: &Query<&RigidBody>,
126+
childrens: &Query<&Children>,
127+
128+
found: &mut Vec<Entity>,
129+
possibilities: &mut Vec<Entity>,
130+
) {
131+
found.clear();
132+
possibilities.clear();
133+
possibilities.push(base_entity);
134+
while let Some(entity) = possibilities.pop() {
135+
if rigid_bodies.contains(entity) {
136+
continue;
137+
}
138+
139+
if colliders.contains(entity) {
140+
found.push(entity);
141+
}
142+
143+
if let Ok(children) = childrens.get(entity) {
144+
possibilities.extend(children.iter());
145+
} else {
146+
continue;
147+
};
148+
}
149+
}
150+
122151
/// System responsible for detecting changes in the hierarchy that would
123152
/// affect the collider's parent rigid body.
124153
pub fn collect_collider_hierarchy_changes(
@@ -130,29 +159,10 @@ pub fn collect_collider_hierarchy_changes(
130159
rigid_bodies: Query<&RigidBody>,
131160
colliders: Query<&Collider>,
132161
mut collider_parents: Query<&mut ColliderParent>,
133-
) {
134-
let child_colliders = |entity: Entity| -> Vec<Entity> {
135-
let mut found = Vec::new();
136-
let mut possibilities = vec![entity];
137-
while let Some(entity) = possibilities.pop() {
138-
if rigid_bodies.contains(entity) {
139-
continue;
140-
}
141-
142-
if colliders.contains(entity) {
143-
found.push(entity);
144-
}
145-
146-
if let Ok(children) = childrens.get(entity) {
147-
possibilities.extend(children.iter());
148-
} else {
149-
continue;
150-
};
151-
}
152-
153-
found
154-
};
155162

163+
mut found: Local<Vec<Entity>>,
164+
mut possibilities: Local<Vec<Entity>>,
165+
) {
156166
let parent_rigid_body = |mut entity: Entity| -> Option<Entity> {
157167
loop {
158168
if rigid_bodies.contains(entity) {
@@ -170,25 +180,39 @@ pub fn collect_collider_hierarchy_changes(
170180
for event in hierarchy_events.iter() {
171181
match event {
172182
HierarchyEvent::ChildAdded { child, .. } | HierarchyEvent::ChildMoved { child, .. } => {
173-
let colliders = child_colliders(*child);
174183
let Some(rigid_body) = parent_rigid_body(*child) else {
175184
continue;
176185
};
186+
find_child_colliders(
187+
*child,
188+
&colliders,
189+
&rigid_bodies,
190+
&childrens,
191+
&mut found,
192+
&mut possibilities,
193+
);
177194

178-
for collider in colliders {
195+
for collider in &found {
179196
let new_collider_parent = ColliderParent(rigid_body);
180-
if let Ok(mut collider_parent) = collider_parents.get_mut(collider) {
197+
if let Ok(mut collider_parent) = collider_parents.get_mut(*collider) {
181198
*collider_parent = new_collider_parent;
182199
} else {
183-
commands.entity(collider).insert(new_collider_parent);
200+
commands.entity(*collider).insert(new_collider_parent);
184201
}
185202
}
186203
}
187204
HierarchyEvent::ChildRemoved { child, .. } => {
188-
let colliders = child_colliders(*child);
189-
for collider in colliders {
190-
if collider_parents.contains(collider) {
191-
commands.entity(collider).remove::<ColliderParent>();
205+
find_child_colliders(
206+
*child,
207+
&colliders,
208+
&rigid_bodies,
209+
&childrens,
210+
&mut found,
211+
&mut possibilities,
212+
);
213+
for collider in &found {
214+
if collider_parents.contains(*collider) {
215+
commands.entity(*collider).remove::<ColliderParent>();
192216
}
193217
}
194218
}

0 commit comments

Comments
 (0)