Skip to content

Commit 44b18b1

Browse files
committed
wip reuse implementation from insert children for push children
1 parent a17e8a2 commit 44b18b1

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

crates/bevy_transform/src/hierarchy/child_builder.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,13 @@ pub struct ChildBuilder<'a> {
6363

6464
impl Command for PushChildren {
6565
fn write(self: Box<Self>, world: &mut World, _resources: &mut Resources) {
66-
let mut childset = HashSet::default();
67-
68-
let mut new_children = if let Ok(children) = world.get::<Children>(self.parent) {
69-
childset.extend(children.iter().copied());
70-
children.0.clone()
71-
} else {
72-
Default::default()
73-
};
74-
7566
for child in self.children.iter() {
76-
if !childset.contains(child) {
77-
new_children.push(*child);
78-
}
79-
8067
if let Ok(Parent(old_parent)) = world.get::<Parent>(*child) {
8168
let old_parent = *old_parent;
8269

8370
// clean old parent of children references
8471
if let Ok(mut children) = world.get_mut::<Children>(old_parent) {
85-
let vec = children
86-
.iter()
87-
.filter_map(|c| if c != child { Some(*c) } else { None })
88-
.collect();
72+
let vec = children.iter().copied().filter(|c| c != child).collect();
8973
children.0 = vec;
9074
}
9175

@@ -99,9 +83,21 @@ impl Command for PushChildren {
9983
}
10084
}
10185

102-
world
103-
.insert_one(self.parent, Children(self.children))
104-
.unwrap();
86+
if let Ok(mut children) = world.get_mut::<Children>(self.parent) {
87+
let vec = &mut children.0;
88+
let index = vec.len();
89+
90+
// note that for cases with many children a HashSet might be better for contains() check
91+
for child in self.children.iter().rev() {
92+
if !vec.contains(child) {
93+
vec.insert(index, *child);
94+
}
95+
}
96+
} else {
97+
world
98+
.insert_one(self.parent, Children(self.children))
99+
.unwrap();
100+
};
105101
}
106102
}
107103

0 commit comments

Comments
 (0)