Put asset_events behind a run condition (#11800)

# 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>
This commit is contained in:
James Liu 2024-02-12 07:19:36 -08:00 committed by GitHub
parent 9e30aa7c92
commit eee71bfa93
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View file

@ -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`].

View file

@ -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))
}