Skip to content

Change Bundle::component_ids to return an iterator#21821

Merged
mockersf merged 9 commits intobevyengine:mainfrom
hymm:bundle-iter-component-ids
Nov 13, 2025
Merged

Change Bundle::component_ids to return an iterator#21821
mockersf merged 9 commits intobevyengine:mainfrom
hymm:bundle-iter-component-ids

Conversation

@hymm
Copy link
Copy Markdown
Contributor

@hymm hymm commented Nov 13, 2025

Objective

  • As part of get_components_mut #21780, I need a way to iterate over the component ids of a bundle for Entity*Except conflict checking without allocating. Pulled this out as it changes some unrelated code too.

Solution

  • Change Bundle::component_ids and Bundle::get_component_ids to return an iterator instead of taking a closure. In theory I would expect this to compile to the same asm. I would also argue that using an iterator is a more natural api for this than the closure. It probably took a closure before because expressing that the iterator doesn't capture the &mut ComponentRegistrator lifetime wasn't possible without the use syntax.
  • Removed some #[allow(deprecated)] in the Bundle macro that was missed.

Testing

  • Checked the asm for hook_on_add in the observers example for to confirm it was still the same. This is a pretty simple example though, so not sure how good of a check this is.
  • None of the code touched are in any hot paths, but ran the spawn and insert benches. Any changes seem to be in the noise.

}
}
let generics = ast.generics;
let generics_ty_list = generics.type_params().map(|p| p.ident.clone());
Copy link
Copy Markdown
Contributor Author

@hymm hymm Nov 13, 2025

Choose a reason for hiding this comment

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

I think this is the right way to get a iterator over the generic identifiers, but not very good at macros or syn, so someone else should probably confirm. ty_generics includes the angle brackets and is not a list.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Uncontroversial This work is generally agreed upon D-Macros Code that generates Rust code labels Nov 13, 2025
Copy link
Copy Markdown
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 checked the assembly myself for a simple method calling B::component_ids(components).collect(). And it got better! A bunch of calls to RawVec<T,A>::grow_one got removed, probably because once() and chain() impl TrustedLen.

@hymm hymm 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 Nov 13, 2025
@mockersf mockersf added this pull request to the merge queue Nov 13, 2025
Merged via the queue into bevyengine:main with commit 25aba2e Nov 13, 2025
37 of 38 checks passed
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Nov 15, 2025
# Objective

- As part of bevyengine#21780, I need a way to iterate over the component ids of a
bundle for `Entity*Except` conflict checking without allocating. Pulled
this out as it changes some unrelated code too.

## Solution

- Change `Bundle::component_ids` and `Bundle::get_component_ids` to
return an iterator instead of taking a closure. In theory I would expect
this to compile to the same asm. I would also argue that using an
iterator is a more natural api for this than the closure. It probably
took a closure before because expressing that the iterator doesn't
capture the `&mut ComponentRegistrator` lifetime wasn't possible without
the `use` syntax.
- Removed some #[allow(deprecated)] in the Bundle macro that was missed.

## Testing

- Checked the asm for `hook_on_add` in the observers example for to
confirm it was still the same. This is a pretty simple example though,
so not sure how good of a check this is.
- None of the code touched are in any hot paths, but ran the spawn and
insert benches. Any changes seem to be in the noise.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Nov 16, 2025
# Objective

- As part of bevyengine#21780, I need a way to iterate over the component ids of a
bundle for `Entity*Except` conflict checking without allocating. Pulled
this out as it changes some unrelated code too.

## Solution

- Change `Bundle::component_ids` and `Bundle::get_component_ids` to
return an iterator instead of taking a closure. In theory I would expect
this to compile to the same asm. I would also argue that using an
iterator is a more natural api for this than the closure. It probably
took a closure before because expressing that the iterator doesn't
capture the `&mut ComponentRegistrator` lifetime wasn't possible without
the `use` syntax.
- Removed some #[allow(deprecated)] in the Bundle macro that was missed.

## Testing

- Checked the asm for `hook_on_add` in the observers example for to
confirm it was still the same. This is a pretty simple example though,
so not sure how good of a check this is.
- None of the code touched are in any hot paths, but ran the spawn and
insert benches. Any changes seem to be in the noise.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Nov 16, 2025
# Objective

- As part of bevyengine#21780, I need a way to iterate over the component ids of a
bundle for `Entity*Except` conflict checking without allocating. Pulled
this out as it changes some unrelated code too.

## Solution

- Change `Bundle::component_ids` and `Bundle::get_component_ids` to
return an iterator instead of taking a closure. In theory I would expect
this to compile to the same asm. I would also argue that using an
iterator is a more natural api for this than the closure. It probably
took a closure before because expressing that the iterator doesn't
capture the `&mut ComponentRegistrator` lifetime wasn't possible without
the `use` syntax.
- Removed some #[allow(deprecated)] in the Bundle macro that was missed.

## Testing

- Checked the asm for `hook_on_add` in the observers example for to
confirm it was still the same. This is a pretty simple example though,
so not sure how good of a check this is.
- None of the code touched are in any hot paths, but ran the spawn and
insert benches. Any changes seem to be in the noise.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Nov 18, 2025
# Objective

- As part of bevyengine#21780, I need a way to iterate over the component ids of a
bundle for `Entity*Except` conflict checking without allocating. Pulled
this out as it changes some unrelated code too.

## Solution

- Change `Bundle::component_ids` and `Bundle::get_component_ids` to
return an iterator instead of taking a closure. In theory I would expect
this to compile to the same asm. I would also argue that using an
iterator is a more natural api for this than the closure. It probably
took a closure before because expressing that the iterator doesn't
capture the `&mut ComponentRegistrator` lifetime wasn't possible without
the `use` syntax.
- Removed some #[allow(deprecated)] in the Bundle macro that was missed.

## Testing

- Checked the asm for `hook_on_add` in the observers example for to
confirm it was still the same. This is a pretty simple example though,
so not sure how good of a check this is.
- None of the code touched are in any hot paths, but ran the spawn and
insert benches. Any changes seem to be in the noise.
@hymm hymm deleted the bundle-iter-component-ids branch November 18, 2025 22:15
beicause pushed a commit to beicause/bevy that referenced this pull request Nov 26, 2025
# Objective

- As part of bevyengine#21780, I need a way to iterate over the component ids of a
bundle for `Entity*Except` conflict checking without allocating. Pulled
this out as it changes some unrelated code too.

## Solution

- Change `Bundle::component_ids` and `Bundle::get_component_ids` to
return an iterator instead of taking a closure. In theory I would expect
this to compile to the same asm. I would also argue that using an
iterator is a more natural api for this than the closure. It probably
took a closure before because expressing that the iterator doesn't
capture the `&mut ComponentRegistrator` lifetime wasn't possible without
the `use` syntax.
- Removed some #[allow(deprecated)] in the Bundle macro that was missed.

## Testing

- Checked the asm for `hook_on_add` in the observers example for to
confirm it was still the same. This is a pretty simple example though,
so not sure how good of a check this is.
- None of the code touched are in any hot paths, but ran the spawn and
insert benches. Any changes seem to be in the noise.
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-Code-Quality A section of code that is hard to understand or change C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Macros Code that generates Rust code 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.

5 participants