Skip to content

Commit daf92d8

Browse files
committed
Add a extend_from_iter method to RelationShipSourceCollection
1 parent 9e55dd8 commit daf92d8

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

crates/bevy_ecs/src/relationship/related_methods.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,13 @@ impl<'w> EntityWorldMut<'w> {
160160
let collection = target.collection_mut_risky();
161161
collection.clear();
162162

163-
// TODO: Optimize with extend
164-
for entity in entities_to_relate {
165-
collection.add(*entity);
166-
}
163+
collection.extend_from_iter(entities_to_relate.iter().copied());
167164
} else {
168165
let mut empty =
169166
<R::RelationshipTarget as RelationshipTarget>::Collection::with_capacity(
170167
entities_to_relate.len(),
171168
);
172-
// TODO: Optimize with extend
173-
for entity in entities_to_relate {
174-
empty.add(*entity);
175-
}
169+
empty.extend_from_iter(entities_to_relate.iter().copied());
176170

177171
// SAFETY: We've just initialized this collection
178172
self.insert_with_relationship_insert_hook_mode(

crates/bevy_ecs/src/relationship/relationship_source_collection.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ pub trait RelationshipSourceCollection {
4646
fn is_empty(&self) -> bool {
4747
self.len() == 0
4848
}
49+
50+
/// Add multiple entities to collection at once.
51+
///
52+
/// May be faster than repeatedly calling [`Self::add`].
53+
fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
54+
// The method name shouldn't conflict with `Extend::extend` as it's in the rust prelude and
55+
// would always conflict with it.
56+
for entity in entities {
57+
self.add(entity);
58+
}
59+
}
4960
}
5061

5162
impl RelationshipSourceCollection for Vec<Entity> {
@@ -82,6 +93,10 @@ impl RelationshipSourceCollection for Vec<Entity> {
8293
fn clear(&mut self) {
8394
self.clear();
8495
}
96+
97+
fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
98+
self.extend(entities);
99+
}
85100
}
86101

87102
impl RelationshipSourceCollection for EntityHashSet {
@@ -112,6 +127,10 @@ impl RelationshipSourceCollection for EntityHashSet {
112127
fn clear(&mut self) {
113128
self.0.clear();
114129
}
130+
131+
fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
132+
self.extend(entities);
133+
}
115134
}
116135

117136
impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
@@ -148,6 +167,10 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
148167
fn clear(&mut self) {
149168
self.clear();
150169
}
170+
171+
fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
172+
self.extend(entities);
173+
}
151174
}
152175

153176
impl RelationshipSourceCollection for Entity {
@@ -187,6 +210,12 @@ impl RelationshipSourceCollection for Entity {
187210
fn clear(&mut self) {
188211
*self = Entity::PLACEHOLDER;
189212
}
213+
214+
fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
215+
if let Some(entity) = entities.into_iter().last() {
216+
*self = entity;
217+
}
218+
}
190219
}
191220

192221
#[cfg(test)]

0 commit comments

Comments
 (0)