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

very low performance when spawning UI nodes one inside the other #677

Closed
mockersf opened this issue Oct 13, 2020 · 7 comments
Closed

very low performance when spawning UI nodes one inside the other #677

mockersf opened this issue Oct 13, 2020 · 7 comments
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Dependencies A change to the crates that Bevy depends on C-Performance A change motivated by improving speed, memory usage or compile times

Comments

@mockersf
Copy link
Member

mockersf commented Oct 13, 2020

When spawning every second a new node as a children of the last one that had been spawned, the application starts to slow down after 10 iterations, and becomes unresponsive after 15~20 iterations

use bevy::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    App::build()
        .add_default_plugins()
        .add_startup_system(setup.system())
        .add_system(deep.system())
        .run();

    Ok(())
}

fn setup(mut commands: Commands) {
    commands.spawn(UiCameraComponents::default());

    commands
        .spawn(NodeComponents::default())
        .with(Timer::from_seconds(1., false));
}

fn deep(mut commands: Commands, entity: Entity, timer: &Timer) {
    if timer.just_finished {
        println!("spawning new child");
        commands
            .spawn(NodeComponents::default())
            .with(Timer::from_seconds(1., false));
        let child = commands.current_entity().unwrap();

        commands.push_children(entity, &[child]);
    }
}

with some logging and diagnostics:

Oct 14 00:19:36.576  INFO tank: spawning new child
Oct 14 00:19:36.577 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0145      (avg 0.0166)
Oct 14 00:19:36.578 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 60.3177     (avg 60.0176)
Oct 14 00:19:37.576 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0146      (avg 0.0165)
Oct 14 00:19:37.576 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 60.2317     (avg 60.1338)
Oct 14 00:19:37.592  INFO tank: spawning new child
Oct 14 00:19:38.580 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0178      (avg 0.0167)
Oct 14 00:19:38.580 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 60.0023     (avg 60.0193)
Oct 14 00:19:38.592  INFO tank: spawning new child
Oct 14 00:19:39.587 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0180      (avg 0.0163)
Oct 14 00:19:39.587 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 59.7169     (avg 60.2253)
Oct 14 00:19:39.602  INFO tank: spawning new child
Oct 14 00:19:40.589 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0174      (avg 0.0168)
Oct 14 00:19:40.590 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 59.9331     (avg 59.9481)
Oct 14 00:19:40.602  INFO tank: spawning new child
Oct 14 00:19:41.589 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0152      (avg 0.0166)
Oct 14 00:19:41.589 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 59.9631     (avg 59.9649)
Oct 14 00:19:41.621  INFO tank: spawning new child
Oct 14 00:19:42.573 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0159      (avg 0.0167)
Oct 14 00:19:42.573 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 60.3003     (avg 59.9682)
Oct 14 00:19:42.627  INFO tank: spawning new child
Oct 14 00:19:43.590 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0157      (avg 0.0166)
Oct 14 00:19:43.590 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 60.0817     (avg 60.0140)
Oct 14 00:19:43.644  INFO tank: spawning new child
Oct 14 00:19:44.584 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0172      (avg 0.0168)
Oct 14 00:19:44.585 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 59.9687     (avg 59.6242)
Oct 14 00:19:44.650  INFO tank: spawning new child
Oct 14 00:19:45.596 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0266      (avg 0.0263)
Oct 14 00:19:45.596 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 39.7056     (avg 38.1866)
Oct 14 00:19:45.664  INFO tank: spawning new child
Oct 14 00:19:46.628 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0447      (avg 0.0438)
Oct 14 00:19:46.628 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 34.7194     (avg 27.4654)
Oct 14 00:19:46.688  INFO tank: spawning new child
Oct 14 00:19:47.713 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0438      (avg 0.0656)
Oct 14 00:19:47.713 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 24.3330     (avg 20.1163)
Oct 14 00:19:47.722  INFO tank: spawning new child
Oct 14 00:19:48.738 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0511      (avg 0.0981)
Oct 14 00:19:48.738 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 22.6240     (avg 16.2551)
Oct 14 00:19:48.748  INFO tank: spawning new child
Oct 14 00:19:49.883 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0783      (avg 0.1336)
Oct 14 00:19:49.883 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 19.3643     (avg 13.7073)
Oct 14 00:19:49.893  INFO tank: spawning new child
Oct 14 00:19:51.104  INFO tank: spawning new child
Oct 14 00:19:52.276 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0769      (avg 0.1966)
Oct 14 00:19:52.276 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 17.5957     (avg 11.7911)
Oct 14 00:19:52.290  INFO tank: spawning new child
Oct 14 00:19:54.523 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0798      (avg 0.2521)
Oct 14 00:19:54.523 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 17.0662     (avg 11.1097)
Oct 14 00:19:54.540  INFO tank: spawning new child
Oct 14 00:19:58.888 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0784      (avg 0.3606)
Oct 14 00:19:58.888 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 16.5496     (avg 10.3950)
Oct 14 00:19:58.911  INFO tank: spawning new child
Oct 14 00:20:07.772 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0784      (avg 0.5752)
Oct 14 00:20:07.772 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 16.0953     (avg 9.6545)
Oct 14 00:20:07.807  INFO tank: spawning new child
Oct 14 00:20:26.534 DEBUG bevy_log_diagnostic: diagnostic: frame_time               : 0.0793      (avg 1.0161)
Oct 14 00:20:26.534 DEBUG bevy_log_diagnostic: diagnostic: fps                      : 15.6552     (avg 8.8989)
Oct 14 00:20:26.615  INFO tank: spawning new child

the FPS starts to slow down after 10 seconds, and is false after 15 seconds as some seconds are skipped.

@cart
Copy link
Member

cart commented Oct 13, 2020

Looks like stretch really explodes here for some reason:

image

@mockersf
Copy link
Member Author

mockersf commented Oct 13, 2020

it seems this marks the root node as dirty at every step, which means stretch has everything to compute again.
I'm trying to fix it

@zicklag
Copy link
Member

zicklag commented Oct 13, 2020

@cart, what did you use to create that flamegraph? Could be useful for the benchmarking stuff I'm trying to do.

@cart
Copy link
Member

cart commented Oct 14, 2020

Check out our profiling doc here:

https://github.com/bevyengine/bevy/blob/master/docs/profiling.md

@cart
Copy link
Member

cart commented Oct 14, 2020

https://github.com/flamegraph-rs/flamegraph

@karroffel karroffel added C-Performance A change motivated by improving speed, memory usage or compile times A-UI Graphical user interfaces, styles, layouts, and widgets labels Oct 14, 2020
@mockersf
Copy link
Member Author

actually this seems related to vislyhq/stretch#71

I could reproduce by running the same calls to stretch made by bevy, but without bevy being involved

@mockersf
Copy link
Member Author

updated example:

use bevy::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup)
        .add_system(deep)
        .run();

    Ok(())
}

#[derive(Component)]
struct TickTimer(Timer);

fn setup(mut commands: Commands) {
    commands.spawn_bundle(UiCameraBundle::default());

    commands
        .spawn_bundle(NodeBundle::default())
        .insert(TickTimer(Timer::from_seconds(1., false)));
}

fn deep(mut commands: Commands, time: Res<Time>, mut query: Query<(Entity, &mut TickTimer)>) {
    for (entity, mut timer) in query.iter_mut() {
        if timer.0.tick(time.delta()).just_finished() {
            info!("spawning new child");
            let child = commands
                .spawn_bundle(NodeBundle::default())
                .insert(TickTimer(Timer::from_seconds(1., false)))
                .id();

            commands.entity(entity).push_children(&[child]);
        }
    }
}

@bors bors bot closed this as completed in 86dd6f0 Jun 21, 2022
james7132 pushed a commit to james7132/bevy that referenced this issue Jun 22, 2022
…gine#4716)

# Objective

DioxusLabs and Bevy have taken over maintaining what was our abandoned ui layout dependency [stretch](https://github.com/vislyhq/stretch). Dioxus' fork has had a lot of work done on it by @alice-i-cecile, @Weibye , @jkelleyrtp, @mockersf, @HackerFoo, @TimJentzsch and a dozen other contributors and now is in much better shape than stretch was. The updated crate is called taffy and is available on github [here](https://github.com/DioxusLabs/taffy) ([taffy](https://crates.io/crates/taffy) on crates.io). The goal of this PR is to replace stretch v0.3.2 with taffy v0.1.0.

## Solution

I changed the bevy_ui Cargo.toml to depend on taffy instead of stretch and fixed all the errors rustc complained about.

---

## Changelog

Changed bevy_ui layout dependency from stretch to taffy (the maintained fork of stretch).

fixes bevyengine#677

## Migration Guide

The public api of taffy is different from that of stretch so please advise me on what to do here @alice-i-cecile.
james7132 pushed a commit to james7132/bevy that referenced this issue Jun 22, 2022
…gine#4716)

# Objective

DioxusLabs and Bevy have taken over maintaining what was our abandoned ui layout dependency [stretch](https://github.com/vislyhq/stretch). Dioxus' fork has had a lot of work done on it by @alice-i-cecile, @Weibye , @jkelleyrtp, @mockersf, @HackerFoo, @TimJentzsch and a dozen other contributors and now is in much better shape than stretch was. The updated crate is called taffy and is available on github [here](https://github.com/DioxusLabs/taffy) ([taffy](https://crates.io/crates/taffy) on crates.io). The goal of this PR is to replace stretch v0.3.2 with taffy v0.1.0.

## Solution

I changed the bevy_ui Cargo.toml to depend on taffy instead of stretch and fixed all the errors rustc complained about.

---

## Changelog

Changed bevy_ui layout dependency from stretch to taffy (the maintained fork of stretch).

fixes bevyengine#677

## Migration Guide

The public api of taffy is different from that of stretch so please advise me on what to do here @alice-i-cecile.
inodentry pushed a commit to IyesGames/bevy that referenced this issue Aug 8, 2022
…gine#4716)

# Objective

DioxusLabs and Bevy have taken over maintaining what was our abandoned ui layout dependency [stretch](https://github.com/vislyhq/stretch). Dioxus' fork has had a lot of work done on it by @alice-i-cecile, @Weibye , @jkelleyrtp, @mockersf, @HackerFoo, @TimJentzsch and a dozen other contributors and now is in much better shape than stretch was. The updated crate is called taffy and is available on github [here](https://github.com/DioxusLabs/taffy) ([taffy](https://crates.io/crates/taffy) on crates.io). The goal of this PR is to replace stretch v0.3.2 with taffy v0.1.0.

## Solution

I changed the bevy_ui Cargo.toml to depend on taffy instead of stretch and fixed all the errors rustc complained about.

---

## Changelog

Changed bevy_ui layout dependency from stretch to taffy (the maintained fork of stretch).

fixes bevyengine#677

## Migration Guide

The public api of taffy is different from that of stretch so please advise me on what to do here @alice-i-cecile.
james7132 pushed a commit to james7132/bevy that referenced this issue Oct 28, 2022
…gine#4716)

# Objective

DioxusLabs and Bevy have taken over maintaining what was our abandoned ui layout dependency [stretch](https://github.com/vislyhq/stretch). Dioxus' fork has had a lot of work done on it by @alice-i-cecile, @Weibye , @jkelleyrtp, @mockersf, @HackerFoo, @TimJentzsch and a dozen other contributors and now is in much better shape than stretch was. The updated crate is called taffy and is available on github [here](https://github.com/DioxusLabs/taffy) ([taffy](https://crates.io/crates/taffy) on crates.io). The goal of this PR is to replace stretch v0.3.2 with taffy v0.1.0.

## Solution

I changed the bevy_ui Cargo.toml to depend on taffy instead of stretch and fixed all the errors rustc complained about.

---

## Changelog

Changed bevy_ui layout dependency from stretch to taffy (the maintained fork of stretch).

fixes bevyengine#677

## Migration Guide

The public api of taffy is different from that of stretch so please advise me on what to do here @alice-i-cecile.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this issue Feb 1, 2023
…gine#4716)

# Objective

DioxusLabs and Bevy have taken over maintaining what was our abandoned ui layout dependency [stretch](https://github.com/vislyhq/stretch). Dioxus' fork has had a lot of work done on it by @alice-i-cecile, @Weibye , @jkelleyrtp, @mockersf, @HackerFoo, @TimJentzsch and a dozen other contributors and now is in much better shape than stretch was. The updated crate is called taffy and is available on github [here](https://github.com/DioxusLabs/taffy) ([taffy](https://crates.io/crates/taffy) on crates.io). The goal of this PR is to replace stretch v0.3.2 with taffy v0.1.0.

## Solution

I changed the bevy_ui Cargo.toml to depend on taffy instead of stretch and fixed all the errors rustc complained about.

---

## Changelog

Changed bevy_ui layout dependency from stretch to taffy (the maintained fork of stretch).

fixes bevyengine#677

## Migration Guide

The public api of taffy is different from that of stretch so please advise me on what to do here @alice-i-cecile.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Dependencies A change to the crates that Bevy depends on C-Performance A change motivated by improving speed, memory usage or compile times
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants