Skip to content

Commit 9629224

Browse files
gallexmemakspll
andauthored
feat: Profiling First Draft (#232)
* feat: profiling first draft * run `cargo fmt` to trigger CI --------- Co-authored-by: makspll <makspl17@gmail.com>
1 parent 3ad3fcf commit 9629224

File tree

21 files changed

+118
-47
lines changed

21 files changed

+118
-47
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mlua_serialize = ["bevy_mod_scripting_lua?/mlua_serialize"]
4343
mlua_macros = ["bevy_mod_scripting_lua?/mlua_macros"]
4444
mlua_async = ["bevy_mod_scripting_lua?/mlua_async"]
4545

46+
4647
## rhai
4748
rhai = ["bevy_mod_scripting_rhai"]
4849

@@ -56,8 +57,8 @@ bevy_mod_scripting_lua = { path = "crates/languages/bevy_mod_scripting_lua", ver
5657
bevy_mod_scripting_rhai = { path = "crates/languages/bevy_mod_scripting_rhai", version = "0.9.0-alpha.9", optional = true }
5758
# bevy_mod_scripting_rune = { path = "crates/languages/bevy_mod_scripting_rune", version = "0.9.0-alpha.2", optional = true }
5859
bevy_mod_scripting_functions = { workspace = true }
59-
6060
[workspace.dependencies]
61+
profiling = {version = "1.0" }
6162
bevy = { version = "0.15.0", default-features = false }
6263
bevy_mod_scripting_core = { path = "crates/bevy_mod_scripting_core", version = "0.9.0-alpha.9" }
6364
bevy_mod_scripting_functions = { path = "crates/bevy_mod_scripting_functions", version = "0.9.0-alpha.9", default-features = false }

crates/bevy_mod_scripting_core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ dashmap = "6"
4040
smallvec = "1.11"
4141
itertools = "0.13"
4242
derivative = "2.2"
43-
43+
profiling = {workspace = true}
4444
[dev-dependencies]
4545
test_utils = { workspace = true }
4646

crates/bevy_mod_scripting_core/src/bindings/access_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ pub struct AccessMap {
215215
individual_accesses: DashMap<u64, AccessCount>,
216216
global_lock: RwLock<AccessCount>,
217217
}
218-
218+
#[profiling::all_functions]
219219
impl AccessMap {
220220
pub fn is_locked_exclusively(&self) -> bool {
221221
let global_lock = self.global_lock.read();

crates/bevy_mod_scripting_core/src/bindings/allocator.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub struct ReflectAllocator {
141141
allocations: HashMap<ReflectAllocationId, ReflectAllocation>,
142142
types: HashMap<u64, TypeId>,
143143
}
144-
144+
#[profiling::all_functions]
145145
impl ReflectAllocator {
146146
/// Allocates a new [`Reflect`] value and returns an [`AllocationId`] which can be used to access it later.
147147
/// Use [`Self::allocate_boxed`] if you already have an allocated boxed value.
@@ -206,6 +206,7 @@ impl ReflectAllocator {
206206
}
207207

208208
/// Cleans up dangling script allocations
209+
#[profiling::function]
209210
pub fn garbage_collector(allocator: ResMut<AppReflectAllocator>) {
210211
let mut allocator = allocator.write();
211212
allocator.clean_garbage_allocations()

crates/bevy_mod_scripting_core/src/bindings/function/from.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl FromScript for () {
4343

4444
impl FromScript for bool {
4545
type This<'w> = Self;
46-
46+
#[profiling::function]
4747
fn from_script(
4848
value: ScriptValue,
4949
world: WorldGuard<'_>,
@@ -67,6 +67,7 @@ macro_rules! impl_from_with_downcast {
6767
$(
6868
impl FromScript for $ty {
6969
type This<'w> = Self;
70+
#[profiling::function]
7071
fn from_script(value: ScriptValue, world: WorldGuard) -> Result<Self, InteropError> {
7172
match value {
7273
ScriptValue::Integer(i) => Ok(i as $ty),
@@ -88,6 +89,7 @@ macro_rules! impl_from_stringlike {
8889
$(
8990
impl FromScript for $ty {
9091
type This<'w> = Self;
92+
#[profiling::function]
9193
fn from_script(value: ScriptValue, world: WorldGuard) -> Result<Self, InteropError> {
9294
match value {
9395
ScriptValue::String(s) => Ok(s.to_string().into()),
@@ -104,7 +106,7 @@ impl_from_stringlike!(String, PathBuf, OsString);
104106

105107
impl FromScript for char {
106108
type This<'w> = Self;
107-
109+
#[profiling::function]
108110
fn from_script(
109111
value: ScriptValue,
110112
world: WorldGuard<'_>,
@@ -128,6 +130,7 @@ impl FromScript for char {
128130

129131
impl FromScript for ReflectReference {
130132
type This<'w> = Self;
133+
#[profiling::function]
131134
fn from_script(value: ScriptValue, _world: WorldGuard) -> Result<Self, InteropError> {
132135
match value {
133136
ScriptValue::Reference(r) => Ok(r),
@@ -178,6 +181,7 @@ impl<T> From<T> for Val<T> {
178181

179182
impl<T: FromReflect> FromScript for Val<T> {
180183
type This<'w> = Self;
184+
#[profiling::function]
181185
fn from_script(value: ScriptValue, world: WorldGuard) -> Result<Self, InteropError> {
182186
match value {
183187
ScriptValue::Reference(reflect_reference) => Ok(Val(reflect_reference.with_reflect(
@@ -221,7 +225,7 @@ impl<T> Deref for Ref<'_, T> {
221225

222226
impl<T: FromReflect> FromScript for Ref<'_, T> {
223227
type This<'a> = Ref<'a, T>;
224-
228+
#[profiling::function]
225229
fn from_script(
226230
value: ScriptValue,
227231
world: WorldGuard<'_>,
@@ -293,7 +297,7 @@ impl<'a, T> From<&'a mut T> for Mut<'a, T> {
293297

294298
impl<T: FromReflect> FromScript for Mut<'_, T> {
295299
type This<'w> = Mut<'w, T>;
296-
300+
#[profiling::function]
297301
fn from_script(
298302
value: ScriptValue,
299303
world: WorldGuard<'_>,
@@ -331,7 +335,7 @@ where
331335
for<'w> T::This<'w>: Into<T>,
332336
{
333337
type This<'w> = Self;
334-
338+
#[profiling::function]
335339
fn from_script(value: ScriptValue, world: WorldGuard) -> Result<Self, InteropError> {
336340
match value {
337341
ScriptValue::Unit => Ok(None),
@@ -345,7 +349,7 @@ where
345349
for<'w> T::This<'w>: Into<T>,
346350
{
347351
type This<'w> = Self;
348-
352+
#[profiling::function]
349353
fn from_script(value: ScriptValue, world: WorldGuard) -> Result<Self, InteropError> {
350354
match value {
351355
ScriptValue::List(list) => {
@@ -368,7 +372,7 @@ where
368372
for<'w> T::This<'w>: Into<T>,
369373
{
370374
type This<'w> = Self;
371-
375+
#[profiling::function]
372376
fn from_script(value: ScriptValue, world: WorldGuard) -> Result<Self, InteropError> {
373377
match value {
374378
ScriptValue::List(list) if list.len() == N => {
@@ -390,7 +394,7 @@ where
390394

391395
impl FromScript for DynamicScriptFunctionMut {
392396
type This<'w> = Self;
393-
397+
#[profiling::function]
394398
fn from_script(value: ScriptValue, _: WorldGuard<'_>) -> Result<Self::This<'_>, InteropError>
395399
where
396400
Self: Sized,
@@ -407,7 +411,7 @@ impl FromScript for DynamicScriptFunctionMut {
407411

408412
impl FromScript for DynamicScriptFunction {
409413
type This<'w> = Self;
410-
414+
#[profiling::function]
411415
fn from_script(value: ScriptValue, _: WorldGuard<'_>) -> Result<Self::This<'_>, InteropError>
412416
where
413417
Self: Sized,
@@ -428,7 +432,7 @@ where
428432
for<'w> V::This<'w>: Into<V>,
429433
{
430434
type This<'w> = Self;
431-
435+
#[profiling::function]
432436
fn from_script(value: ScriptValue, world: WorldGuard) -> Result<Self, InteropError> {
433437
match value {
434438
ScriptValue::Map(map) => {

crates/bevy_mod_scripting_core/src/bindings/function/from_ref.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub trait FromScriptRef {
2424
}
2525

2626
impl FromScriptRef for Box<dyn PartialReflect> {
27+
#[profiling::function]
2728
fn from_script_ref(
2829
target: TypeId,
2930
value: ScriptValue,

crates/bevy_mod_scripting_core/src/bindings/function/into_ref.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ macro_rules! downcast_into_value {
5353
}
5454

5555
impl IntoScriptRef for ReflectReference {
56+
#[profiling::function]
5657
fn into_script_ref(
5758
self_: ReflectReference,
5859
world: WorldGuard,
5960
) -> Result<ScriptValue, InteropError> {
6061
self_.with_reflect(world.clone(), |r| into_script_ref(self_.clone(), r, world))?
6162
}
6263
}
63-
64+
#[profiling::function]
6465
fn into_script_ref(
6566
mut self_: ReflectReference,
6667
r: &dyn PartialReflect,

crates/bevy_mod_scripting_core/src/bindings/function/script_function.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl DynamicScriptFunction {
100100
args: I,
101101
context: FunctionCallContext,
102102
) -> Result<ScriptValue, InteropError> {
103+
profiling::scope!("Dynamic Call ", self.name().clone());
103104
let args = args.into_iter().collect::<VecDeque<_>>();
104105
// should we be inlining call errors into the return value?
105106
let return_val = (self.func)(context, args);
@@ -151,6 +152,7 @@ impl DynamicScriptFunctionMut {
151152
args: I,
152153
context: FunctionCallContext,
153154
) -> Result<ScriptValue, InteropError> {
155+
profiling::scope!("Dynamic Call Mut", self.name().clone());
154156
let args = args.into_iter().collect::<VecDeque<_>>();
155157
// should we be inlining call errors into the return value?
156158
let mut write = self.func.write();
@@ -276,7 +278,7 @@ pub struct FunctionKey {
276278
pub struct ScriptFunctionRegistry {
277279
functions: HashMap<FunctionKey, DynamicScriptFunction>,
278280
}
279-
281+
#[profiling::all_functions]
280282
impl ScriptFunctionRegistry {
281283
/// Register a script function with the given name. If the name already exists,
282284
/// the new function will be registered as an overload of the function.
@@ -525,7 +527,9 @@ macro_rules! impl_script_function {
525527
$( $param::This<'env>: Into<$param>,)*
526528
{
527529
#[allow(unused_mut,unused_variables)]
530+
#[profiling::function]
528531
fn $trait_fn_name(mut self) -> $dynamic_type {
532+
529533
let func = (move |caller_context: FunctionCallContext, mut args: VecDeque<ScriptValue> | {
530534
let res: Result<ScriptValue, InteropError> = (|| {
531535
let received_args_len = args.len();

crates/bevy_mod_scripting_core/src/bindings/pretty_print.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ pub trait DisplayWithWorld: std::fmt::Debug {
342342
self.display_with_world(world)
343343
}
344344
}
345-
345+
#[profiling::all_functions]
346346
impl DisplayWithWorld for ReflectReference {
347347
fn display_with_world(&self, world: WorldGuard) -> String {
348348
ReflectReferencePrinter::new(self.clone()).pretty_print(Some(world))
@@ -356,7 +356,7 @@ impl DisplayWithWorld for ReflectReference {
356356
ReflectReferencePrinter::new(self.clone()).pretty_print(None)
357357
}
358358
}
359-
359+
#[profiling::all_functions]
360360
impl DisplayWithWorld for ReflectBaseType {
361361
fn display_with_world(&self, world: WorldGuard) -> String {
362362
let mut string = String::new();
@@ -374,7 +374,7 @@ impl DisplayWithWorld for ReflectBaseType {
374374
string
375375
}
376376
}
377-
377+
#[profiling::all_functions]
378378
impl DisplayWithWorld for ComponentId {
379379
fn display_without_world(&self) -> String {
380380
format!("ComponentOrResource({})", self.index())
@@ -393,7 +393,7 @@ impl DisplayWithWorld for ComponentId {
393393
}
394394
}
395395
}
396-
396+
#[profiling::all_functions]
397397
impl DisplayWithWorld for ReflectAccessId {
398398
fn display_without_world(&self) -> String {
399399
match self.kind {
@@ -425,7 +425,7 @@ impl DisplayWithWorld for ReflectAccessId {
425425
}
426426
}
427427
}
428-
428+
#[profiling::all_functions]
429429
impl DisplayWithWorld for TypeId {
430430
fn display_with_world(&self, world: WorldGuard) -> String {
431431
if *self == TypeId::of::<FakeType>() {
@@ -455,7 +455,7 @@ impl DisplayWithWorld for TypeId {
455455
format!("{:?}", self)
456456
}
457457
}
458-
458+
#[profiling::all_functions]
459459
impl DisplayWithWorld for ScriptValue {
460460
fn display_with_world(&self, world: WorldGuard) -> String {
461461
match self {
@@ -509,7 +509,7 @@ impl DisplayWithWorld for ScriptValue {
509509
}
510510
}
511511
}
512-
512+
#[profiling::all_functions]
513513
impl<T: DisplayWithWorld> DisplayWithWorld for Vec<T> {
514514
fn display_with_world(&self, world: WorldGuard) -> String {
515515
let mut string = String::new();
@@ -550,7 +550,7 @@ impl<T: DisplayWithWorld> DisplayWithWorld for Vec<T> {
550550
string
551551
}
552552
}
553-
553+
#[profiling::all_functions]
554554
impl DisplayWithWorld for String {
555555
fn display_with_world(&self, _world: WorldGuard) -> String {
556556
self.to_string()
@@ -564,7 +564,7 @@ impl DisplayWithWorld for String {
564564
self.to_string()
565565
}
566566
}
567-
567+
#[profiling::all_functions]
568568
impl<K: DisplayWithWorld, V: DisplayWithWorld> DisplayWithWorld
569569
for std::collections::HashMap<K, V>
570570
{

crates/bevy_mod_scripting_core/src/bindings/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub struct ScriptQueryResult {
161161
// self.try_read().and_then(|world| world.query(query))
162162
// }
163163
// }
164-
164+
#[profiling::all_functions]
165165
impl WorldAccessGuard<'_> {
166166
pub fn query(
167167
&self,

crates/bevy_mod_scripting_core/src/bindings/reference.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub enum TypeIdSource {
5858
/// Givent the Tail reference is a container type, use the type id of the keys of the container
5959
Key,
6060
}
61-
61+
#[profiling::all_functions]
6262
impl ReflectReference {
6363
/// Creates a new infinite iterator. This iterator will keep returning the next element reference forever.
6464
pub fn into_iter_infinite(self) -> ReflectRefIter {
@@ -421,7 +421,7 @@ pub enum ReflectBase {
421421
Resource(ComponentId),
422422
Owned(ReflectAllocationId),
423423
}
424-
424+
#[profiling::all_functions]
425425
impl ReflectBase {
426426
/// Retrieves the pointer to the underlying `dyn PartialReflect` object valid for the 'w lifteime of the world cell
427427
///
@@ -469,7 +469,7 @@ pub trait ReflectionPathExt {
469469

470470
fn iter(&self) -> impl Iterator<Item = &bevy::reflect::OffsetAccess>;
471471
}
472-
472+
#[profiling::all_functions]
473473
impl ReflectionPathExt for ParsedPath {
474474
/// Assumes the accesses are 1 indexed and converts them to 0 indexed
475475
fn convert_to_0_indexed(&mut self) {
@@ -505,7 +505,7 @@ pub struct ReflectRefIter {
505505
pub enum IterationKey {
506506
Index(usize),
507507
}
508-
508+
#[profiling::all_functions]
509509
impl ReflectRefIter {
510510
pub fn new_indexed(base: ReflectReference) -> Self {
511511
Self {
@@ -537,7 +537,7 @@ impl ReflectRefIter {
537537
const fn list_index_access(index: usize) -> bevy::reflect::Access<'static> {
538538
bevy::reflect::Access::ListIndex(index)
539539
}
540-
540+
#[profiling::all_functions]
541541
impl Iterator for ReflectRefIter {
542542
type Item = Result<ReflectReference, InteropError>;
543543

crates/bevy_mod_scripting_core/src/bindings/script_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl From<HashMap<String, ScriptValue>> for ScriptValue {
147147
ScriptValue::Map(value)
148148
}
149149
}
150-
150+
#[profiling::all_functions]
151151
impl TryFrom<ScriptValue> for ParsedPath {
152152
type Error = InteropError;
153153
fn try_from(value: ScriptValue) -> Result<Self, Self::Error> {

0 commit comments

Comments
 (0)