Skip to content
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

Closed

Conversation

superdump
Copy link
Contributor

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

@superdump superdump added A-Rendering Drawing game state to the screen C-Performance A change motivated by improving speed, memory usage or compile times labels Mar 11, 2022
@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Mar 11, 2022
@superdump superdump removed the S-Needs-Triage This issue needs to be labelled label Mar 11, 2022
@mockersf
Copy link
Member

--- 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
components is an exact sized iterator, I think std collect does smarter things with those than pushing individual values inside a vec

@superdump
Copy link
Contributor Author

I didn't see any practical difference but I think it makes sense anyway.

},),
)
})
.collect::<Vec<_>>();
Copy link
Member

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.

Copy link
Member

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.

@cart
Copy link
Member

cart commented Mar 11, 2022

bors r+

bors bot pushed a commit that referenced this pull request Mar 11, 2022
# 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>
@bors bors bot changed the title bevy_render: Batch insertion for prepare_uniform_components [Merged by Bors] - bevy_render: Batch insertion for prepare_uniform_components Mar 11, 2022
@bors bors bot closed this Mar 11, 2022
aevyrie pushed a commit to aevyrie/bevy that referenced this pull request Jun 7, 2022
…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>
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Performance A change motivated by improving speed, memory usage or compile times
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants