From 615d2014241dcdc56dd245c48f127f8e3be9fe16 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 24 Mar 2025 14:26:40 +0800 Subject: [PATCH] refactoring --- src/DebugUtils/EcsDebug.cs | 48 ++++++++++++++++++++---------- src/DebugUtils/TypeMeta.cs | 4 +-- src/EcsAspect.cs | 2 +- src/EcsPipeline.cs | 14 ++++----- src/EcsWorld.cs | 16 +++------- src/EcsWorld.static.cs | 9 +++--- src/Internal/ArrayUtility.cs | 33 ++++++++++++++++++++ src/Internal/EcsTypeCodeManager.cs | 2 +- src/Internal/UnsafeArray.cs | 1 + 9 files changed, 86 insertions(+), 43 deletions(-) diff --git a/src/DebugUtils/EcsDebug.cs b/src/DebugUtils/EcsDebug.cs index fc34c38..0f5608e 100644 --- a/src/DebugUtils/EcsDebug.cs +++ b/src/DebugUtils/EcsDebug.cs @@ -1,16 +1,18 @@ #if DISABLE_DEBUG #undef DEBUG #endif +using DCFApixels.DragonECS.Core; using DCFApixels.DragonECS.Internal; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; +using static DCFApixels.DragonECS.EcsConsts; namespace DCFApixels.DragonECS { - using static EcsConsts; + #region EcsProfilerMarker public readonly struct EcsProfilerMarker { #if DEBUG || DRAGONECS_ENABLE_DEBUG_SERVICE @@ -75,6 +77,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public static explicit operator EcsProfilerMarker(string markerName) { return new EcsProfilerMarker(markerName); } } + #endregion [MetaColor(MetaColor.DragonRose)] [MetaGroup(PACK_GROUP, DEBUG_GROUP)] @@ -82,6 +85,7 @@ namespace DCFApixels.DragonECS [MetaID("DragonECS_10A4587C92013B55820D8604D718A1C3")] public static class EcsDebug { + #region Set public static void Set() where T : DebugService, new() { DebugService.Set(); @@ -90,7 +94,9 @@ namespace DCFApixels.DragonECS { DebugService.Set(service); } + #endregion + #region Print #if UNITY_2021_3_OR_NEWER [UnityEngine.HideInCallstack] #endif @@ -168,6 +174,9 @@ namespace DCFApixels.DragonECS DebugService.CurrentThreadInstance.Print(tag, v); #endif } + #endregion + + #region Other [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Break() { @@ -175,9 +184,12 @@ namespace DCFApixels.DragonECS DebugService.CurrentThreadInstance.Break(); #endif } + #endregion + #region Events public static OnPrintHandler OnPrint = delegate { }; public delegate void OnPrintHandler(string tag, object v); + #endregion } //------------------------------------------------------------------------------------------------------------// @@ -185,19 +197,23 @@ namespace DCFApixels.DragonECS public abstract class DebugService { private static DebugService _instance; - private static object _lock = new object(); + private readonly static object _lock = new object(); - private static HashSet _threadServiceClonesSet = new HashSet(); + private readonly static HashSet _threadServiceClonesSet = new HashSet(); [ThreadStatic] private static DebugService _currentThreadInstanceClone; [ThreadStatic] private static DebugService _currentThreadInstance; // для сравнения - private static IdDispenser _idDispenser = new IdDispenser(16, 0); - private static Dictionary _nameIdTable = new Dictionary(); + private readonly static IdDispenser _idDispenser = new IdDispenser(16, 0); + private readonly static Dictionary _nameIdTable = new Dictionary(); #region Properties + public static bool IsNullOrDefault + { + get { return _instance == null || _instance is NullDebugService || _instance is DefaultDebugService; } + } public static DebugService Instance { get { return _instance; } @@ -231,7 +247,11 @@ namespace DCFApixels.DragonECS #region Static Constructor static DebugService() { +#if !UNITY_5_3_OR_NEWER + Set(new NullDebugService()); +#else Set(new DefaultDebugService()); +#endif } #endregion @@ -357,6 +377,11 @@ namespace DCFApixels.DragonECS public delegate void OnServiceChangedHandler(DebugService service); #endregion } +} + +namespace DCFApixels.DragonECS.Core +{ + #region DebugServiceExtensions public static class DebugServiceExtensions { public static void PrintWarning(this DebugService self, object v) @@ -386,7 +411,9 @@ namespace DCFApixels.DragonECS } //TODO PrintJson возможно будет добавлено когда-то } + #endregion + #region DefaultServices //------------------------------------------------------------------------------------------------------------// public sealed class NullDebugService : DebugService @@ -404,7 +431,6 @@ namespace DCFApixels.DragonECS public sealed class DefaultDebugService : DebugService { -#if !UNITY_5_3_OR_NEWER private const string PROFILER_MARKER = "ProfilerMark"; private const string PROFILER_MARKER_CACHE = "[" + PROFILER_MARKER + "] "; @@ -573,14 +599,6 @@ namespace DCFApixels.DragonECS return this.AutoToString(); } } -#else - protected sealed override DebugService CreateThreadInstance() { return this; } - public sealed override void Break() { } - public sealed override void Print(string tag, object v) { } - public sealed override void ProfilerMarkBegin(int id) { } - public sealed override void ProfilerMarkEnd(int id) { } - protected sealed override void OnDelProfilerMark(int id) { } - protected sealed override void OnNewProfilerMark(int id, string name) { } -#endif } + #endregion } \ No newline at end of file diff --git a/src/DebugUtils/TypeMeta.cs b/src/DebugUtils/TypeMeta.cs index ca26c08..8eceb19 100644 --- a/src/DebugUtils/TypeMeta.cs +++ b/src/DebugUtils/TypeMeta.cs @@ -45,7 +45,7 @@ namespace DCFApixels.DragonECS private const string NULL_NAME = "NULL"; public static readonly TypeMeta NullTypeMeta; - private static object _lock = new object(); + private static readonly object _lock = new object(); private static readonly Dictionary _metaCache = new Dictionary(); private static int _increment = 1; @@ -84,7 +84,7 @@ namespace DCFApixels.DragonECS _name = NULL_NAME, _typeName = NULL_NAME, - _color = new MetaColor(MetaColor.Black), + _color = MetaColor.Black, _description = new MetaDescription("", NULL_NAME), _group = MetaGroup.Empty, _tags = Array.Empty(), diff --git a/src/EcsAspect.cs b/src/EcsAspect.cs index 13f38e4..c1dc6e1 100644 --- a/src/EcsAspect.cs +++ b/src/EcsAspect.cs @@ -125,7 +125,7 @@ namespace DCFApixels.DragonECS #endregion //Инициализация аспектов проходит в синхронизированном состоянии, поэтому использование _staticMaskCache потоко безопасно. - private static Dictionary _staticMaskCache = new Dictionary(); + private readonly static Dictionary _staticMaskCache = new Dictionary(); internal EcsWorld _source; internal EcsMask _mask; diff --git a/src/EcsPipeline.cs b/src/EcsPipeline.cs index ca8af5f..0bebeec 100644 --- a/src/EcsPipeline.cs +++ b/src/EcsPipeline.cs @@ -114,20 +114,18 @@ namespace DCFApixels.DragonECS private static IEcsProcess[] _buffer; private T[] CreateProcess() where T : IEcsProcess { - if (_buffer == null || _buffer.Length < _allSystems.Length) - { - Array.Resize(ref _buffer, _allSystems.Length); - } - int l = 0; + ArrayUtility.UpsizeWithoutCopy(ref _buffer, _allSystems.Length); + int bufferLength = 0; for (int i = 0, iMax = _allSystems.Length; i < iMax; i++) { if (_allSystems[i] is T) { - _buffer[l++] = _allSystems[i]; + _buffer[bufferLength++] = _allSystems[i]; } } - T[] result = new T[l]; - Array.Copy(_buffer, result, l); + T[] result = new T[bufferLength]; + Array.Copy(_buffer, result, bufferLength); + Array.Clear(_buffer, 0, bufferLength); return result; } #endregion diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 9c5d932..8965082 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -955,10 +955,8 @@ namespace DCFApixels.DragonECS public ReadOnlySpan GetComponentsFor(int entityID) { int count = GetComponentTypeIDsFor_Internal(entityID, ref _componentIDsBuffer); - if (_componentsBuffer == null || _componentsBuffer.Length < count) - { - _componentsBuffer = new object[count]; - } + ArrayUtility.UpsizeWithoutCopy(ref _componentIDsBuffer, count); + for (int i = 0; i < count; i++) { _componentsBuffer[i] = _pools[_componentIDsBuffer[i]].GetRaw(entityID); @@ -986,14 +984,7 @@ namespace DCFApixels.DragonECS private unsafe int GetComponentTypeIDsFor_Internal(int entityID, ref int[] componentIDs) { var itemsCount = GetComponentsCount(entityID); - if (componentIDs == null) - { - componentIDs = new int[itemsCount]; - } - if (componentIDs.Length < itemsCount) - { - Array.Resize(ref componentIDs, itemsCount); - } + ArrayUtility.UpsizeWithoutCopy(ref componentIDs, itemsCount); if (itemsCount <= 0) { return 0; } @@ -1170,6 +1161,7 @@ namespace DCFApixels.DragonECS public long Version { get { return _world.Version; } } public IEcsPool[] Pools { get { return _world._pools; } } public short ID { get { return _world.ID; } } + public bool IsDestroyed { get { return _world._isDestroyed; } } public List MaskQueries { get { return _queries; } } public DebuggerProxy(EcsWorld world) { diff --git a/src/EcsWorld.static.cs b/src/EcsWorld.static.cs index cdfe30c..c87882d 100644 --- a/src/EcsWorld.static.cs +++ b/src/EcsWorld.static.cs @@ -26,11 +26,11 @@ namespace DCFApixels.DragonECS private static EcsWorld[] _worlds = Array.Empty(); private static readonly IdDispenser _worldIdDispenser = new IdDispenser(4, 0, n => Array.Resize(ref _worlds, n)); - private static StructList _allWorldComponentPools = new StructList(64); + private static readonly object _worldLock = new object(); + private StructList _worldComponentPools; private int _builtinWorldComponentsCount = 0; - private static readonly object _worldLock = new object(); static EcsWorld() { @@ -92,6 +92,7 @@ namespace DCFApixels.DragonECS } world = null; } + _worlds = Array.Empty(); _worldIdDispenser.ReleaseAll(); } @@ -137,8 +138,8 @@ namespace DCFApixels.DragonECS private static short _count; private static short[] _recycledItems = new short[4]; private static short _recycledItemsCount; - private static IEcsWorldComponent _interface = EcsWorldComponentHandler.instance; - private static Abstract _controller = new Abstract(); + private static readonly IEcsWorldComponent _interface = EcsWorldComponentHandler.instance; + private static readonly Abstract _controller = new Abstract(); static WorldComponentPool() { _allWorldComponentPools.Add(_controller); diff --git a/src/Internal/ArrayUtility.cs b/src/Internal/ArrayUtility.cs index 33dc2a0..16cfb46 100644 --- a/src/Internal/ArrayUtility.cs +++ b/src/Internal/ArrayUtility.cs @@ -134,6 +134,39 @@ namespace DCFApixels.DragonECS.Internal array[i] = value; } } + + + public static void UpsizeWithoutCopy(ref T[] array, int minSize) + { + if (array == null || minSize > array.Length) + { + array = new T[minSize]; + } + } + public static void Upsize(ref T[] array, int minSize) + { + if (array == null) + { + array = new T[minSize]; + } + else if (minSize > array.Length) + { + Array.Resize(ref array, minSize); + } + } + public static void UpsizeToNextPow2(ref T[] array, int minSize) + { + if (array == null) + { + minSize = NextPow2(minSize); + array = new T[minSize]; + } + else if (minSize > array.Length) + { + minSize = NextPow2(minSize); + Array.Resize(ref array, minSize); + } + } } internal readonly struct EnumerableInt : IEnumerable { diff --git a/src/Internal/EcsTypeCodeManager.cs b/src/Internal/EcsTypeCodeManager.cs index d6ec595..1bafb05 100644 --- a/src/Internal/EcsTypeCodeManager.cs +++ b/src/Internal/EcsTypeCodeManager.cs @@ -22,7 +22,7 @@ namespace DCFApixels.DragonECS.Internal { private static readonly Dictionary _codes = new Dictionary(); private static int _increment = 1; - private static object _lock = new object(); + private static readonly object _lock = new object(); public static int Count { [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/Internal/UnsafeArray.cs b/src/Internal/UnsafeArray.cs index ad8e385..9f00cbe 100644 --- a/src/Internal/UnsafeArray.cs +++ b/src/Internal/UnsafeArray.cs @@ -44,6 +44,7 @@ namespace DCFApixels.DragonECS.Internal } } } + #if ENABLE_IL2CPP [Il2CppSetOption(Option.NullChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)]