Skip to content

Commit 79b9231

Browse files
committed
Move system_commands spans into apply_buffers (#6900)
# Objective A separate `tracing` span for running a system's commands is created, even if the system doesn't have commands. This is adding extra measuring overhead (see #4892) where it's not needed. ## Solution Move the span into `ParallelCommandState` and `CommandQueue`'s `SystemParamState::apply`. To get the right metadata for the span, a additional `&SystemMeta` parameter was added to `SystemParamState::apply`. --- ## Changelog Added: `SystemMeta::name` Changed: Systems without `Commands` and `ParallelCommands` will no longer show a "system_commands" span when profiling. Changed: `SystemParamState::apply` now takes a `&SystemMeta` parameter in addition to the provided `&mut World`.
1 parent 4820917 commit 79b9231

File tree

5 files changed

+35
-52
lines changed

5 files changed

+35
-52
lines changed

crates/bevy_ecs/macros/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ pub fn impl_param_set(_input: TokenStream) -> TokenStream {
285285
)*
286286
}
287287

288-
fn apply(&mut self, world: &mut World) {
289-
self.0.apply(world)
288+
fn apply(&mut self, system_meta: &SystemMeta, world: &mut World) {
289+
self.0.apply(system_meta, world)
290290
}
291291

292292
#[inline]
@@ -466,8 +466,8 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
466466
self.state.new_archetype(archetype, system_meta)
467467
}
468468

469-
fn apply(&mut self, world: &mut #path::world::World) {
470-
self.state.apply(world)
469+
fn apply(&mut self, system_meta: &#path::system::SystemMeta, world: &mut #path::world::World) {
470+
self.state.apply(system_meta, world)
471471
}
472472

473473
unsafe fn get_param<'w, 's>(

crates/bevy_ecs/src/schedule/stage.rs

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,10 @@ impl SystemStage {
229229
}
230230

231231
pub fn apply_buffers(&mut self, world: &mut World) {
232+
#[cfg(feature = "trace")]
233+
let _span = bevy_utils::tracing::info_span!("stage::apply_buffers").entered();
232234
for container in &mut self.parallel {
233-
let system = container.system_mut();
234-
#[cfg(feature = "trace")]
235-
let _span = bevy_utils::tracing::info_span!("system_commands", name = &*system.name())
236-
.entered();
237-
system.apply_buffers(world);
235+
container.system_mut().apply_buffers(world);
238236
}
239237
}
240238

@@ -781,15 +779,7 @@ impl Stage for SystemStage {
781779
.entered();
782780
container.system_mut().run((), world);
783781
}
784-
{
785-
#[cfg(feature = "trace")]
786-
let _system_span = bevy_utils::tracing::info_span!(
787-
"system_commands",
788-
name = &*container.name()
789-
)
790-
.entered();
791-
container.system_mut().apply_buffers(world);
792-
}
782+
container.system_mut().apply_buffers(world);
793783
}
794784
}
795785

@@ -813,28 +803,14 @@ impl Stage for SystemStage {
813803
.entered();
814804
container.system_mut().run((), world);
815805
}
816-
{
817-
#[cfg(feature = "trace")]
818-
let _system_span = bevy_utils::tracing::info_span!(
819-
"system_commands",
820-
name = &*container.name()
821-
)
822-
.entered();
823-
container.system_mut().apply_buffers(world);
824-
}
806+
container.system_mut().apply_buffers(world);
825807
}
826808
}
827809

828810
// Apply parallel systems' buffers.
829811
if self.apply_buffers {
830812
for container in &mut self.parallel {
831813
if container.should_run {
832-
#[cfg(feature = "trace")]
833-
let _span = bevy_utils::tracing::info_span!(
834-
"system_commands",
835-
name = &*container.name()
836-
)
837-
.entered();
838814
container.system_mut().apply_buffers(world);
839815
}
840816
}
@@ -852,15 +828,7 @@ impl Stage for SystemStage {
852828
.entered();
853829
container.system_mut().run((), world);
854830
}
855-
{
856-
#[cfg(feature = "trace")]
857-
let _system_span = bevy_utils::tracing::info_span!(
858-
"system_commands",
859-
name = &*container.name()
860-
)
861-
.entered();
862-
container.system_mut().apply_buffers(world);
863-
}
831+
container.system_mut().apply_buffers(world);
864832
}
865833
}
866834

crates/bevy_ecs/src/system/commands/parallel_scope.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use thread_local::ThreadLocal;
55
use crate::{
66
entity::Entities,
77
prelude::World,
8-
system::{SystemParam, SystemParamState},
8+
system::{SystemMeta, SystemParam, SystemParamState},
99
};
1010

1111
use super::{CommandQueue, Commands};
@@ -60,7 +60,11 @@ unsafe impl SystemParamState for ParallelCommandsState {
6060
Self::default()
6161
}
6262

63-
fn apply(&mut self, world: &mut World) {
63+
fn apply(&mut self, _system_meta: &SystemMeta, world: &mut World) {
64+
#[cfg(feature = "trace")]
65+
let _system_span =
66+
bevy_utils::tracing::info_span!("system_commands", name = _system_meta.name())
67+
.entered();
6468
for cq in &mut self.thread_local_storage {
6569
cq.get_mut().apply(world);
6670
}

crates/bevy_ecs/src/system/function_system.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ impl SystemMeta {
3737
}
3838
}
3939

40+
/// Returns the system's name
41+
#[inline]
42+
pub fn name(&self) -> &str {
43+
&self.name
44+
}
45+
4046
/// Returns true if the system is [`Send`].
4147
#[inline]
4248
pub fn is_send(&self) -> bool {
@@ -182,7 +188,7 @@ impl<Param: SystemParam> SystemState<Param> {
182188
/// This function should be called manually after the values returned by [`SystemState::get`] and [`SystemState::get_mut`]
183189
/// are finished being used.
184190
pub fn apply(&mut self, world: &mut World) {
185-
self.param_state.apply(world);
191+
self.param_state.apply(&self.meta, world);
186192
}
187193

188194
#[inline]
@@ -416,7 +422,7 @@ where
416422
#[inline]
417423
fn apply_buffers(&mut self, world: &mut World) {
418424
let param_state = self.param_state.as_mut().expect(Self::PARAM_MESSAGE);
419-
param_state.apply(world);
425+
param_state.apply(&self.system_meta, world);
420426
}
421427

422428
#[inline]

crates/bevy_ecs/src/system/system_param.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ pub unsafe trait SystemParamState: Send + Sync + 'static {
140140
#[inline]
141141
fn new_archetype(&mut self, _archetype: &Archetype, _system_meta: &mut SystemMeta) {}
142142
#[inline]
143-
fn apply(&mut self, _world: &mut World) {}
143+
#[allow(unused_variables)]
144+
fn apply(&mut self, system_meta: &SystemMeta, _world: &mut World) {}
144145

145146
type Item<'world, 'state>: SystemParam<State = Self>;
146147
/// # Safety
@@ -615,7 +616,11 @@ unsafe impl SystemParamState for CommandQueue {
615616
Default::default()
616617
}
617618

618-
fn apply(&mut self, world: &mut World) {
619+
fn apply(&mut self, _system_meta: &SystemMeta, world: &mut World) {
620+
#[cfg(feature = "trace")]
621+
let _system_span =
622+
bevy_utils::tracing::info_span!("system_commands", name = _system_meta.name())
623+
.entered();
619624
self.apply(world);
620625
}
621626

@@ -1473,9 +1478,9 @@ macro_rules! impl_system_param_tuple {
14731478
}
14741479

14751480
#[inline]
1476-
fn apply(&mut self, _world: &mut World) {
1481+
fn apply(&mut self, _system_meta: &SystemMeta, _world: &mut World) {
14771482
let ($($param,)*) = self;
1478-
$($param.apply(_world);)*
1483+
$($param.apply(_system_meta, _world);)*
14791484
}
14801485

14811486
#[inline]
@@ -1611,8 +1616,8 @@ unsafe impl<S: SystemParamState, P: SystemParam<State = S> + 'static> SystemPara
16111616
self.0.new_archetype(archetype, system_meta);
16121617
}
16131618

1614-
fn apply(&mut self, world: &mut World) {
1615-
self.0.apply(world);
1619+
fn apply(&mut self, system_meta: &SystemMeta, world: &mut World) {
1620+
self.0.apply(system_meta, world);
16161621
}
16171622

16181623
unsafe fn get_param<'world, 'state>(

0 commit comments

Comments
 (0)