Repository: bevyengine/bevy
File: crates/bevy_ui/src/layout/mod.rs (Rust)

Code before:
            ui_children: &UiChildren,
            added_node_query: &Query<(), Added<Node>>,
            entity: Entity,
        ) {
            if ui_surface.entity_to_taffy.contains_key(&entity)
                && (added_node_query.contains(entity)
                    || ui_children.is_changed(entity)
                    || ui_children
                        .iter_ui_children(entity)
                        .any(|child| added_node_query.contains(child)))
            {
                ui_surface.update_children(entity, ui_children.iter_ui_children(entity));
            }

            for child in ui_children.iter_ui_children(entity) {
                update_children_recursively(ui_surface, ui_children, added_node_query, child);
            }
        }

        for ui_root_entity in ui_root_node_query.iter() {
            update_children_recursively(
                &mut ui_surface,
                &ui_children,
                &added_node_query,
                ui_root_entity,
            );
        }

        let (_, _, _, computed_target) = node_query.get(ui_root_entity).unwrap();

        ui_surface.compute_layout(
            ui_root_entity,
            computed_target.physical_size,
            &mut buffer_query,
            &mut font_system,
        );

        update_uinode_geometry_recursive(
            ui_root_entity,
            &mut ui_surface,
            true,
            computed_target.physical_size().as_vec2(),
            Affine2::IDENTITY,
            &mut node_update_query,
            &ui_children,
            computed_target.scale_factor.recip(),
            Vec2::ZERO,
            Vec2::ZERO,
        );
    }


Review comment (suggestion):
This loop seems unnecessary. We don't need to traverse all the UI trees for every root right?

```suggestion
        update_children_recursively(
            &mut ui_surface,
            &ui_children,
            &added_node_query,
            ui_root_entity,
        );
```

Code after:
            ui_children: &UiChildren,
            added_node_query: &Query<(), Added<Node>>,
            entity: Entity,
        ) {
            if ui_surface.entity_to_taffy.contains_key(&entity)
                && (added_node_query.contains(entity)
                    || ui_children.is_changed(entity)
                    || ui_children
                        .iter_ui_children(entity)
                        .any(|child| added_node_query.contains(child)))
            {
                ui_surface.update_children(entity, ui_children.iter_ui_children(entity));
            }

            for child in ui_children.iter_ui_children(entity) {
                update_children_recursively(ui_surface, ui_children, added_node_query, child);
            }
        }

        update_children_recursively(
            &mut ui_surface,
            &ui_children,
            &added_node_query,
            ui_root_entity,
        );

        let (_, _, _, computed_target) = node_query.get(ui_root_entity).unwrap();

        ui_surface.compute_layout(
            ui_root_entity,
            computed_target.physical_size,
            &mut buffer_query,
            &mut font_system,
        );

        update_uinode_geometry_recursive(
            ui_root_entity,
            &mut ui_surface,
            true,
            computed_target.physical_size().as_vec2(),
            Affine2::IDENTITY,
            &mut node_update_query,
            &ui_children,
            computed_target.scale_factor.recip(),
            Vec2::ZERO,
            Vec2::ZERO,
        );
    }

    // Returns the combined bounding box of the node and any of its overflowing children.
    fn update_uinode_geometry_recursive(