Skip to content

Commit

Permalink
Put asset_events behind a run condition (#11800)
Browse files Browse the repository at this point in the history
# Objective
Scheduling low cost systems has significant overhead due to task pool
contention and the extra machinery to schedule and run them. Following
the example of #7728, `asset_events` is good example of this kind of
system, where there is no work to be done when there are no queued asset
events.

## Solution
Put a run condition on it that checks if there are any queued events.

## Performance
Tested against `many_foxes`, we can see a slight improvement in the
total time spent in `UpdateAssets`. Also noted much less volatility due
to not being at the whim of the OS thread scheduler.

![image](https://github.com/bevyengine/bevy/assets/3137680/e0b282bf-27d0-4fe4-81b9-ecd72ab258e5)

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
  • Loading branch information
james7132 and alice-i-cecile authored Feb 12, 2024
1 parent 9e30aa7 commit eee71bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 8 additions & 0 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,14 @@ impl<A: Asset> Assets<A> {
pub fn asset_events(mut assets: ResMut<Self>, mut events: EventWriter<AssetEvent<A>>) {
events.send_batch(assets.queued_events.drain(..));
}

/// A run condition for [`asset_events`]. The system will not run if there are no events to
/// flush.
///
/// [`asset_events`]: Self::asset_events
pub(crate) fn asset_events_condition(assets: Res<Self>) -> bool {
!assets.queued_events.is_empty()
}
}

/// A mutable iterator over [`Assets`].
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,10 @@ impl AssetApp for App {
.add_event::<AssetLoadFailedEvent<A>>()
.register_type::<Handle<A>>()
.register_type::<AssetId<A>>()
.add_systems(AssetEvents, Assets::<A>::asset_events)
.add_systems(
AssetEvents,
Assets::<A>::asset_events.run_if(Assets::<A>::asset_events_condition),
)
.add_systems(UpdateAssets, Assets::<A>::track_assets.in_set(TrackAssets))
}

Expand Down

0 comments on commit eee71bf

Please sign in to comment.