Add render world entity count to `EntityCountDiagnosticsPlugin`
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 171
Description
## What problem does this solve or what need does it fill?
Since we started retaining the render world, spawning new entities in the render world without `TemporaryRenderEntity` can be a source of entity/memory leaks.
## What solution would you like?
We have an [`EntityCountDiagnosticsPlugin`](https://docs.rs/bevy/latest/bevy/diagnostic/struct.EntityCountDiagnosticsPlugin.html) already. I think that we should add a count of render world entities to it. (this may not be possible)
Being able to see stats about all of your arbitrary subapps would be nice, really.
Here's a quick and dirty one that was good enough for my purposes:
Expand Code
```rust
const RENDER_WORLD_ENTITY_COUNT: DiagnosticPath =
DiagnosticPath::const_new("render_world_entity_count");
#[derive(Resource, Default)]
struct RenderWorldEntityCount(u32);
struct RenderWorldEntityCountPlugin;
impl Plugin for RenderWorldEntityCountPlugin {
fn build(&self, app: &mut App) {
app.register_diagnostic(Diagnostic::new(RENDER_WORLD_ENTITY_COUNT));
app.init_resource::();
app.add_systems(
Update,
|mut diagnostics: Diagnostics, entities: Res| {
diagnostics.add_measurement(&RENDER_WORLD_ENTITY_COUNT, || entities.0 as f64);
},
);
let render_app = app.sub_app_mut(RenderApp);
render_app.add_systems(ExtractSchedule, |world: &mut World| {
let entity_count = world.entities().len();
world
.resource_mut::()
.resource_mut::()
.0 = entity_count;
});
}
}
```
## What alternative(s) have you considered?
It might already be possible to add `EntityCountDiagnosticsPlugin` to the render world?
Maybe a lint in the upcoming `bevy_lint`, but it seems like that would be pretty difficult to implement?
Contributor guide
Research direction
Start with EntityCountDiagnosticsPlugin and the RenderApp/ExtractSchedule integration shown in the issue, then trace how diagnostics and MainWorld resources are handled across sub-apps. Done means the render world entity count is exposed through the existing diagnostics plugin, with the behavior verified in the relevant tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- observability
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 40/100