-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
bevy_render: Reserve only entities from the last frame #4322
Conversation
@@ -107,6 +107,10 @@ pub struct RenderApp; | |||
#[derive(Default)] | |||
struct ScratchRenderWorld(World); | |||
|
|||
/// The number of entities used in the render world in the previous frame. | |||
#[derive(Default)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it may make sense to initialize this at something low but not tiny, like perhaps 100?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initializing would only work if rendering entities are added in the first frame. I think a minimum or offset applied when reserving would probably be better.
This strategy makes sense to me: it's certainly better than the existing solution. I think we can actually just compute the exact number of entities to reserve quickly and correctly, which should be preferred (see #3953), but this PR has my approval if that doesn't pan out. |
Sounds good to me. |
Yeah, it should be possible to use #4244 in addition with a Note the same problem has been encountered and solved similarly in the extract_mesh system in |
crates/bevy_render/src/lib.rs
Outdated
render_app | ||
.world | ||
.resource_mut::<NumberOfRenderingEntitiesToReserve>() | ||
.0 = render_app.world.entities().meta.len() as u32; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
render_app | |
.world | |
.resource_mut::<NumberOfRenderingEntitiesToReserve>() | |
.0 = render_app.world.entities().meta.len() as u32; | |
let mut entities_to_reserve = render_app | |
.world | |
.resource_mut::<NumberOfRenderingEntitiesToReserve>(); | |
entities_to_reserve.0 = render_app.world.entities().meta.len() as u32; |
The PR looks good. But I find assignments at the end of field access chains a bit difficult to read. It could be improved this way.
#4244 is merged now; can you update this PR to use it? |
@alice-i-cecile As I mentioned in my earlier reply. Size hint is a double-edged sword. As soon as there is filtering based on non-archetype criteria, it goes woop. I did an analysis of the impact of size hints on another PR: #4240 To make sure the benefits are there, I suggest benchmarking. |
Ah right, good choice. In that case, I'm in favor of merging this as is and creating an issue to investigate this further. The existing strategy is clearly poor. I'll defer to @superdump on the final call though. @zyllian can you rebase this? |
@@ -187,13 +192,13 @@ impl Plugin for RenderPlugin { | |||
bevy_utils::tracing::info_span!("stage", name = "reserve_and_flush") | |||
.entered(); | |||
|
|||
// reserve all existing app entities for use in render_app | |||
// reserve the number of entities used in the previous frame for use in render_app |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of reserving the entities is so that the same entity from the main world can be used to refer to an entity in the render world. If we don’t reserve the entire active main world entity address space, something could be spawned after what is reserved and then something else is supposed to refer to that. I’m not convinced this change is safe to do. I’ll need to think about it some more. Maybe @cart can beat me to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup the approach in this PR breaks the intended behavior. This line isn't about reserving space for rendering entities. Its about ensuring that rendering entities don't get spawned in the "current app id space".
We should not merge this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I made the change I was expecting it to not work at all so I could learn more about Bevy's internals. I'm curious why it doesn't obviously break things, though.
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Objective
Solution