-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[Merged by Bors] - bevy_render: Batch insertion for prepare_uniform_components #4179
[Merged by Bors] - bevy_render: Batch insertion for prepare_uniform_components #4179
Conversation
--- a/crates/bevy_render/src/render_component.rs
+++ b/crates/bevy_render/src/render_component.rs
@@ -107,22 +107,22 @@ fn prepare_uniform_components<C: Component>(
render_queue: Res<RenderQueue>,
mut component_uniforms: ResMut<ComponentUniforms<C>>,
components: Query<(Entity, &C)>,
- mut prev_len: Local<usize>,
) where
C: AsStd140 + Clone,
{
component_uniforms.uniforms.clear();
- let mut entities = Vec::with_capacity(*prev_len);
- for (entity, component) in components.iter() {
- entities.push((
- entity,
- (DynamicUniformIndex::<C> {
- index: component_uniforms.uniforms.push(component.clone()),
- marker: PhantomData,
- },),
- ));
- }
- *prev_len = entities.len();
+ let entities = components
+ .iter()
+ .map(|(entity, component)| {
+ (
+ entity,
+ (DynamicUniformIndex::<C> {
+ index: component_uniforms.uniforms.push(component.clone()),
+ marker: PhantomData,
+ },),
+ )
+ })
+ .collect::<Vec<_>>();
commands.insert_or_spawn_batch(entities); is even faster on my side |
I didn't see any practical difference but I think it makes sense anyway. |
},), | ||
) | ||
}) | ||
.collect::<Vec<_>>(); |
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.
Try using a Local<Vec<...>>
here and clearing every frame instead of allocating a new Vec
.
Alternatively, insert_or_spawn_batch takes an iterator. You could pass the iterator directly into it instead of collecting into a Vec
here.
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.
This won't work. The iterator needs to be "owned" because the insertion is a deferred command. You can't "borrow" the Local vec.
bors r+ |
# Objective - Make insertion of uniform components faster ## Solution - Use batch insertion in the prepare_uniform_components system - Improves `many_cubes -- sphere` from ~42fps to ~43fps Co-authored-by: François <mockersf@gmail.com>
…ne#4179) # Objective - Make insertion of uniform components faster ## Solution - Use batch insertion in the prepare_uniform_components system - Improves `many_cubes -- sphere` from ~42fps to ~43fps Co-authored-by: François <mockersf@gmail.com>
…ne#4179) # Objective - Make insertion of uniform components faster ## Solution - Use batch insertion in the prepare_uniform_components system - Improves `many_cubes -- sphere` from ~42fps to ~43fps Co-authored-by: François <mockersf@gmail.com>
Objective
Solution
many_cubes -- sphere
from ~42fps to ~43fps