Skip to content

Only get valid component ids#19510

Merged
alice-i-cecile merged 5 commits intobevyengine:mainfrom
ElliottjPierce:only-get-valid-component-ids
Jun 6, 2025
Merged

Only get valid component ids#19510
alice-i-cecile merged 5 commits intobevyengine:mainfrom
ElliottjPierce:only-get-valid-component-ids

Conversation

@ElliottjPierce
Copy link
Contributor

@ElliottjPierce ElliottjPierce commented Jun 6, 2025

Objective

Solution

  • Whenever we expect a component value to exist, we only care about fully registered components, not queued to be registered components since, for the value to exist, it must be registered.
  • So we can use the faster get_valid_* instead of get_* in a lot of places.
  • Also found a bug where valid_* did not forward to get_valid_* properly. That's fixed.

Testing

CI

@ElliottjPierce ElliottjPierce added C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events C-Performance A change motivated by improving speed, memory usage or compile times S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 6, 2025
/// Note that [`Self::get()`] may still return `Err` if the resource does not exist.
pub fn has_read<R: Resource>(&self) -> bool {
let component_id = self.world.components().resource_id::<R>();
let component_id = self.world.components().valid_resource_id::<R>();
Copy link
Contributor

Choose a reason for hiding this comment

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

To make sure I understand the reasoning on most of these: The component must be valid before it can actually be inserted. So if we're trying to access component data, then we can ignore queued component ids, since even with the ID we'd never find any data.

I don't think that argument applies to FilteredResources::has_read, FilteredResourcesMut::has_read, or FilteredResourcesMut::has_write. It's possible to grant access to this by component id, in which case it may have access to a component that's only queued. And I don't think these methods are called much, if at all, so it should be fine if they have slightly worse perf.

But it's not a big deal either way, since a user who wants the other behavior can always just get the ID and check the access themselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair. But IDK why anyone would make a FilteredResource, etc, and then not try to use it to get the resource.

But I'd be happy to revert those; that does seem more technically correct. Anyone have a strong opinion?

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer a revision for the filtered cases here. I think it's slightly more correct, and it's rare enough that the perf edge case doesn't bother me.

@alice-i-cecile alice-i-cecile added this to the 0.16.2 milestone Jun 6, 2025
@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 P-High This is particularly urgent, and deserves immediate attention and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 6, 2025
@alice-i-cecile alice-i-cecile enabled auto-merge June 6, 2025 20:46
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jun 6, 2025
Merged via the queue into bevyengine:main with commit 8ad7118 Jun 6, 2025
32 checks passed
VitalyAnkh pushed a commit to VitalyAnkh/bevy that referenced this pull request Jun 8, 2025
# Objective

- bevyengine#19504 showed a 11x regression in getting component values for
unregistered components. This pr should fix that and improve others a
little too.
- This is some cleanup work from bevyengine#18173 .

## Solution

- Whenever we expect a component value to exist, we only care about
fully registered components, not queued to be registered components
since, for the value to exist, it must be registered.
- So we can use the faster `get_valid_*` instead of `get_*` in a lot of
places.
- Also found a bug where `valid_*` did not forward to `get_valid_*`
properly. That's fixed.

## Testing

CI
@mockersf mockersf removed this from the 0.16.2 milestone Aug 18, 2025
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-Bug An unexpected or incorrect behavior C-Performance A change motivated by improving speed, memory usage or compile times P-High This is particularly urgent, and deserves immediate attention S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants