Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more methods to RelationshipSourceCollection #18421

Merged
merged 4 commits into from
Mar 20, 2025

Conversation

Brezak
Copy link
Contributor

@Brezak Brezak commented Mar 19, 2025

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.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide X-Uncontroversial This work is generally agreed upon D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 19, 2025
Copy link
Contributor

@chescock chescock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I left a few minor nits, but nothing that should block it.

@@ -16,9 +16,19 @@ pub trait RelationshipSourceCollection {
where
Self: 'a;

/// Creates a new empty instance.
fn new() -> Self;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this always be equivalent to Default::default()? If so, then I might vote for removing this method and putting Default as a supertrait.

... Ah, that wouldn't work for Entity.

It might help to have a comment explaining how this is different from Default::default(), although I'm not quite sure what to write there.

/// Reserves capacity for at least `additional` more entities to be inserted.
///
/// Not all collections support this operation, in which case it is a no-op.
fn reserve(&mut self, additional: usize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to offer default implementations of the optional methods? Even with_capacity could default to returning Self::new().

On the other hand, Entity is the only collection that doesn't implement them, so it wouldn't really save much.

Suggested change
fn reserve(&mut self, additional: usize);
fn reserve(&mut self, _additional: usize) {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using with_capacity in new or vice versa would require adding a Self: Sized bound. I decide to not bother and just implement the method manually everywhere.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 19, 2025
Co-authored-by: Chris Russell <[email protected]>
@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Mar 19, 2025
@alice-i-cecile
Copy link
Member

I think I got those merge conflicts right. @Brezak please yell at me and/or fix them if CI fails again <3

auto-merge was automatically disabled March 19, 2025 23:47

Head branch was pushed to by a user without write access

@Brezak
Copy link
Contributor Author

Brezak commented Mar 19, 2025

@alice-i-cecile formatting has been fixed but the auto-merge was disabled.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 20, 2025
Merged via the queue into bevyengine:main with commit 90ce1ee Mar 20, 2025
32 checks passed
@Brezak Brezak deleted the relationship-collection-shrink branch March 20, 2025 06:04
mockersf pushed a commit that referenced this pull request Mar 23, 2025
# 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 <[email protected]>
Co-authored-by: Chris Russell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants