-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add benchmarks for spawning and inserting bundles #19762
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use benches::bench; | ||
use bevy_ecs::{component::Component, world::World}; | ||
use criterion::Criterion; | ||
|
||
const ENTITY_COUNT: usize = 2_000; | ||
|
||
#[derive(Component)] | ||
struct C<const N: usize>(usize); | ||
|
||
pub fn insert_many(criterion: &mut Criterion) { | ||
let mut group = criterion.benchmark_group(bench!("insert_many")); | ||
|
||
group.bench_function("all", |bencher| { | ||
bencher.iter(|| { | ||
let mut world = World::new(); | ||
for _ in 0..ENTITY_COUNT { | ||
world | ||
.spawn_empty() | ||
.insert(C::<0>(1)) | ||
.insert(C::<1>(1)) | ||
.insert(C::<2>(1)) | ||
.insert(C::<3>(1)) | ||
.insert(C::<4>(1)) | ||
.insert(C::<5>(1)) | ||
.insert(C::<6>(1)) | ||
.insert(C::<7>(1)) | ||
.insert(C::<8>(1)) | ||
.insert(C::<9>(1)) | ||
.insert(C::<10>(1)) | ||
.insert(C::<11>(1)) | ||
.insert(C::<12>(1)) | ||
.insert(C::<13>(1)) | ||
.insert(C::<14>(1)); | ||
} | ||
}); | ||
}); | ||
|
||
group.bench_function("only_last", |bencher| { | ||
bencher.iter(|| { | ||
let mut world = World::new(); | ||
for _ in 0..ENTITY_COUNT { | ||
world | ||
.spawn(( | ||
C::<0>(1), | ||
C::<1>(1), | ||
C::<2>(1), | ||
C::<3>(1), | ||
C::<4>(1), | ||
C::<5>(1), | ||
C::<6>(1), | ||
C::<7>(1), | ||
C::<8>(1), | ||
C::<9>(1), | ||
C::<10>(1), | ||
C::<11>(1), | ||
C::<12>(1), | ||
C::<13>(1), | ||
)) | ||
.insert(C::<14>(1)); | ||
} | ||
}); | ||
}); | ||
|
||
group.finish(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use criterion::criterion_group; | ||
|
||
mod insert_many; | ||
mod spawn_many; | ||
mod spawn_many_zst; | ||
mod spawn_one_zst; | ||
|
||
criterion_group!( | ||
benches, | ||
spawn_one_zst::spawn_one_zst, | ||
spawn_many_zst::spawn_many_zst, | ||
spawn_many::spawn_many, | ||
insert_many::insert_many, | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use core::hint::black_box; | ||
|
||
use benches::bench; | ||
use bevy_ecs::{component::Component, world::World}; | ||
use criterion::Criterion; | ||
|
||
const ENTITY_COUNT: usize = 2_000; | ||
|
||
#[derive(Component)] | ||
struct C<const N: usize>(usize); | ||
|
||
pub fn spawn_many(criterion: &mut Criterion) { | ||
let mut group = criterion.benchmark_group(bench!("spawn_many")); | ||
|
||
group.bench_function("static", |bencher| { | ||
bencher.iter(|| { | ||
let mut world = World::new(); | ||
for _ in 0..ENTITY_COUNT { | ||
world.spawn(black_box(( | ||
C::<0>(1), | ||
C::<1>(1), | ||
C::<2>(1), | ||
C::<3>(1), | ||
C::<4>(1), | ||
C::<5>(1), | ||
C::<6>(1), | ||
C::<7>(1), | ||
C::<8>(1), | ||
C::<9>(1), | ||
C::<10>(1), | ||
C::<11>(1), | ||
C::<12>(1), | ||
C::<13>(1), | ||
C::<14>(1), | ||
))); | ||
} | ||
}); | ||
}); | ||
|
||
group.finish(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use core::hint::black_box; | ||
|
||
use benches::bench; | ||
use bevy_ecs::{component::Component, world::World}; | ||
use criterion::Criterion; | ||
|
||
const ENTITY_COUNT: usize = 2_000; | ||
|
||
#[derive(Component)] | ||
struct C<const N: usize>; | ||
|
||
pub fn spawn_many_zst(criterion: &mut Criterion) { | ||
let mut group = criterion.benchmark_group(bench!("spawn_many_zst")); | ||
|
||
group.bench_function("static", |bencher| { | ||
bencher.iter(|| { | ||
let mut world = World::new(); | ||
for _ in 0..ENTITY_COUNT { | ||
world.spawn(black_box(( | ||
C::<0>, C::<1>, C::<2>, C::<3>, C::<4>, C::<5>, C::<6>, C::<7>, C::<8>, C::<9>, | ||
C::<10>, C::<11>, C::<12>, C::<13>, C::<14>, | ||
))); | ||
} | ||
}); | ||
}); | ||
|
||
group.finish(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use core::hint::black_box; | ||
|
||
use benches::bench; | ||
use bevy_ecs::{component::Component, world::World}; | ||
use criterion::Criterion; | ||
|
||
const ENTITY_COUNT: usize = 10_000; | ||
|
||
#[derive(Component)] | ||
struct A; | ||
|
||
pub fn spawn_one_zst(criterion: &mut Criterion) { | ||
let mut group = criterion.benchmark_group(bench!("spawn_one_zst")); | ||
|
||
group.bench_function("static", |bencher| { | ||
bencher.iter(|| { | ||
let mut world = World::new(); | ||
for _ in 0..ENTITY_COUNT { | ||
world.spawn(black_box(A)); | ||
} | ||
}); | ||
}); | ||
|
||
group.finish(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the
black_box
being used here on the bundle before spawning it. You'll definitely need the blackbox somewhere, but I'm not sure here is where it should be...I'm not sure if it would work exactly, or what precisely you're trying to bench, but putting the black box over the bundle makes rustc unsure where the bundle comes from. That makes sense for dynamic bundles, but for static bundles, I don't mind letting the compiler see that information since that's going to be more realistic. Have you tried passing a
&mut World
andEntity
to the blackbox instead? That might be more realistic for static bundles. That said, I'm not expert, and it really does depend on what you're testing. As it is, this is kinda more benching spawning anfn() -> impl StaticBundle
. So up to you.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially I put them for consistency with the dynamic ones, because for them I would expect the compiler to not know whether there is a
Some
or aNone
. In this case it doesn't really matter since all the components are ZST, but in any case I removed it.Since I moved the
World
outside theiter
I believe this is not automatically done by criterion when calling the inner closure (since itblack_box
es it, which has the effect ofblack_box
ing all the captured variables including theWorld
).