From bdb063497d802b9180d1b608bf54339e5a726b70 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Thu, 21 Sep 2023 10:53:20 -0700 Subject: [PATCH] Use radsort for Transparent2d PhaseItem sorting (#9882) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective Fix a performance regression in the "[bevy vs pixi](https://github.com/SUPERCILEX/bevy-vs-pixi)" benchmark. This benchmark seems to have a slightly pathological distribution of `z` values -- Sprites are spawned with a random `z` value with a child sprite at `f32::EPSILON` relative to the parent. See discussion here: https://github.com/bevyengine/bevy/issues/8100#issuecomment-1726978633 ## Solution Use `radsort` for sorting `Transparent2d` `PhaseItem`s. Use random `z` values in bevymark to stress the phase sort. Add an `--ordered-z` option to `bevymark` that uses the old behavior. ## Benchmarks mac m1 max | benchmark | fps before | fps after | diff | | - | - | - | - | | bevymark --waves 120 --per-wave 1000 --random-z | 42.16 | 47.06 | 🟩 +11.6% | | bevymark --waves 120 --per-wave 1000 | 52.50 | 52.29 | 🟥 -0.4% | | bevymark --waves 120 --per-wave 1000 --mode mesh2d --random-z | 9.64 | 10.24 | 🟩 +6.2% | | bevymark --waves 120 --per-wave 1000 --mode mesh2d | 15.83 | 15.59 | 🟥 -1.5% | | bevy-vs-pixi | 39.71 | 59.88 | 🟩 +50.1% | ## Discussion It's possible that `TransparentUi` should also change. We could probably use `slice::sort_unstable_by_key` with the current sort key though, as its items are always sorted and unique. I'd prefer to follow up later to look into that. Here's a survey of sorts used by other `PhaseItem`s #### slice::sort_by_key `Transparent2d`, `TransparentUi` #### radsort `Opaque3d`, `AlphaMask3d`, `Transparent3d`, `Opaque3dPrepass`, `AlphaMask3dPrepass`, `Shadow` I also tried `slice::sort_unstable_by_key` with a compound sort key including `Entity`, but it didn't seem as promising and I didn't test it as thoroughly. --------- Co-authored-by: Alice Cecile Co-authored-by: Robert Swain --- crates/bevy_core_pipeline/src/core_2d/mod.rs | 3 ++- crates/bevy_sprite/Cargo.toml | 1 + examples/stress_tests/bevymark.rs | 20 ++++++++++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/crates/bevy_core_pipeline/src/core_2d/mod.rs b/crates/bevy_core_pipeline/src/core_2d/mod.rs index 49f4260b20..084205ee24 100644 --- a/crates/bevy_core_pipeline/src/core_2d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_2d/mod.rs @@ -106,7 +106,8 @@ impl PhaseItem for Transparent2d { #[inline] fn sort(items: &mut [Self]) { - items.sort_by_key(|item| item.sort_key()); + // radsort is a stable radix sort that performed better than `slice::sort_by_key` or `slice::sort_unstable_by_key`. + radsort::sort_by_key(items, |item| item.sort_key().0); } #[inline] diff --git a/crates/bevy_sprite/Cargo.toml b/crates/bevy_sprite/Cargo.toml index 37db1b9eb2..649dc42345 100644 --- a/crates/bevy_sprite/Cargo.toml +++ b/crates/bevy_sprite/Cargo.toml @@ -31,3 +31,4 @@ guillotiere = "0.6.0" thiserror = "1.0" rectangle-pack = "0.4" bitflags = "2.3" +radsort = "0.1" diff --git a/examples/stress_tests/bevymark.rs b/examples/stress_tests/bevymark.rs index 4e2670ecab..cd8c1ad77f 100644 --- a/examples/stress_tests/bevymark.rs +++ b/examples/stress_tests/bevymark.rs @@ -60,6 +60,10 @@ struct Args { /// the number of different textures from which to randomly select the material color. 0 means no textures. #[argh(option, default = "1")] material_texture_count: usize, + + /// generate z values in increasing order rather than randomly + #[argh(switch)] + ordered_z: bool, } #[derive(Default, Clone)] @@ -163,6 +167,7 @@ struct BirdResources { color_rng: StdRng, material_rng: StdRng, velocity_rng: StdRng, + transform_rng: StdRng, } #[derive(Component)] @@ -204,6 +209,7 @@ fn setup( color_rng: StdRng::seed_from_u64(42), material_rng: StdRng::seed_from_u64(42), velocity_rng: StdRng::seed_from_u64(42), + transform_rng: StdRng::seed_from_u64(42), }; let text_section = move |color, value: &str| { @@ -360,7 +366,12 @@ fn spawn_birds( Mode::Sprite => { let batch = (0..spawn_count) .map(|count| { - let bird_z = (current_count + count) as f32 * 0.00001; + let bird_z = if args.ordered_z { + (current_count + count) as f32 * 0.00001 + } else { + bird_resources.transform_rng.gen::() + }; + let (transform, velocity) = bird_velocity_transform( half_extents, Vec3::new(bird_x, bird_y, bird_z), @@ -398,7 +409,12 @@ fn spawn_birds( Mode::Mesh2d => { let batch = (0..spawn_count) .map(|count| { - let bird_z = (current_count + count) as f32 * 0.00001; + let bird_z = if args.ordered_z { + (current_count + count) as f32 * 0.00001 + } else { + bird_resources.transform_rng.gen::() + }; + let (transform, velocity) = bird_velocity_transform( half_extents, Vec3::new(bird_x, bird_y, bird_z),