Skip to content

Commit

Permalink
Use radsort for Transparent2d PhaseItem sorting (#9882)
Browse files Browse the repository at this point in the history
# 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:
#8100 (comment)

## 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 <alice.i.cecile@gmail.com>
Co-authored-by: Robert Swain <robert.swain@gmail.com>
  • Loading branch information
3 people committed Sep 21, 2023
1 parent 1116207 commit bdb0634
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/core_2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_sprite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ guillotiere = "0.6.0"
thiserror = "1.0"
rectangle-pack = "0.4"
bitflags = "2.3"
radsort = "0.1"
20 changes: 18 additions & 2 deletions examples/stress_tests/bevymark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -163,6 +167,7 @@ struct BirdResources {
color_rng: StdRng,
material_rng: StdRng,
velocity_rng: StdRng,
transform_rng: StdRng,
}

#[derive(Component)]
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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::<f32>()
};

let (transform, velocity) = bird_velocity_transform(
half_extents,
Vec3::new(bird_x, bird_y, bird_z),
Expand Down Expand Up @@ -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::<f32>()
};

let (transform, velocity) = bird_velocity_transform(
half_extents,
Vec3::new(bird_x, bird_y, bird_z),
Expand Down

0 comments on commit bdb0634

Please sign in to comment.