From eb7f1836aac296c8b67daf99d533d94a21d892b3 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 27 Mar 2023 17:58:51 +0800 Subject: [PATCH] Add profile markers for builtin runners --- src/Builtin/DefaultRunners.cs | 104 ++++++++++++++++++++++++++++++++-- src/Debug/EcsDebug.cs | 14 +++-- 2 files changed, 108 insertions(+), 10 deletions(-) diff --git a/src/Builtin/DefaultRunners.cs b/src/Builtin/DefaultRunners.cs index df7ba22..9b371a5 100644 --- a/src/Builtin/DefaultRunners.cs +++ b/src/Builtin/DefaultRunners.cs @@ -21,30 +21,126 @@ public sealed class EcsPreInitRunner : EcsRunner, IEcsPreInitSystem { - void IEcsPreInitSystem.PreInit(EcsSystems systems) +#if DEBUG + private int[] _targetIds; +#endif + public void PreInit(EcsSystems systems) { +#if DEBUG + for (int i = 0; i < targets.Length; i++) + { + int id = _targetIds[i]; + EcsDebug.ProfileMarkBegin(id); + targets[i].PreInit(systems); + EcsDebug.ProfileMarkEnd(id); + } +#else foreach (var item in targets) item.PreInit(systems); +#endif } + +#if DEBUG + protected override void OnSetup() + { + _targetIds = new int[targets.Length]; + for (int i = 0; i < targets.Length; i++) + { + _targetIds[i] = EcsDebug.RegisterMark($"EcsRunner.{targets[i].GetType().Name}.{nameof(PreInit)}"); + } + } +#endif } public sealed class EcsInitRunner : EcsRunner, IEcsInitSystem { - void IEcsInitSystem.Init(EcsSystems systems) +#if DEBUG + private int[] _targetIds; +#endif + public void Init(EcsSystems systems) { +#if DEBUG + for (int i = 0; i < targets.Length; i++) + { + int id = _targetIds[i]; + EcsDebug.ProfileMarkBegin(id); + targets[i].Init(systems); + EcsDebug.ProfileMarkEnd(id); + } +#else foreach (var item in targets) item.Init(systems); +#endif } + +#if DEBUG + protected override void OnSetup() + { + _targetIds = new int[targets.Length]; + for (int i = 0; i < targets.Length; i++) + { + _targetIds[i] = EcsDebug.RegisterMark($"EcsRunner.{targets[i].GetType().Name}.{nameof(Init)}"); + } + } +#endif } public sealed class EcsRunRunner : EcsRunner, IEcsRunSystem { - void IEcsRunSystem.Run(EcsSystems systems) +#if DEBUG + private int[] _targetIds; +#endif + public void Run(EcsSystems systems) { +#if DEBUG + for (int i = 0; i < targets.Length; i++) + { + int id = _targetIds[i]; + EcsDebug.ProfileMarkBegin(id); + targets[i].Run(systems); + EcsDebug.ProfileMarkEnd(id); + } +#else foreach (var item in targets) item.Run(systems); +#endif } + +#if DEBUG + protected override void OnSetup() + { + _targetIds = new int[targets.Length]; + for (int i = 0; i < targets.Length; i++) + { + _targetIds[i] = EcsDebug.RegisterMark($"EcsRunner.{targets[i].GetType().Name}.{nameof(Run)}"); + } + } +#endif } public sealed class EcsDestroyRunner : EcsRunner, IEcsDestroySystem { - void IEcsDestroySystem.Destroy(EcsSystems systems) +#if DEBUG + private int[] _targetIds; +#endif + public void Destroy(EcsSystems systems) { +#if DEBUG + for (int i = 0; i < targets.Length; i++) + { + int id = _targetIds[i]; + EcsDebug.ProfileMarkBegin(id); + targets[i].Destroy(systems); + EcsDebug.ProfileMarkEnd(id); + } +#else foreach (var item in targets) item.Destroy(systems); +#endif } + +#if DEBUG + protected override void OnSetup() + { + _targetIds = new int[targets.Length]; + for (int i = 0; i < targets.Length; i++) + { + _targetIds[i] = EcsDebug.RegisterMark($"EcsRunner.{targets[i].GetType().Name}.{nameof(Destroy)}"); + } + } +#endif } } diff --git a/src/Debug/EcsDebug.cs b/src/Debug/EcsDebug.cs index 9abb76c..216a116 100644 --- a/src/Debug/EcsDebug.cs +++ b/src/Debug/EcsDebug.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; - +using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { @@ -11,13 +11,16 @@ namespace DCFApixels.DragonECS public static void Set(DebugService service) => DebugService.Set(service); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Print(object v) => DebugService.Instance.Print(v); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Print(string tag, object v) { #if !DISABLE_ECS_DEBUG DebugService.Instance.Print(tag, v); #endif } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int RegisterMark(string name) { #if !DISABLE_ECS_DEBUG @@ -26,18 +29,21 @@ namespace DCFApixels.DragonECS return 0; #endif } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void DeleteMark(string name) { #if !DISABLE_ECS_DEBUG DebugService.Instance.DeleteMark(name); #endif } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ProfileMarkBegin(int id) { #if !DISABLE_ECS_DEBUG DebugService.Instance.ProfileMarkBegin(id); #endif } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static double ProfileMarkEnd(int id) { #if !DISABLE_ECS_DEBUG @@ -70,18 +76,16 @@ namespace DCFApixels.DragonECS _instance = service; OnServiceChanged(_instance); } - public static Action OnServiceChanged = delegate { }; - private IntDispenser _idDispenser = new IntDispenser(0); private Dictionary _nameIdTable = new Dictionary(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Print(object v) => Print(null, v); public abstract void Print(string tag, object v); - public int RegisterMark(string name) { int id; @@ -107,8 +111,6 @@ namespace DCFApixels.DragonECS public abstract void ProfileMarkBegin(int id); public abstract double ProfileMarkEnd(int id); - - // public abstract IEnumerable<> } public sealed class DefaultDebugService : DebugService