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

Use batch spawn in benchmarks #11611

Merged
merged 1 commit into from
Feb 1, 2024
Merged

Conversation

BD103
Copy link
Member

@BD103 BD103 commented Jan 29, 2024

Objective

  • The benchmarks for bevy_ecs' iter_simple group use for loops instead of World::spawn_batch.
  • There's a TODO comment that says to batch spawn them.

Solution

  • Replace the for loops with World::spawn_batch.

@BD103 BD103 changed the title chore(bench): batch spawn iter_simple entities Use batch spawn in benchmarks Jan 29, 2024
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Benchmarks Stress tests and benchmarks used to measure how fast things are labels Jan 29, 2024
@hymm
Copy link
Contributor

hymm commented Feb 1, 2024

What is the improvement in the benches from this?

@mockersf
Copy link
Member

mockersf commented Feb 1, 2024

it shouldn't change the benches has it's in the setup, but would be nice to confirm

@BD103
Copy link
Member Author

BD103 commented Feb 1, 2024

I tried benchmarking spawn_batch. Here what I found.

Before:

iter_simple/bd103       time:   [526.69 µs 533.42 µs 540.87 µs]                              
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

After:

iter_simple/bd103       time:   [346.32 µs 347.52 µs 348.79 µs]                              
                        change: [-36.600% -36.049% -35.473%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild
Code used to test this
// Before
group.bench_function("bd103", |b| {
    use bevy_ecs::prelude::*;
    use glam::*;

    #[derive(Component, Copy, Clone)]
    struct Transform(Mat4);
    #[derive(Component, Copy, Clone)]
    struct Position(Vec3);
    #[derive(Component, Copy, Clone)]
    struct Rotation(Vec3);
    #[derive(Component, Copy, Clone)]
    struct Velocity(Vec3);

    b.iter(|| {
        let mut world = World::new();

        for _ in 0..10_000 {
            world.spawn((
                Transform(Mat4::from_scale(Vec3::ONE)),
                Position(Vec3::X),
                Rotation(Vec3::X),
                Velocity(Vec3::X),
            ));
        }
    })
});

// After
group.bench_function("bd103", |b| {
    use bevy_ecs::prelude::*;
    use glam::*;

    #[derive(Component, Copy, Clone)]
    struct Transform(Mat4);
    #[derive(Component, Copy, Clone)]
    struct Position(Vec3);
    #[derive(Component, Copy, Clone)]
    struct Rotation(Vec3);
    #[derive(Component, Copy, Clone)]
    struct Velocity(Vec3);

    b.iter(|| {
        let mut world = World::new();

        world.spawn_batch(
            std::iter::repeat((
                Transform(Mat4::from_scale(Vec3::ONE)),
                Position(Vec3::X),
                Rotation(Vec3::X),
                Velocity(Vec3::X),
            ))
            .take(10_000)
        );
    })
});

@hymm
Copy link
Contributor

hymm commented Feb 1, 2024

Nice faster and less noisy

@hymm hymm added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Feb 1, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 1, 2024
github-merge-queue bot pushed a commit that referenced this pull request Feb 1, 2024
# Objective

- The benchmarks for `bevy_ecs`' `iter_simple` group use `for` loops
instead of `World::spawn_batch`.
- There's a TODO comment that says to batch spawn them.

## Solution

- Replace the `for` loops with `World::spawn_batch`.
Merged via the queue into bevyengine:main with commit 3d2d61d Feb 1, 2024
23 checks passed
@BD103 BD103 deleted the bench/batch branch February 1, 2024 20:32
tjamaan pushed a commit to tjamaan/bevy that referenced this pull request Feb 6, 2024
# Objective

- The benchmarks for `bevy_ecs`' `iter_simple` group use `for` loops
instead of `World::spawn_batch`.
- There's a TODO comment that says to batch spawn them.

## Solution

- Replace the `for` loops with `World::spawn_batch`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Benchmarks Stress tests and benchmarks used to measure how fast things are S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants