Skip to content

Commit 90ce1ee

Browse files
Brezakalice-i-cecilechescock
authored
Add more methods to RelationshipSourceCollection (#18421)
# Objective While working on #18058 I realized I could use `RelationshipTargetCollection::new`, so I added it. ## Solution - Add `RelationshipTargetCollection::new` - Add `RelationshipTargetCollection::reserve`. Could generally be useful when doing micro-optimizations. - Add `RelationshipTargetCollection::shrink_to_fit`. Rust collections generally don't shrink when removing elements. Might be a good idea to call this once in a while. ## Testing `cargo clippy` --- ## Showcase `RelationshipSourceCollection` now implements `new`, `reserve` and `shrink_to_fit` to give greater control over how much memory it consumes. ## Migration Guide Any type implementing `RelationshipSourceCollection` now needs to also implement `new`, `reserve` and `shrink_to_fit`. `reserve` and `shrink_to_fit` can be made no-ops if they conceptually mean nothing to a collection. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: Chris Russell <8494645+chescock@users.noreply.github.com>
1 parent 655ee4b commit 90ce1ee

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

crates/bevy_ecs/src/relationship/relationship_source_collection.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,19 @@ pub trait RelationshipSourceCollection {
1616
where
1717
Self: 'a;
1818

19+
/// Creates a new empty instance.
20+
fn new() -> Self;
21+
1922
/// Returns an instance with the given pre-allocated entity `capacity`.
23+
///
24+
/// Some collections will ignore the provided `capacity` and return a default instance.
2025
fn with_capacity(capacity: usize) -> Self;
2126

27+
/// Reserves capacity for at least `additional` more entities to be inserted.
28+
///
29+
/// Not all collections support this operation, in which case it is a no-op.
30+
fn reserve(&mut self, additional: usize);
31+
2232
/// Adds the given `entity` to the collection.
2333
///
2434
/// Returns whether the entity was added to the collection.
@@ -41,6 +51,11 @@ pub trait RelationshipSourceCollection {
4151
/// Clears the collection.
4252
fn clear(&mut self);
4353

54+
/// Attempts to save memory by shrinking the capacity to fit the current length.
55+
///
56+
/// This operation is a no-op for collections that do not support it.
57+
fn shrink_to_fit(&mut self);
58+
4459
/// Returns true if the collection contains no entities.
4560
#[inline]
4661
fn is_empty(&self) -> bool {
@@ -62,6 +77,14 @@ pub trait RelationshipSourceCollection {
6277
impl RelationshipSourceCollection for Vec<Entity> {
6378
type SourceIter<'a> = core::iter::Copied<core::slice::Iter<'a, Entity>>;
6479

80+
fn new() -> Self {
81+
Vec::new()
82+
}
83+
84+
fn reserve(&mut self, additional: usize) {
85+
Vec::reserve(self, additional);
86+
}
87+
6588
fn with_capacity(capacity: usize) -> Self {
6689
Vec::with_capacity(capacity)
6790
}
@@ -94,6 +117,10 @@ impl RelationshipSourceCollection for Vec<Entity> {
94117
self.clear();
95118
}
96119

120+
fn shrink_to_fit(&mut self) {
121+
Vec::shrink_to_fit(self);
122+
}
123+
97124
fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
98125
self.extend(entities);
99126
}
@@ -102,6 +129,14 @@ impl RelationshipSourceCollection for Vec<Entity> {
102129
impl RelationshipSourceCollection for EntityHashSet {
103130
type SourceIter<'a> = core::iter::Copied<crate::entity::hash_set::Iter<'a>>;
104131

132+
fn new() -> Self {
133+
EntityHashSet::new()
134+
}
135+
136+
fn reserve(&mut self, additional: usize) {
137+
self.0.reserve(additional);
138+
}
139+
105140
fn with_capacity(capacity: usize) -> Self {
106141
EntityHashSet::with_capacity(capacity)
107142
}
@@ -128,6 +163,10 @@ impl RelationshipSourceCollection for EntityHashSet {
128163
self.0.clear();
129164
}
130165

166+
fn shrink_to_fit(&mut self) {
167+
self.0.shrink_to_fit();
168+
}
169+
131170
fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
132171
self.extend(entities);
133172
}
@@ -136,6 +175,14 @@ impl RelationshipSourceCollection for EntityHashSet {
136175
impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
137176
type SourceIter<'a> = core::iter::Copied<core::slice::Iter<'a, Entity>>;
138177

178+
fn new() -> Self {
179+
SmallVec::new()
180+
}
181+
182+
fn reserve(&mut self, additional: usize) {
183+
SmallVec::reserve(self, additional);
184+
}
185+
139186
fn with_capacity(capacity: usize) -> Self {
140187
SmallVec::with_capacity(capacity)
141188
}
@@ -168,6 +215,10 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
168215
self.clear();
169216
}
170217

218+
fn shrink_to_fit(&mut self) {
219+
SmallVec::shrink_to_fit(self);
220+
}
221+
171222
fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
172223
self.extend(entities);
173224
}
@@ -176,10 +227,16 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
176227
impl RelationshipSourceCollection for Entity {
177228
type SourceIter<'a> = core::iter::Once<Entity>;
178229

179-
fn with_capacity(_capacity: usize) -> Self {
230+
fn new() -> Self {
180231
Entity::PLACEHOLDER
181232
}
182233

234+
fn reserve(&mut self, _: usize) {}
235+
236+
fn with_capacity(_capacity: usize) -> Self {
237+
Self::new()
238+
}
239+
183240
fn add(&mut self, entity: Entity) -> bool {
184241
*self = entity;
185242

@@ -211,6 +268,8 @@ impl RelationshipSourceCollection for Entity {
211268
*self = Entity::PLACEHOLDER;
212269
}
213270

271+
fn shrink_to_fit(&mut self) {}
272+
214273
fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
215274
if let Some(entity) = entities.into_iter().last() {
216275
*self = entity;

0 commit comments

Comments
 (0)