Skip to content

Add #[inline] and #repr[transparent] to ComponentIdSet to attempt to fix perf regression#23471

Merged
alice-i-cecile merged 2 commits intobevyengine:mainfrom
chescock:ComponentIdSet-inline
Apr 6, 2026
Merged

Add #[inline] and #repr[transparent] to ComponentIdSet to attempt to fix perf regression#23471
alice-i-cecile merged 2 commits intobevyengine:mainfrom
chescock:ComponentIdSet-inline

Conversation

@chescock
Copy link
Copy Markdown
Contributor

Objective

Attempt to fix a performance regression from adding the ComponentSet type in #23384. See #23464.

Solution

Add #[inline] attributes to the new trait impls. I had added them to the inherent methods, but forgot about the traits!

Testing

I have not tested this at all, so I don't know whether it will actually fix the issue! I don't even understand how #23384 could have affected those benchmarks in the first place, since they don't seem to call any methods on Access during the loop.

But these trait methods all simply delegate to methods on FixedBitSet, so marking them #[inline] seems harmless at worst.

@chescock chescock added D-Trivial Nice and easy! A great choice to get started with Bevy 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 Mar 22, 2026
@github-project-automation github-project-automation bot moved this to Needs SME Triage in ECS Mar 22, 2026
@alice-i-cecile alice-i-cecile added S-Needs-Benchmarking This set of changes needs performance benchmarking to double-check that they help and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 22, 2026
@alice-i-cecile alice-i-cecile added this to the 0.19 milestone Mar 22, 2026
@alice-i-cecile
Copy link
Copy Markdown
Member

@mockersf do you want to try merging this and see what happens?

@Bluefinger
Copy link
Copy Markdown
Contributor

Bluefinger commented Mar 22, 2026

I might suggest adding #[repr(transparent)] to ComponentIdSet, otherwise some optimisations from #[inline] don't occur if rustc can't infer them (since these optimisations tend to be layout-dependent)

@alice-i-cecile alice-i-cecile changed the title Add #[inline] attributes to trait impls for ComponentIdSet Add #[inline] and #repr[transparent] to ComponentIdSet to attempt to fix perf regression Mar 22, 2026
Copy link
Copy Markdown
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

I have no complaints about doing this, and I'm fine to let our automated benchmarking test this.


#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(ComponentId::new)
Copy link
Copy Markdown
Contributor

@Bluefinger Bluefinger Mar 23, 2026

Choose a reason for hiding this comment

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

I think .map doesn't always output the best gen code (I need to check with godbolt), and I keep seeing other codebases that a match statement is better for codegen/optimisation. It's very small improvements, but I saw some of it with me optimising my nailpit project.

Example from my codebase. This form ended up being the "best" from what I could profile/benchmark for this part of the codebase.

These matter more for code in hot-loops, which would be the iterator .next code, etc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I added -C opt-level=3 -C target-cpu=x86-64-v3 to each, and they both compile down to the same optimised asm:

map: https://godbolt.org/z/nMzrbT834
match: https://godbolt.org/z/dhbKv4MEh

I think the .map advice is more for optimising compile times rather than runtime speed, by the looks of it.

@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-Benchmarking This set of changes needs performance benchmarking to double-check that they help labels Apr 1, 2026
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Apr 6, 2026
Merged via the queue into bevyengine:main with commit fcabe2a Apr 6, 2026
40 checks passed
@github-project-automation github-project-automation bot moved this from Needs SME Triage to Done in ECS Apr 6, 2026
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-Performance A change motivated by improving speed, memory usage or compile times D-Trivial Nice and easy! A great choice to get started with Bevy S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants