diff --git a/Cargo.toml b/Cargo.toml index f9eb521cab..b540d6a486 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ mlua_serialize = ["bevy_mod_scripting_lua?/mlua_serialize"] mlua_macros = ["bevy_mod_scripting_lua?/mlua_macros"] mlua_async = ["bevy_mod_scripting_lua?/mlua_async"] + ## rhai rhai = ["bevy_mod_scripting_rhai"] @@ -56,8 +57,8 @@ bevy_mod_scripting_lua = { path = "crates/languages/bevy_mod_scripting_lua", ver bevy_mod_scripting_rhai = { path = "crates/languages/bevy_mod_scripting_rhai", version = "0.9.0-alpha.7", optional = true } # bevy_mod_scripting_rune = { path = "crates/languages/bevy_mod_scripting_rune", version = "0.9.0-alpha.2", optional = true } bevy_mod_scripting_functions = { workspace = true } - [workspace.dependencies] +profiling = {version = "1.0" } bevy = { version = "0.15.0", default-features = false } bevy_mod_scripting_core = { path = "crates/bevy_mod_scripting_core", version = "0.9.0-alpha.8" } bevy_mod_scripting_functions = { path = "crates/bevy_mod_scripting_functions", version = "0.9.0-alpha.8", default-features = false } diff --git a/crates/bevy_mod_scripting_core/Cargo.toml b/crates/bevy_mod_scripting_core/Cargo.toml index e1336749d7..6ec8e0c1d5 100644 --- a/crates/bevy_mod_scripting_core/Cargo.toml +++ b/crates/bevy_mod_scripting_core/Cargo.toml @@ -40,7 +40,7 @@ dashmap = "6" smallvec = "1.11" itertools = "0.13" derivative = "2.2" - +profiling = {workspace = true} [dev-dependencies] test_utils = { workspace = true } diff --git a/crates/bevy_mod_scripting_core/src/bindings/access_map.rs b/crates/bevy_mod_scripting_core/src/bindings/access_map.rs index 02ba704485..5fefab3d2d 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/access_map.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/access_map.rs @@ -211,7 +211,7 @@ pub struct AccessMap { individual_accesses: DashMap, global_lock: RwLock, } - +#[profiling::all_functions] impl AccessMap { pub fn is_locked_exclusively(&self) -> bool { let global_lock = self.global_lock.read(); diff --git a/crates/bevy_mod_scripting_core/src/bindings/allocator.rs b/crates/bevy_mod_scripting_core/src/bindings/allocator.rs index d31fcc9090..269ccf26e4 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/allocator.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/allocator.rs @@ -141,7 +141,7 @@ pub struct ReflectAllocator { allocations: HashMap, types: HashMap, } - +#[profiling::all_functions] impl ReflectAllocator { /// Allocates a new [`Reflect`] value and returns an [`AllocationId`] which can be used to access it later. /// Use [`Self::allocate_boxed`] if you already have an allocated boxed value. @@ -206,6 +206,7 @@ impl ReflectAllocator { } /// Cleans up dangling script allocations +#[profiling::function] pub fn garbage_collector(allocator: ResMut) { let mut allocator = allocator.write(); allocator.clean_garbage_allocations() diff --git a/crates/bevy_mod_scripting_core/src/bindings/function/from.rs b/crates/bevy_mod_scripting_core/src/bindings/function/from.rs index 254beed275..efd28d2dc9 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/function/from.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/function/from.rs @@ -43,7 +43,7 @@ impl FromScript for () { impl FromScript for bool { type This<'w> = Self; - + #[profiling::function] fn from_script( value: ScriptValue, world: WorldGuard<'_>, @@ -67,6 +67,7 @@ macro_rules! impl_from_with_downcast { $( impl FromScript for $ty { type This<'w> = Self; + #[profiling::function] fn from_script(value: ScriptValue, world: WorldGuard) -> Result { match value { ScriptValue::Integer(i) => Ok(i as $ty), @@ -88,6 +89,7 @@ macro_rules! impl_from_stringlike { $( impl FromScript for $ty { type This<'w> = Self; + #[profiling::function] fn from_script(value: ScriptValue, world: WorldGuard) -> Result { match value { ScriptValue::String(s) => Ok(s.to_string().into()), @@ -104,7 +106,7 @@ impl_from_stringlike!(String, PathBuf, OsString); impl FromScript for char { type This<'w> = Self; - + #[profiling::function] fn from_script( value: ScriptValue, world: WorldGuard<'_>, @@ -128,6 +130,7 @@ impl FromScript for char { impl FromScript for ReflectReference { type This<'w> = Self; + #[profiling::function] fn from_script(value: ScriptValue, _world: WorldGuard) -> Result { match value { ScriptValue::Reference(r) => Ok(r), @@ -178,6 +181,7 @@ impl From for Val { impl FromScript for Val { type This<'w> = Self; + #[profiling::function] fn from_script(value: ScriptValue, world: WorldGuard) -> Result { match value { ScriptValue::Reference(reflect_reference) => Ok(Val(reflect_reference.with_reflect( @@ -221,7 +225,7 @@ impl Deref for Ref<'_, T> { impl FromScript for Ref<'_, T> { type This<'a> = Ref<'a, T>; - + #[profiling::function] fn from_script( value: ScriptValue, world: WorldGuard<'_>, @@ -293,7 +297,7 @@ impl<'a, T> From<&'a mut T> for Mut<'a, T> { impl FromScript for Mut<'_, T> { type This<'w> = Mut<'w, T>; - + #[profiling::function] fn from_script( value: ScriptValue, world: WorldGuard<'_>, @@ -331,7 +335,7 @@ where for<'w> T::This<'w>: Into, { type This<'w> = Self; - + #[profiling::function] fn from_script(value: ScriptValue, world: WorldGuard) -> Result { match value { ScriptValue::Unit => Ok(None), @@ -345,7 +349,7 @@ where for<'w> T::This<'w>: Into, { type This<'w> = Self; - + #[profiling::function] fn from_script(value: ScriptValue, world: WorldGuard) -> Result { match value { ScriptValue::List(list) => { @@ -368,7 +372,7 @@ where for<'w> T::This<'w>: Into, { type This<'w> = Self; - + #[profiling::function] fn from_script(value: ScriptValue, world: WorldGuard) -> Result { match value { ScriptValue::List(list) if list.len() == N => { @@ -390,7 +394,7 @@ where impl FromScript for DynamicScriptFunctionMut { type This<'w> = Self; - + #[profiling::function] fn from_script(value: ScriptValue, _: WorldGuard<'_>) -> Result, InteropError> where Self: Sized, @@ -407,7 +411,7 @@ impl FromScript for DynamicScriptFunctionMut { impl FromScript for DynamicScriptFunction { type This<'w> = Self; - + #[profiling::function] fn from_script(value: ScriptValue, _: WorldGuard<'_>) -> Result, InteropError> where Self: Sized, @@ -428,7 +432,7 @@ where for<'w> V::This<'w>: Into, { type This<'w> = Self; - + #[profiling::function] fn from_script(value: ScriptValue, world: WorldGuard) -> Result { match value { ScriptValue::Map(map) => { diff --git a/crates/bevy_mod_scripting_core/src/bindings/function/from_ref.rs b/crates/bevy_mod_scripting_core/src/bindings/function/from_ref.rs index 36c3bd1fd2..af23175fa3 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/function/from_ref.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/function/from_ref.rs @@ -24,6 +24,7 @@ pub trait FromScriptRef { } impl FromScriptRef for Box { + #[profiling::function] fn from_script_ref( target: TypeId, value: ScriptValue, diff --git a/crates/bevy_mod_scripting_core/src/bindings/function/into_ref.rs b/crates/bevy_mod_scripting_core/src/bindings/function/into_ref.rs index a9358da882..eb2496460e 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/function/into_ref.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/function/into_ref.rs @@ -53,6 +53,7 @@ macro_rules! downcast_into_value { } impl IntoScriptRef for ReflectReference { + #[profiling::function] fn into_script_ref( self_: ReflectReference, world: WorldGuard, @@ -60,7 +61,7 @@ impl IntoScriptRef for ReflectReference { self_.with_reflect(world.clone(), |r| into_script_ref(self_.clone(), r, world))? } } - +#[profiling::function] fn into_script_ref( mut self_: ReflectReference, r: &dyn PartialReflect, diff --git a/crates/bevy_mod_scripting_core/src/bindings/function/script_function.rs b/crates/bevy_mod_scripting_core/src/bindings/function/script_function.rs index 52d6e4bab9..890f340d41 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/function/script_function.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/function/script_function.rs @@ -100,6 +100,7 @@ impl DynamicScriptFunction { args: I, context: FunctionCallContext, ) -> Result { + profiling::scope!("Dynamic Call ", self.name().clone()); let args = args.into_iter().collect::>(); // should we be inlining call errors into the return value? let return_val = (self.func)(context, args); @@ -151,6 +152,7 @@ impl DynamicScriptFunctionMut { args: I, context: FunctionCallContext, ) -> Result { + profiling::scope!("Dynamic Call Mut", self.name().clone()); let args = args.into_iter().collect::>(); // should we be inlining call errors into the return value? let mut write = self.func.write(); @@ -276,7 +278,7 @@ pub struct FunctionKey { pub struct ScriptFunctionRegistry { functions: HashMap, } - +#[profiling::all_functions] impl ScriptFunctionRegistry { /// Register a script function with the given name. If the name already exists, /// the new function will be registered as an overload of the function. @@ -525,7 +527,9 @@ macro_rules! impl_script_function { $( $param::This<'env>: Into<$param>,)* { #[allow(unused_mut,unused_variables)] + #[profiling::function] fn $trait_fn_name(mut self) -> $dynamic_type { + let func = (move |caller_context: FunctionCallContext, mut args: VecDeque | { let res: Result = (|| { let received_args_len = args.len(); diff --git a/crates/bevy_mod_scripting_core/src/bindings/pretty_print.rs b/crates/bevy_mod_scripting_core/src/bindings/pretty_print.rs index 800e48f2bb..5f5a20d7f6 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/pretty_print.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/pretty_print.rs @@ -342,7 +342,7 @@ pub trait DisplayWithWorld: std::fmt::Debug { self.display_with_world(world) } } - +#[profiling::all_functions] impl DisplayWithWorld for ReflectReference { fn display_with_world(&self, world: WorldGuard) -> String { ReflectReferencePrinter::new(self.clone()).pretty_print(Some(world)) @@ -356,7 +356,7 @@ impl DisplayWithWorld for ReflectReference { ReflectReferencePrinter::new(self.clone()).pretty_print(None) } } - +#[profiling::all_functions] impl DisplayWithWorld for ReflectBaseType { fn display_with_world(&self, world: WorldGuard) -> String { let mut string = String::new(); @@ -374,7 +374,7 @@ impl DisplayWithWorld for ReflectBaseType { string } } - +#[profiling::all_functions] impl DisplayWithWorld for ComponentId { fn display_without_world(&self) -> String { format!("ComponentOrResource({})", self.index()) @@ -393,7 +393,7 @@ impl DisplayWithWorld for ComponentId { } } } - +#[profiling::all_functions] impl DisplayWithWorld for ReflectAccessId { fn display_without_world(&self) -> String { match self.kind { @@ -425,7 +425,7 @@ impl DisplayWithWorld for ReflectAccessId { } } } - +#[profiling::all_functions] impl DisplayWithWorld for TypeId { fn display_with_world(&self, world: WorldGuard) -> String { if *self == TypeId::of::() { @@ -455,7 +455,7 @@ impl DisplayWithWorld for TypeId { format!("{:?}", self) } } - +#[profiling::all_functions] impl DisplayWithWorld for ScriptValue { fn display_with_world(&self, world: WorldGuard) -> String { match self { @@ -509,7 +509,7 @@ impl DisplayWithWorld for ScriptValue { } } } - +#[profiling::all_functions] impl DisplayWithWorld for Vec { fn display_with_world(&self, world: WorldGuard) -> String { let mut string = String::new(); @@ -550,7 +550,7 @@ impl DisplayWithWorld for Vec { string } } - +#[profiling::all_functions] impl DisplayWithWorld for String { fn display_with_world(&self, _world: WorldGuard) -> String { self.to_string() @@ -564,7 +564,7 @@ impl DisplayWithWorld for String { self.to_string() } } - +#[profiling::all_functions] impl DisplayWithWorld for std::collections::HashMap { diff --git a/crates/bevy_mod_scripting_core/src/bindings/query.rs b/crates/bevy_mod_scripting_core/src/bindings/query.rs index 78ebe6b9fe..642d49b857 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/query.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/query.rs @@ -161,7 +161,7 @@ pub struct ScriptQueryResult { // self.try_read().and_then(|world| world.query(query)) // } // } - +#[profiling::all_functions] impl WorldAccessGuard<'_> { pub fn query( &self, diff --git a/crates/bevy_mod_scripting_core/src/bindings/reference.rs b/crates/bevy_mod_scripting_core/src/bindings/reference.rs index dfc8dc01a9..83e1dd1e44 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/reference.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/reference.rs @@ -58,7 +58,7 @@ pub enum TypeIdSource { /// Givent the Tail reference is a container type, use the type id of the keys of the container Key, } - +#[profiling::all_functions] impl ReflectReference { /// Creates a new infinite iterator. This iterator will keep returning the next element reference forever. pub fn into_iter_infinite(self) -> ReflectRefIter { @@ -421,7 +421,7 @@ pub enum ReflectBase { Resource(ComponentId), Owned(ReflectAllocationId), } - +#[profiling::all_functions] impl ReflectBase { /// Retrieves the pointer to the underlying `dyn PartialReflect` object valid for the 'w lifteime of the world cell /// @@ -469,7 +469,7 @@ pub trait ReflectionPathExt { fn iter(&self) -> impl Iterator; } - +#[profiling::all_functions] impl ReflectionPathExt for ParsedPath { /// Assumes the accesses are 1 indexed and converts them to 0 indexed fn convert_to_0_indexed(&mut self) { @@ -505,7 +505,7 @@ pub struct ReflectRefIter { pub enum IterationKey { Index(usize), } - +#[profiling::all_functions] impl ReflectRefIter { pub fn new_indexed(base: ReflectReference) -> Self { Self { @@ -537,7 +537,7 @@ impl ReflectRefIter { const fn list_index_access(index: usize) -> bevy::reflect::Access<'static> { bevy::reflect::Access::ListIndex(index) } - +#[profiling::all_functions] impl Iterator for ReflectRefIter { type Item = Result; diff --git a/crates/bevy_mod_scripting_core/src/bindings/script_value.rs b/crates/bevy_mod_scripting_core/src/bindings/script_value.rs index b623274305..b1b67de1cf 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/script_value.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/script_value.rs @@ -147,7 +147,7 @@ impl From> for ScriptValue { ScriptValue::Map(value) } } - +#[profiling::all_functions] impl TryFrom for ParsedPath { type Error = InteropError; fn try_from(value: ScriptValue) -> Result { diff --git a/crates/bevy_mod_scripting_core/src/bindings/world.rs b/crates/bevy_mod_scripting_core/src/bindings/world.rs index d3d983331b..43eb7ca89d 100644 --- a/crates/bevy_mod_scripting_core/src/bindings/world.rs +++ b/crates/bevy_mod_scripting_core/src/bindings/world.rs @@ -69,7 +69,7 @@ impl WorldAccessGuard<'static> { unsafe { std::mem::transmute(self) } } } - +#[profiling::all_functions] impl<'w> WorldAccessGuard<'w> { /// Safely allows access to the world for the duration of the closure via a static [`WorldAccessGuard`]. /// @@ -428,6 +428,7 @@ impl<'w> WorldAccessGuard<'w> { } /// Impl block for higher level world methods +#[profiling::all_functions] impl WorldAccessGuard<'_> { pub fn spawn(&self) -> Result { self.with_global_access(|world| { @@ -826,7 +827,7 @@ pub struct ThreadWorldContainer; thread_local! { static WORLD_CALLBACK_ACCESS: RefCell>> = const { RefCell::new(None) }; } - +#[profiling::all_functions] impl WorldContainer for ThreadWorldContainer { type Error = InteropError; diff --git a/crates/bevy_mod_scripting_core/src/extractors.rs b/crates/bevy_mod_scripting_core/src/extractors.rs index 035766dba5..fb3f9a9e10 100644 --- a/crates/bevy_mod_scripting_core/src/extractors.rs +++ b/crates/bevy_mod_scripting_core/src/extractors.rs @@ -21,7 +21,7 @@ pub(crate) struct HandlerContext { pub runtime_container: RuntimeContainer

, pub script_contexts: ScriptContexts

, } - +#[profiling::function] pub(crate) fn extract_handler_context( world: &mut World, ) -> Result, MissingResourceError> { @@ -53,7 +53,7 @@ pub(crate) fn extract_handler_context( script_contexts, }) } - +#[profiling::function] pub(crate) fn yield_handler_context( world: &mut World, context: HandlerContext

, diff --git a/crates/bevy_mod_scripting_core/src/handler.rs b/crates/bevy_mod_scripting_core/src/handler.rs index 12e75a3377..ac99e73fb3 100644 --- a/crates/bevy_mod_scripting_core/src/handler.rs +++ b/crates/bevy_mod_scripting_core/src/handler.rs @@ -46,7 +46,7 @@ impl Clone for CallbackSettings

{ } } } - +#[profiling::all_functions] impl CallbackSettings

{ pub fn new(callback_handler: HandlerFn

) -> Self { Self { callback_handler } @@ -92,6 +92,7 @@ macro_rules! push_err_and_continue { } /// A utility to separate the event handling logic from the retrieval of the handler context +#[profiling::function] pub(crate) fn event_handler_internal( world: &mut World, res_ctxt: &mut HandlerContext

, @@ -182,6 +183,7 @@ pub(crate) fn event_handler_internal( world: &mut World, params: &mut SystemState<( diff --git a/crates/bevy_mod_scripting_functions/Cargo.toml b/crates/bevy_mod_scripting_functions/Cargo.toml index 8b85fa5a97..adef06b9cf 100644 --- a/crates/bevy_mod_scripting_functions/Cargo.toml +++ b/crates/bevy_mod_scripting_functions/Cargo.toml @@ -30,6 +30,7 @@ bevy = { workspace = true, features = [ "file_watcher", "multi_threaded", ] } +profiling = {workspace = true} uuid = "1.11" smol_str = "0.2.2" bevy_mod_scripting_core = { workspace = true } diff --git a/crates/bevy_mod_scripting_functions/src/core.rs b/crates/bevy_mod_scripting_functions/src/core.rs index 751c52838e..81d20abe91 100644 --- a/crates/bevy_mod_scripting_functions/src/core.rs +++ b/crates/bevy_mod_scripting_functions/src/core.rs @@ -32,6 +32,8 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat .register( "get_type_by_name", |ctxt: FunctionCallContext, type_name: String| { + profiling::function_scope!("get_type_by_name"); + profiling::function_scope!("get_type_by_name"); let world = ctxt.world()?; let val = world.get_type_by_name(type_name); @@ -76,6 +78,7 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat |ctxt: FunctionCallContext, entity: Val, registration: Val| { + profiling::function_scope!("get_component"); let world = ctxt.world()?; world.get_component(*entity, registration.component_id()) }, @@ -85,6 +88,7 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat |ctxt: FunctionCallContext, entity: Val, registration: Val| { + profiling::function_scope!("has_component"); let world = ctxt.world()?; world.has_component(*entity, registration.component_id()) }, @@ -92,6 +96,7 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat .register( "remove_component", |ctxt: FunctionCallContext, e: Val, r: Val| { + profiling::function_scope!("remove_component"); let world = ctxt.world()?; world.remove_component(*e, r.clone()) }, @@ -99,6 +104,7 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat .register( "get_resource", |ctxt: FunctionCallContext, registration: Val| { + profiling::function_scope!("get_resource"); let world = ctxt.world()?; world.get_resource(registration.resource_id()) }, @@ -106,6 +112,7 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat .register( "has_resource", |ctxt: FunctionCallContext, registration: Val| { + profiling::function_scope!("has_resource"); let world = ctxt.world()?; world.has_resource(registration.resource_id()) }, @@ -113,6 +120,7 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat .register( "remove_resource", |ctxt: FunctionCallContext, r: Val| { + profiling::function_scope!("remove_resource"); let world = ctxt.world()?; world.remove_resource(r.into_inner()) }, @@ -120,11 +128,13 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat .register( "add_default_component", |ctxt: FunctionCallContext, e: Val, r: Val| { + profiling::function_scope!("add_default_component"); let world = ctxt.world()?; world.add_default_component(*e, r.clone()) }, ) .register("spawn", |ctxt: FunctionCallContext| { + profiling::function_scope!("spawn"); let world = ctxt.world()?; Ok(Val(world.spawn()?)) }) @@ -134,6 +144,7 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat e: Val, r: Val, v: ReflectReference| { + profiling::function_scope!("insert_component"); let world = ctxt.world()?; world.insert_component(*e, r.into_inner(), v) }, @@ -141,6 +152,7 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat .register( "insert_children", |ctxt: FunctionCallContext, e: Val, index: usize, c: Vec>| { + profiling::function_scope!("insert_children"); let world = ctxt.world()?; let index = if ctxt.convert_to_0_indexed { index - 1 @@ -153,6 +165,7 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat .register( "push_children", |ctxt: FunctionCallContext, e: Val, c: Vec>| { + profiling::function_scope!("push_children"); let world = ctxt.world()?; world.push_children(*e, &c.into_iter().map(|v| *v).collect::>()) }, @@ -160,23 +173,27 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat .register( "get_children", |ctxt: FunctionCallContext, e: Val| { + profiling::function_scope!("get_children"); let world = ctxt.world()?; let children = world.get_children(*e)?; Ok(children.into_iter().map(Val).collect::>()) }, ) .register("get_parent", |ctxt: FunctionCallContext, e: Val| { + profiling::function_scope!("get_parent"); let world = ctxt.world()?; let parent = world.get_parent(*e)?; Ok(parent.map(Val)) }) .register("despawn", |ctxt: FunctionCallContext, e: Val| { + profiling::function_scope!("despawn"); let world = ctxt.world()?; world.despawn(*e) }) .register( "despawn_descendants", |ctxt: FunctionCallContext, e: Val| { + profiling::function_scope!("despawn_descendants"); let world = ctxt.world()?; world.despawn_descendants(*e) }, @@ -184,19 +201,23 @@ pub fn register_world_functions(reg: &mut World) -> Result<(), FunctionRegistrat .register( "despawn_recursive", |ctxt: FunctionCallContext, e: Val| { + profiling::function_scope!("despawn_recursive"); let world = ctxt.world()?; world.despawn_recursive(*e) }, ) .register("has_entity", |ctxt: FunctionCallContext, e: Val| { + profiling::function_scope!("has_entity"); let world = ctxt.world()?; world.has_entity(*e) }) .register("query", || { + profiling::function_scope!("query"); let query_builder = ScriptQueryBuilder::default(); Ok(Val(query_builder)) }) .register("exit", |ctxt: FunctionCallContext| { + profiling::function_scope!("exit"); let world = ctxt.world()?; world.exit() }); @@ -210,19 +231,22 @@ pub fn register_reflect_reference_functions( .register( "display_ref", |ctxt: FunctionCallContext, s: ReflectReference| { + profiling::function_scope!("display_ref"); let world = ctxt.world()?; Ok(s.display_with_world(world)) }, ) .register("display_value", |ctxt: FunctionCallContext, s: ReflectReference| { + profiling::function_scope!("display_value"); let world = ctxt.world()?; Ok(s.display_value_with_world(world)) }) .register( "get", |ctxt: FunctionCallContext, - mut self_: ReflectReference, - key: ScriptValue| { + mut self_: ReflectReference, + key: ScriptValue| { + profiling::function_scope!("get"); let mut path: ParsedPath = key.try_into()?; if ctxt.convert_to_0_indexed { path.convert_to_0_indexed(); @@ -235,9 +259,10 @@ pub fn register_reflect_reference_functions( .register( "set", |ctxt: FunctionCallContext, - self_: ScriptValue, - key: ScriptValue, - value: ScriptValue| { + self_: ScriptValue, + key: ScriptValue, + value: ScriptValue| { + profiling::function_scope!("set"); if let ScriptValue::Reference(mut self_) = self_ { let world = ctxt.world()?; let mut path: ParsedPath = key.try_into()?; @@ -268,6 +293,7 @@ pub fn register_reflect_reference_functions( .register( "push", |ctxt: FunctionCallContext, s: ReflectReference, v: ScriptValue| { + profiling::function_scope!("push"); let world = ctxt.world()?; let target_type_id = s.element_type_id(world.clone())?.ok_or_else(|| { InteropError::unsupported_operation( @@ -281,6 +307,7 @@ pub fn register_reflect_reference_functions( }, ) .register("pop", |ctxt: FunctionCallContext, s: ReflectReference| { + profiling::function_scope!("pop"); let world = ctxt.world()?; let o = s.with_reflect_mut(world.clone(), |s| s.try_pop_boxed())??; let reference = { @@ -292,6 +319,7 @@ pub fn register_reflect_reference_functions( ReflectReference::into_script_ref(reference, world) }) .register("insert", |ctxt: FunctionCallContext, s: ReflectReference, k: ScriptValue, v: ScriptValue| { + profiling::function_scope!("insert"); let world = ctxt.world()?; let key_type_id = s.key_type_id(world.clone())?.ok_or_else(|| { InteropError::unsupported_operation( @@ -320,14 +348,17 @@ pub fn register_reflect_reference_functions( s.with_reflect_mut(world, |s| s.try_insert_boxed(key, value))? }) .register("clear", |ctxt: FunctionCallContext, s: ReflectReference| { + profiling::function_scope!("clear"); let world = ctxt.world()?; s.with_reflect_mut(world, |s| s.try_clear())? }) .register("len", |ctxt: FunctionCallContext, s: ReflectReference| { + profiling::function_scope!("len"); let world = ctxt.world()?; s.len(world) }) .register("remove", |ctxt: FunctionCallContext, s: ReflectReference, k: ScriptValue| { + profiling::function_scope!("remove"); let world = ctxt.world()?; let key_type_id = s.key_type_id(world.clone())?.ok_or_else(|| { InteropError::unsupported_operation( @@ -357,6 +388,7 @@ pub fn register_reflect_reference_functions( } }) .register("iter", |ctxt: FunctionCallContext, s: ReflectReference| { + profiling::function_scope!("iter"); let world = ctxt.world()?; let mut len = s.len(world.clone())?.unwrap_or_default(); let mut infinite_iter = s.into_iter_infinite(); @@ -380,6 +412,7 @@ pub fn register_reflect_reference_functions( Ok(iter_function.into_dynamic_script_function_mut()) }) .register("functions", |ctxt: FunctionCallContext, s: ReflectReference| { + profiling::function_scope!("functions"); let world = ctxt.world()?; let type_id = s.tail_type_id(world.clone())?.or_fake_id(); let functions = world.get_functions_on_type(type_id) @@ -428,6 +461,7 @@ pub fn register_script_query_builder_functions( .register( "component", |s: Val, components: Val| { + profiling::function_scope!("component"); let mut builder = s.into_inner(); builder.component(components.into_inner()); Val(builder) @@ -436,6 +470,7 @@ pub fn register_script_query_builder_functions( .register( "with", |s: Val, with: Val| { + profiling::function_scope!("with"); let mut builder = s.into_inner(); builder.with_component(with.into_inner()); Val(builder) @@ -444,6 +479,7 @@ pub fn register_script_query_builder_functions( .register( "without", |s: Val, without: Val| { + profiling::function_scope!("without"); let mut builder = s.into_inner(); builder.without_component(without.into_inner()); Val(builder) @@ -452,6 +488,7 @@ pub fn register_script_query_builder_functions( .register( "build", |ctxt: FunctionCallContext, s: Val| { + profiling::function_scope!("build"); let world = ctxt.world()?; let builder = s.into_inner(); let result = world.query(builder)?; @@ -470,6 +507,7 @@ pub fn register_script_query_result_functions( .register("components", |s: Ref| { s.components.to_vec() }); + Ok(()) } diff --git a/crates/languages/bevy_mod_scripting_lua/Cargo.toml b/crates/languages/bevy_mod_scripting_lua/Cargo.toml index 53138f8f11..0da52f8b0b 100644 --- a/crates/languages/bevy_mod_scripting_lua/Cargo.toml +++ b/crates/languages/bevy_mod_scripting_lua/Cargo.toml @@ -45,6 +45,7 @@ parking_lot = "0.12.1" uuid = "1.1" smol_str = "0.2.2" smallvec = "1.13" +profiling = {workspace = true} [dev-dependencies] script_integration_test_harness = { workspace = true } diff --git a/crates/languages/bevy_mod_scripting_lua/src/bindings/reference.rs b/crates/languages/bevy_mod_scripting_lua/src/bindings/reference.rs index 116d317d87..2711619cd2 100644 --- a/crates/languages/bevy_mod_scripting_lua/src/bindings/reference.rs +++ b/crates/languages/bevy_mod_scripting_lua/src/bindings/reference.rs @@ -38,6 +38,7 @@ impl UserData for LuaReflectReference { m.add_meta_function( MetaMethod::Index, |_, (self_, key): (LuaReflectReference, LuaScriptValue)| { + profiling::function_scope!("MetaMethod::Index"); let world = ThreadWorldContainer.try_get_world()?; let self_: ReflectReference = self_.into(); let type_id = self_.tail_type_id(world.clone())?.or_fake_id(); @@ -72,6 +73,7 @@ impl UserData for LuaReflectReference { m.add_meta_function( MetaMethod::NewIndex, |_, (self_, key, value): (LuaReflectReference, LuaScriptValue, LuaScriptValue)| { + profiling::function_scope!("MetaMethod::NewIndex"); let world = ThreadWorldContainer.try_get_world()?; let self_: ReflectReference = self_.into(); let key: ScriptValue = key.into(); @@ -96,6 +98,7 @@ impl UserData for LuaReflectReference { m.add_meta_function( MetaMethod::Sub, |_, (self_, other): (LuaReflectReference, LuaScriptValue)| { + profiling::function_scope!("MetaMethod::Sub"); let world = ThreadWorldContainer.try_get_world()?; let self_: ReflectReference = self_.into(); let other: ScriptValue = other.into(); @@ -110,6 +113,7 @@ impl UserData for LuaReflectReference { m.add_meta_function( MetaMethod::Add, |_, (self_, other): (LuaReflectReference, LuaScriptValue)| { + profiling::function_scope!("MetaMethod::Add"); let world = ThreadWorldContainer.try_get_world()?; let self_: ReflectReference = self_.into(); let other: ScriptValue = other.into(); @@ -124,6 +128,7 @@ impl UserData for LuaReflectReference { m.add_meta_function( MetaMethod::Mul, |_, (self_, other): (LuaReflectReference, LuaScriptValue)| { + profiling::function_scope!("MetaMethod::Mul"); let world = ThreadWorldContainer.try_get_world()?; let self_: ReflectReference = self_.into(); let other: ScriptValue = other.into(); @@ -138,6 +143,7 @@ impl UserData for LuaReflectReference { m.add_meta_function( MetaMethod::Div, |_, (self_, other): (LuaReflectReference, LuaScriptValue)| { + profiling::function_scope!("MetaMethod::Div"); let world = ThreadWorldContainer.try_get_world()?; let self_: ReflectReference = self_.into(); let other: ScriptValue = other.into(); @@ -152,6 +158,7 @@ impl UserData for LuaReflectReference { m.add_meta_function( MetaMethod::Mod, |_, (self_, other): (LuaReflectReference, LuaScriptValue)| { + profiling::function_scope!("MetaMethod::Mod"); let world = ThreadWorldContainer.try_get_world()?; let self_: ReflectReference = self_.into(); let other: ScriptValue = other.into(); @@ -164,6 +171,7 @@ impl UserData for LuaReflectReference { ); m.add_meta_function(MetaMethod::Unm, |_, self_: LuaReflectReference| { + profiling::function_scope!("MetaMethod::Unm"); let world = ThreadWorldContainer.try_get_world()?; let self_: ReflectReference = self_.into(); let target_type_id = self_.tail_type_id(world.clone())?.or_fake_id(); @@ -175,6 +183,7 @@ impl UserData for LuaReflectReference { m.add_meta_function( MetaMethod::Pow, |_, (self_, other): (LuaReflectReference, LuaScriptValue)| { + profiling::function_scope!("MetaMethod::Pow"); let world = ThreadWorldContainer.try_get_world()?; let self_: ReflectReference = self_.into(); let other: ScriptValue = other.into(); @@ -189,6 +198,7 @@ impl UserData for LuaReflectReference { m.add_meta_function( MetaMethod::Eq, |_, (self_, other): (LuaReflectReference, LuaScriptValue)| { + profiling::function_scope!("MetaMethod::Eq"); let world = ThreadWorldContainer.try_get_world()?; let self_: ReflectReference = self_.into(); let other: ScriptValue = other.into(); @@ -203,6 +213,7 @@ impl UserData for LuaReflectReference { m.add_meta_function( MetaMethod::Lt, |_, (self_, other): (LuaReflectReference, LuaScriptValue)| { + profiling::function_scope!("MetaMethod::Lt"); let world = ThreadWorldContainer.try_get_world()?; let self_: ReflectReference = self_.into(); let other: ScriptValue = other.into(); @@ -215,6 +226,7 @@ impl UserData for LuaReflectReference { ); m.add_meta_function(MetaMethod::Len, |_lua, self_: LuaScriptValue| { + profiling::function_scope!("MetaMethod::Len"); let world = ThreadWorldContainer.try_get_world()?; let script_value: ScriptValue = self_.into(); Ok(match script_value { @@ -231,6 +243,7 @@ impl UserData for LuaReflectReference { feature = "luajit52", ))] m.add_meta_function(MetaMethod::Pairs, |_, s: LuaReflectReference| { + profiling::function_scope!("MetaMethod::Pairs"); // let mut iter_func = lookup_dynamic_function_typed::(l, "iter") // .expect("No iter function registered"); let world = ThreadWorldContainer.try_get_world()?; @@ -246,6 +259,7 @@ impl UserData for LuaReflectReference { }); m.add_meta_function(MetaMethod::ToString, |_, self_: LuaReflectReference| { + profiling::function_scope!("MetaMethod::ToString"); let world = ThreadWorldContainer.try_get_world()?; let reflect_reference: ReflectReference = self_.into(); @@ -273,6 +287,7 @@ impl UserData for LuaStaticReflectReference { m.add_meta_function( MetaMethod::Index, |_, (self_, key): (LuaStaticReflectReference, LuaScriptValue)| { + profiling::function_scope!("MetaMethod::Index"); let world = ThreadWorldContainer.try_get_world()?; let type_id = self_.0; diff --git a/crates/languages/bevy_mod_scripting_lua/src/bindings/script_value.rs b/crates/languages/bevy_mod_scripting_lua/src/bindings/script_value.rs index 8de5d1d668..97b935709d 100644 --- a/crates/languages/bevy_mod_scripting_lua/src/bindings/script_value.rs +++ b/crates/languages/bevy_mod_scripting_lua/src/bindings/script_value.rs @@ -36,7 +36,7 @@ impl From for ScriptValue { value.0 } } - +#[profiling::all_functions] impl FromLua for LuaScriptValue { fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result { Ok(match value { @@ -105,7 +105,7 @@ impl FromLua for LuaScriptValue { pub const LUA_CALLER_CONTEXT: FunctionCallContext = FunctionCallContext { convert_to_0_indexed: true, }; - +#[profiling::all_functions] impl IntoLua for LuaScriptValue { fn into_lua(self, lua: &mlua::Lua) -> mlua::Result { Ok(match self.0 { diff --git a/crates/languages/bevy_mod_scripting_lua/src/lib.rs b/crates/languages/bevy_mod_scripting_lua/src/lib.rs index 144274e58e..46679c103e 100644 --- a/crates/languages/bevy_mod_scripting_lua/src/lib.rs +++ b/crates/languages/bevy_mod_scripting_lua/src/lib.rs @@ -132,7 +132,7 @@ impl Default for LuaScriptingPlugin { } } } - +#[profiling::function] fn lua_language_mapper(path: &std::path::Path) -> Language { match path.extension().and_then(|ext| ext.to_str()) { Some("lua") => Language::Lua, @@ -145,7 +145,7 @@ impl Plugin for LuaScriptingPlugin { self.scripting_plugin.build(app); } } - +#[profiling::function] pub fn lua_context_load( script_id: &ScriptId, content: &[u8], @@ -173,7 +173,7 @@ pub fn lua_context_load( Ok(context) } - +#[profiling::function] pub fn lua_context_reload( script: &ScriptId, content: &[u8], @@ -193,6 +193,7 @@ pub fn lua_context_reload( } #[allow(clippy::too_many_arguments)] +#[profiling::function] pub fn lua_handler( args: Vec, entity: bevy::ecs::entity::Entity,