From 5544f47ffdd244887073686ef68d5a19a3707688 Mon Sep 17 00:00:00 2001 From: james7132 Date: Fri, 9 Dec 2022 11:34:18 -0800 Subject: [PATCH 1/4] Move system_commands spans into apply_buffers --- crates/bevy_ecs/macros/src/lib.rs | 8 ++-- crates/bevy_ecs/src/schedule/stage.rs | 45 +++---------------- .../src/system/commands/parallel_scope.rs | 10 ++++- crates/bevy_ecs/src/system/function_system.rs | 12 +++-- crates/bevy_ecs/src/system/system_param.rs | 19 +++++--- 5 files changed, 41 insertions(+), 53 deletions(-) diff --git a/crates/bevy_ecs/macros/src/lib.rs b/crates/bevy_ecs/macros/src/lib.rs index 53121a6c7f883..ebbfab60be30d 100644 --- a/crates/bevy_ecs/macros/src/lib.rs +++ b/crates/bevy_ecs/macros/src/lib.rs @@ -276,8 +276,8 @@ pub fn impl_param_set(_input: TokenStream) -> TokenStream { )* } - fn apply(&mut self, world: &mut World) { - self.0.apply(world) + fn apply(&mut self, system_meta: &SystemMeta, world: &mut World) { + self.0.apply(system_meta, world) } } @@ -450,8 +450,8 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream { self.state.new_archetype(archetype, system_meta) } - fn apply(&mut self, world: &mut #path::world::World) { - self.state.apply(world) + fn apply(&mut self, system_meta: &#path::system::SystemMeta, world: &mut #path::world::World) { + self.state.apply(system_meta, world) } } diff --git a/crates/bevy_ecs/src/schedule/stage.rs b/crates/bevy_ecs/src/schedule/stage.rs index 238743965d7ef..366e1084aef57 100644 --- a/crates/bevy_ecs/src/schedule/stage.rs +++ b/crates/bevy_ecs/src/schedule/stage.rs @@ -229,12 +229,11 @@ impl SystemStage { } pub fn apply_buffers(&mut self, world: &mut World) { + #[cfg(feature = "trace")] + let _span = bevy_utils::tracing::info_span!("stage::apply_buffers") + .entered(); for container in &mut self.parallel { - let system = container.system_mut(); - #[cfg(feature = "trace")] - let _span = bevy_utils::tracing::info_span!("system_commands", name = &*system.name()) - .entered(); - system.apply_buffers(world); + container.system_mut().apply_buffers(world); } } @@ -781,15 +780,7 @@ impl Stage for SystemStage { .entered(); container.system_mut().run((), world); } - { - #[cfg(feature = "trace")] - let _system_span = bevy_utils::tracing::info_span!( - "system_commands", - name = &*container.name() - ) - .entered(); - container.system_mut().apply_buffers(world); - } + container.system_mut().apply_buffers(world); } } @@ -813,15 +804,7 @@ impl Stage for SystemStage { .entered(); container.system_mut().run((), world); } - { - #[cfg(feature = "trace")] - let _system_span = bevy_utils::tracing::info_span!( - "system_commands", - name = &*container.name() - ) - .entered(); - container.system_mut().apply_buffers(world); - } + container.system_mut().apply_buffers(world); } } @@ -829,12 +812,6 @@ impl Stage for SystemStage { if self.apply_buffers { for container in &mut self.parallel { if container.should_run { - #[cfg(feature = "trace")] - let _span = bevy_utils::tracing::info_span!( - "system_commands", - name = &*container.name() - ) - .entered(); container.system_mut().apply_buffers(world); } } @@ -852,15 +829,7 @@ impl Stage for SystemStage { .entered(); container.system_mut().run((), world); } - { - #[cfg(feature = "trace")] - let _system_span = bevy_utils::tracing::info_span!( - "system_commands", - name = &*container.name() - ) - .entered(); - container.system_mut().apply_buffers(world); - } + container.system_mut().apply_buffers(world); } } diff --git a/crates/bevy_ecs/src/system/commands/parallel_scope.rs b/crates/bevy_ecs/src/system/commands/parallel_scope.rs index 573afa70aae51..970d8aeaad022 100644 --- a/crates/bevy_ecs/src/system/commands/parallel_scope.rs +++ b/crates/bevy_ecs/src/system/commands/parallel_scope.rs @@ -5,7 +5,7 @@ use thread_local::ThreadLocal; use crate::{ entity::Entities, prelude::World, - system::{SystemParam, SystemParamFetch, SystemParamState}, + system::{SystemParam, SystemParamFetch, SystemParamState, SystemMeta}, }; use super::{CommandQueue, Commands}; @@ -74,7 +74,13 @@ unsafe impl SystemParamState for ParallelCommandsState { Self::default() } - fn apply(&mut self, world: &mut World) { + fn apply(&mut self, _system_meta: &SystemMeta, world: &mut World) { + #[cfg(feature = "trace")] + let _system_span = bevy_utils::tracing::info_span!( + "system_commands", + name = _system_meta.name() + ) + .entered(); for cq in &mut self.thread_local_storage { cq.get_mut().apply(world); } diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index c3f672e718734..e768b407fa52d 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -37,6 +37,12 @@ impl SystemMeta { } } + /// Returns true if the system is [`Send`]. + #[inline] + pub fn name(&self) -> &str { + &self.name + } + /// Returns true if the system is [`Send`]. #[inline] pub fn is_send(&self) -> bool { @@ -187,8 +193,8 @@ impl SystemState { /// by a [`Commands`](`super::Commands`) parameter to the given [`World`]. /// This function should be called manually after the values returned by [`SystemState::get`] and [`SystemState::get_mut`] /// are finished being used. - pub fn apply(&mut self, world: &mut World) { - self.param_state.apply(world); + pub fn apply(&mut self, system_meta: &SystemMeta, world: &mut World) { + self.param_state.apply(system_meta, world); } #[inline] @@ -422,7 +428,7 @@ where #[inline] fn apply_buffers(&mut self, world: &mut World) { let param_state = self.param_state.as_mut().expect(Self::PARAM_MESSAGE); - param_state.apply(world); + param_state.apply(&self.system_meta, world); } #[inline] diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index a10737acad773..81a3537cf228b 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -108,7 +108,8 @@ pub unsafe trait SystemParamState: Send + Sync + 'static { #[inline] fn new_archetype(&mut self, _archetype: &Archetype, _system_meta: &mut SystemMeta) {} #[inline] - fn apply(&mut self, _world: &mut World) {} + #[allow(unused_variables)] + fn apply(&mut self, system_meta: &SystemMeta, _world: &mut World) {} } /// A [`SystemParamFetch`] that only reads a given [`World`]. @@ -595,7 +596,13 @@ unsafe impl SystemParamState for CommandQueue { Default::default() } - fn apply(&mut self, world: &mut World) { + fn apply(&mut self, _system_meta: &SystemMeta, world: &mut World) { + #[cfg(feature = "trace")] + let _system_span = bevy_utils::tracing::info_span!( + "system_commands", + name = _system_meta.name() + ) + .entered(); self.apply(world); } } @@ -1499,9 +1506,9 @@ macro_rules! impl_system_param_tuple { } #[inline] - fn apply(&mut self, _world: &mut World) { + fn apply(&mut self, _system_meta: &SystemMeta, _world: &mut World) { let ($($param,)*) = self; - $($param.apply(_world);)* + $($param.apply(_system_meta, _world);)* } } }; @@ -1640,8 +1647,8 @@ unsafe impl SystemParamState self.0.new_archetype(archetype, system_meta); } - fn apply(&mut self, world: &mut World) { - self.0.apply(world); + fn apply(&mut self, system_meta: &SystemMeta, world: &mut World) { + self.0.apply(system_meta, world); } } From a1261c7fb37b598749fa6f8e609710665f45089b Mon Sep 17 00:00:00 2001 From: james7132 Date: Fri, 9 Dec 2022 11:56:13 -0800 Subject: [PATCH 2/4] Formatting --- crates/bevy_ecs/src/schedule/stage.rs | 3 +-- crates/bevy_ecs/src/system/commands/parallel_scope.rs | 10 ++++------ crates/bevy_ecs/src/system/system_param.rs | 8 +++----- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/stage.rs b/crates/bevy_ecs/src/schedule/stage.rs index 366e1084aef57..d7386d2e5990d 100644 --- a/crates/bevy_ecs/src/schedule/stage.rs +++ b/crates/bevy_ecs/src/schedule/stage.rs @@ -230,8 +230,7 @@ impl SystemStage { pub fn apply_buffers(&mut self, world: &mut World) { #[cfg(feature = "trace")] - let _span = bevy_utils::tracing::info_span!("stage::apply_buffers") - .entered(); + let _span = bevy_utils::tracing::info_span!("stage::apply_buffers").entered(); for container in &mut self.parallel { container.system_mut().apply_buffers(world); } diff --git a/crates/bevy_ecs/src/system/commands/parallel_scope.rs b/crates/bevy_ecs/src/system/commands/parallel_scope.rs index 970d8aeaad022..7328ad11cbbae 100644 --- a/crates/bevy_ecs/src/system/commands/parallel_scope.rs +++ b/crates/bevy_ecs/src/system/commands/parallel_scope.rs @@ -5,7 +5,7 @@ use thread_local::ThreadLocal; use crate::{ entity::Entities, prelude::World, - system::{SystemParam, SystemParamFetch, SystemParamState, SystemMeta}, + system::{SystemMeta, SystemParam, SystemParamFetch, SystemParamState}, }; use super::{CommandQueue, Commands}; @@ -76,11 +76,9 @@ unsafe impl SystemParamState for ParallelCommandsState { fn apply(&mut self, _system_meta: &SystemMeta, world: &mut World) { #[cfg(feature = "trace")] - let _system_span = bevy_utils::tracing::info_span!( - "system_commands", - name = _system_meta.name() - ) - .entered(); + let _system_span = + bevy_utils::tracing::info_span!("system_commands", name = _system_meta.name()) + .entered(); for cq in &mut self.thread_local_storage { cq.get_mut().apply(world); } diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 81a3537cf228b..922f39de5465a 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -598,11 +598,9 @@ unsafe impl SystemParamState for CommandQueue { fn apply(&mut self, _system_meta: &SystemMeta, world: &mut World) { #[cfg(feature = "trace")] - let _system_span = bevy_utils::tracing::info_span!( - "system_commands", - name = _system_meta.name() - ) - .entered(); + let _system_span = + bevy_utils::tracing::info_span!("system_commands", name = _system_meta.name()) + .entered(); self.apply(world); } } From 109cf969840455879fe9aae0f60615469eed2876 Mon Sep 17 00:00:00 2001 From: James Liu Date: Fri, 9 Dec 2022 13:43:25 -0800 Subject: [PATCH 3/4] Remove redundant `&SystemMeta` parameter on `SystemState::apply`. Co-authored-by: Mike --- crates/bevy_ecs/src/system/function_system.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index e768b407fa52d..69c8f6982e673 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -193,8 +193,8 @@ impl SystemState { /// by a [`Commands`](`super::Commands`) parameter to the given [`World`]. /// This function should be called manually after the values returned by [`SystemState::get`] and [`SystemState::get_mut`] /// are finished being used. - pub fn apply(&mut self, system_meta: &SystemMeta, world: &mut World) { - self.param_state.apply(system_meta, world); + pub fn apply(&mut self, world: &mut World) { + self.param_state.apply(&self.meta, world); } #[inline] From a85d2bc98c489972e7f01e029aa3e7cd55fc224d Mon Sep 17 00:00:00 2001 From: James Liu Date: Sat, 10 Dec 2022 14:59:53 -0800 Subject: [PATCH 4/4] Fix copypasta'ed docs Co-authored-by: Pascal Hertleif --- crates/bevy_ecs/src/system/function_system.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/system/function_system.rs b/crates/bevy_ecs/src/system/function_system.rs index 69c8f6982e673..4ab0308736f0e 100644 --- a/crates/bevy_ecs/src/system/function_system.rs +++ b/crates/bevy_ecs/src/system/function_system.rs @@ -37,7 +37,7 @@ impl SystemMeta { } } - /// Returns true if the system is [`Send`]. + /// Returns the system's name #[inline] pub fn name(&self) -> &str { &self.name