simple refactoring

This commit is contained in:
Mikhail 2023-06-12 20:30:44 +08:00
parent e574e4a64d
commit 05bf563f76
2 changed files with 20 additions and 31 deletions

View File

@ -48,7 +48,7 @@ namespace DCFApixels.DragonECS
_markers = new EcsProfilerMarker[targets.Length]; _markers = new EcsProfilerMarker[targets.Length];
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
_markers[i] = new EcsProfilerMarker($"EcsRunner.{targets[i].GetType().Name}.{nameof(PreInit)}"); _markers[i] = new EcsProfilerMarker($"{targets[i].GetType().Name}.{nameof(PreInit)}");
} }
} }
#endif #endif
@ -77,7 +77,7 @@ namespace DCFApixels.DragonECS
_markers = new EcsProfilerMarker[targets.Length]; _markers = new EcsProfilerMarker[targets.Length];
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
_markers[i] = new EcsProfilerMarker($"EcsRunner.{targets[i].GetType().Name}.{nameof(Init)}"); _markers[i] = new EcsProfilerMarker($"{targets[i].GetType().Name}.{nameof(Init)}");
} }
} }
#endif #endif
@ -107,7 +107,7 @@ namespace DCFApixels.DragonECS
_markers = new EcsProfilerMarker[targets.Length]; _markers = new EcsProfilerMarker[targets.Length];
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
_markers[i] = new EcsProfilerMarker($"EcsRunner.{targets[i].GetType().Name}.{nameof(Run)}"); _markers[i] = new EcsProfilerMarker($"{targets[i].GetType().Name}.{nameof(Run)}");
} }
} }
#endif #endif
@ -136,7 +136,7 @@ namespace DCFApixels.DragonECS
_markers = new EcsProfilerMarker[targets.Length]; _markers = new EcsProfilerMarker[targets.Length];
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
_markers[i] = new EcsProfilerMarker($"EcsRunner.{targets[i].GetType().Name}.{nameof(Destroy)}"); _markers[i] = new EcsProfilerMarker($"{targets[i].GetType().Name}.{nameof(Destroy)}");
} }
} }
#endif #endif

View File

@ -12,9 +12,9 @@ namespace DCFApixels.DragonECS
internal EcsProfilerMarker(int id) => this.id = id; internal EcsProfilerMarker(int id) => this.id = id;
public EcsProfilerMarker(string name) => id = DebugService.Instance.RegisterMark(name); public EcsProfilerMarker(string name) => id = DebugService.Instance.RegisterMark(name);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Begin() => DebugService.Instance.ProfileMarkBegin(id); public void Begin() => DebugService.Instance.ProfilerMarkBegin(id);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void End() => DebugService.Instance.ProfileMarkEnd(id); public void End() => DebugService.Instance.ProfilerMarkEnd(id);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public AutoScope Auto() => new AutoScope(id); public AutoScope Auto() => new AutoScope(id);
public readonly ref struct AutoScope public readonly ref struct AutoScope
@ -23,9 +23,9 @@ namespace DCFApixels.DragonECS
public AutoScope(int id) public AutoScope(int id)
{ {
_id = id; _id = id;
DebugService.Instance.ProfileMarkBegin(id); DebugService.Instance.ProfilerMarkBegin(id);
} }
public void Dispose() => DebugService.Instance.ProfileMarkEnd(_id); public void Dispose() => DebugService.Instance.ProfilerMarkEnd(_id);
} }
} }
@ -55,18 +55,7 @@ namespace DCFApixels.DragonECS
public abstract class DebugService public abstract class DebugService
{ {
private static DebugService _instance; private static DebugService _instance;
public static DebugService Instance => _instance ??= new DefaultDebugService();
public static DebugService Instance
{
get
{
if (_instance == null)
{
_instance = new DefaultDebugService();
}
return _instance;
}
}
public static void Set<T>() where T : DebugService, new() => Set(new T()); public static void Set<T>() where T : DebugService, new() => Set(new T());
public static void Set(DebugService service) public static void Set(DebugService service)
@ -92,7 +81,7 @@ namespace DCFApixels.DragonECS
id = _idDispenser.GetFree(); id = _idDispenser.GetFree();
_nameIdTable.Add(name, id); _nameIdTable.Add(name, id);
} }
OnNewMark(id, name); OnNewProfilerMark(id, name);
return id; return id;
} }
public void DeleteMark(string name) public void DeleteMark(string name)
@ -100,15 +89,15 @@ namespace DCFApixels.DragonECS
int id = _nameIdTable[name]; int id = _nameIdTable[name];
_nameIdTable.Remove(name); _nameIdTable.Remove(name);
_idDispenser.Release(id); _idDispenser.Release(id);
OnDelMark(id); OnDelProfilerMark(id);
} }
protected abstract void OnNewMark(int id, string name); protected abstract void OnNewProfilerMark(int id, string name);
protected abstract void OnDelMark(int id); protected abstract void OnDelProfilerMark(int id);
public abstract void ProfileMarkBegin(int id); public abstract void ProfilerMarkBegin(int id);
public abstract void ProfileMarkEnd(int id); public abstract void ProfilerMarkEnd(int id);
} }
public sealed class DefaultDebugService : DebugService public sealed class DefaultDebugService : DebugService
@ -126,22 +115,22 @@ namespace DCFApixels.DragonECS
{ {
Console.WriteLine($"[{tag}] {v}"); Console.WriteLine($"[{tag}] {v}");
} }
public override void ProfileMarkBegin(int id) public override void ProfilerMarkBegin(int id)
{ {
_stopwatchs[id].Start(); _stopwatchs[id].Start();
} }
public override void ProfileMarkEnd(int id) public override void ProfilerMarkEnd(int id)
{ {
_stopwatchs[id].Stop(); _stopwatchs[id].Stop();
var time = _stopwatchs[id].Elapsed; var time = _stopwatchs[id].Elapsed;
_stopwatchs[id].Reset(); _stopwatchs[id].Reset();
Print(_stopwatchsNames[id] + " s:" + time.TotalSeconds); Print("ProfilerMark", _stopwatchsNames[id] + " s:" + time.TotalSeconds);
} }
protected override void OnDelMark(int id) protected override void OnDelProfilerMark(int id)
{ {
_stopwatchs[id] = null; _stopwatchs[id] = null;
} }
protected override void OnNewMark(int id, string name) protected override void OnNewProfilerMark(int id, string name)
{ {
if (id >= _stopwatchs.Length) if (id >= _stopwatchs.Length)
{ {