Skip to content

Commit

Permalink
bevy_transform: Use Changed in the query for much faster transform_pr…
Browse files Browse the repository at this point in the history
…opagate_system (bevyengine#4180)

# Objective

- Improve transform propagation performance

## Solution

- Use `Changed<Transform>` as part of the `root_query` and `transform_query` to avoid the indirection of having to look up the `Entity` in the `changed_transform_query`
- Get rid of the `changed_transform_query` entirely
- `transform_propagate_system` execution time for `many_cubes -- sphere` dropped from 1.07ms to 0.159ms, an 85% reduction for this system. Frame rate increased from ~42fps to ~44fps
  • Loading branch information
superdump authored and ItsDoot committed Feb 1, 2023
1 parent ff71daa commit 05b0892
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions crates/bevy_transform/src/systems.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::components::{GlobalTransform, Transform};
use bevy_ecs::{
entity::Entity,
query::{Changed, With, Without},
prelude::Changed,
query::{With, Without},
system::Query,
};
use bevy_hierarchy::{Children, Parent};
Expand All @@ -10,16 +11,23 @@ use bevy_hierarchy::{Children, Parent};
/// [`Transform`] component.
pub fn transform_propagate_system(
mut root_query: Query<
(Entity, Option<&Children>, &Transform, &mut GlobalTransform),
(
Option<&Children>,
&Transform,
Changed<Transform>,
&mut GlobalTransform,
),
Without<Parent>,
>,
mut transform_query: Query<(&Transform, &mut GlobalTransform), With<Parent>>,
changed_transform_query: Query<Entity, Changed<Transform>>,
mut transform_query: Query<
(&Transform, Changed<Transform>, &mut GlobalTransform),
With<Parent>,
>,
children_query: Query<Option<&Children>, (With<Parent>, With<GlobalTransform>)>,
) {
for (entity, children, transform, mut global_transform) in root_query.iter_mut() {
for (children, transform, transform_changed, mut global_transform) in root_query.iter_mut() {
let mut changed = false;
if changed_transform_query.get(entity).is_ok() {
if transform_changed {
*global_transform = GlobalTransform::from(*transform);
changed = true;
}
Expand All @@ -28,7 +36,6 @@ pub fn transform_propagate_system(
for child in children.iter() {
propagate_recursive(
&global_transform,
&changed_transform_query,
&mut transform_query,
&children_query,
*child,
Expand All @@ -41,16 +48,19 @@ pub fn transform_propagate_system(

fn propagate_recursive(
parent: &GlobalTransform,
changed_transform_query: &Query<Entity, Changed<Transform>>,
transform_query: &mut Query<(&Transform, &mut GlobalTransform), With<Parent>>,
transform_query: &mut Query<
(&Transform, Changed<Transform>, &mut GlobalTransform),
With<Parent>,
>,
children_query: &Query<Option<&Children>, (With<Parent>, With<GlobalTransform>)>,
entity: Entity,
mut changed: bool,
) {
changed |= changed_transform_query.get(entity).is_ok();

let global_matrix = {
if let Ok((transform, mut global_transform)) = transform_query.get_mut(entity) {
if let Ok((transform, transform_changed, mut global_transform)) =
transform_query.get_mut(entity)
{
changed |= transform_changed;
if changed {
*global_transform = parent.mul_transform(*transform);
}
Expand All @@ -64,7 +74,6 @@ fn propagate_recursive(
for child in children.iter() {
propagate_recursive(
&global_matrix,
changed_transform_query,
transform_query,
children_query,
*child,
Expand Down

0 comments on commit 05b0892

Please sign in to comment.