-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Opportunistically use dense iter for archetypal iteration in Par_iter (…
…#14673) # Objective - follow of #14049 ,we could use it on our Parallel Iterator,this pr also unified the used function in both regular iter and parallel iterations. ## Performance ![image](https://github.com/user-attachments/assets/cba700bc-169c-4b58-b504-823bdca8ec05) no performance regression for regular itertaion 3.5X faster in hybrid parallel iteraion,this number is far greater than the benefits obtained in regular iteration(~1.81) because mutable iterations on continuous memory can effectively reduce the cost of mataining core cache coherence
- Loading branch information
Showing
5 changed files
with
121 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
45 changes: 45 additions & 0 deletions
45
benches/benches/bevy_ecs/iteration/par_iter_simple_foreach_hybrid.rs
This file contains 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,45 @@ | ||
use bevy_ecs::prelude::*; | ||
use bevy_tasks::{ComputeTaskPool, TaskPool}; | ||
use rand::{prelude::SliceRandom, SeedableRng}; | ||
use rand_chacha::ChaCha8Rng; | ||
|
||
#[derive(Component, Copy, Clone)] | ||
struct TableData(f32); | ||
|
||
#[derive(Component, Copy, Clone)] | ||
#[component(storage = "SparseSet")] | ||
struct SparseData(f32); | ||
|
||
fn deterministic_rand() -> ChaCha8Rng { | ||
ChaCha8Rng::seed_from_u64(42) | ||
} | ||
pub struct Benchmark<'w>(World, QueryState<(&'w mut TableData, &'w SparseData)>); | ||
|
||
impl<'w> Benchmark<'w> { | ||
pub fn new() -> Self { | ||
let mut world = World::new(); | ||
ComputeTaskPool::get_or_init(TaskPool::default); | ||
|
||
let mut v = vec![]; | ||
for _ in 0..100000 { | ||
world.spawn((TableData(0.0), SparseData(0.0))); | ||
v.push(world.spawn(TableData(0.)).id()); | ||
} | ||
|
||
// by shuffling ,randomize the archetype iteration order to significantly deviate from the table order. This maximizes the loss of cache locality during archetype-based iteration. | ||
v.shuffle(&mut deterministic_rand()); | ||
for e in v.into_iter() { | ||
world.entity_mut(e).despawn(); | ||
} | ||
|
||
let query = world.query::<(&mut TableData, &SparseData)>(); | ||
Self(world, query) | ||
} | ||
|
||
#[inline(never)] | ||
pub fn run(&mut self) { | ||
self.1 | ||
.par_iter_mut(&mut self.0) | ||
.for_each(|(mut v1, v2)| v1.0 += v2.0) | ||
} | ||
} |
This file contains 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
This file contains 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