Skip to content

Commit

Permalink
Remove atomics in hot loop
Browse files Browse the repository at this point in the history
  • Loading branch information
aevyrie committed Aug 2, 2024
1 parent 8f80274 commit 7cd9386
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 24 deletions.
6 changes: 2 additions & 4 deletions examples/spatial_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,16 @@ fn move_player(
Neighboring Entities: {}
Spatial Hashing Update Cost:
Update Hashes: {: >8.2} us ({} Entities)
Update Maps: {: >10.2} us ({} Entities)
Update Hashes: {: >8.2} us
Update Maps: {: >10.2} us
Local Origin Propagation: {: >10.2?} us
Low Precision Propagation: {: >9.2?} us
High Precision Propagation: {: >8.2?} us",
avg * 1e6,
total,
hash_stats.hash_update_duration().as_secs_f32() * 1e6,
hash_stats.hash_update_entities(),
hash_stats.map_update_duration().as_secs_f32() * 1e6,
hash_stats.map_changed_entities(),
prop_stats.local_origin_propagation().as_secs_f32() * 1e6,
prop_stats.low_precision_propagation().as_secs_f32() * 1e6,
prop_stats.high_precision_propagation().as_secs_f32() * 1e6,
Expand Down
20 changes: 0 additions & 20 deletions src/spatial_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::{
hash::{Hash, Hasher},
marker::PhantomData,
sync::atomic::AtomicU32,
};

use bevy_app::prelude::*;
Expand Down Expand Up @@ -75,7 +74,6 @@ impl<P: GridPrecision, F: QueryFilter> SpatialHashPlugin<P, F> {
)>,
mut stats: ResMut<SpatialHashStats>,
) {
let n_entities = AtomicU32::new(0);
let start = Instant::now();

// Create new
Expand All @@ -95,21 +93,17 @@ impl<P: GridPrecision, F: QueryFilter> SpatialHashPlugin<P, F> {
// multiple plugins are updating the spatial hash, and it is already correct.
if old_hash.ne(&spatial_hash) {
*old_hash = spatial_hash;
n_entities.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
});

stats.hash_update_entities += n_entities.into_inner() as usize;
stats.hash_update_duration += start.elapsed();
}
}

/// Aggregate runtime statistics across all [`SpatialHashPlugin`]s.
#[derive(Resource, Debug, Clone, Default, Reflect)]
pub struct SpatialHashStats {
hash_update_entities: usize,
hash_update_duration: Duration,
map_changed_entities: usize,
map_update_duration: Duration,
}

Expand All @@ -118,22 +112,11 @@ impl SpatialHashStats {
*stats = Self::default();
}

/// Number of entities with a changed parent or grid cell, and needed their hash updated.
pub fn hash_update_entities(&self) -> usize {
self.hash_update_entities
}

/// Time to update all entity hashes.
pub fn hash_update_duration(&self) -> Duration {
self.hash_update_duration
}

/// Number of entities that were queried when updating spatial hash maps. This should match
/// [`Self::hash_update_entities`].
pub fn map_changed_entities(&self) -> usize {
self.map_changed_entities
}

/// Time to update all spatial hash maps.
pub fn map_update_duration(&self) -> Duration {
self.map_update_duration
Expand Down Expand Up @@ -282,7 +265,6 @@ where
mut stats: ResMut<SpatialHashStats>,
mut destroy_list: Local<Vec<SpatialHash<P>>>,
) {
let mut n_entities = 0;
let start = Instant::now();

for entity in removed.read() {
Expand All @@ -291,12 +273,10 @@ where

for (entity, spatial_hash, parent, cell) in &changed_entities {
spatial_map.insert_or_update(entity, *spatial_hash, parent, cell);
n_entities += 1;
}

spatial_map.clean_up_empty_sets(&mut destroy_list);

stats.map_changed_entities += n_entities;
stats.map_update_duration += start.elapsed();
}

Expand Down

0 comments on commit 7cd9386

Please sign in to comment.