diff --git a/src/Collections/EcsGroup.cs b/src/Collections/EcsGroup.cs index 997b4ce..150cc47 100644 --- a/src/Collections/EcsGroup.cs +++ b/src/Collections/EcsGroup.cs @@ -196,10 +196,12 @@ namespace DCFApixels.DragonECS { if (_groupSparsePagePoolCount <= 0) { - var x = UnmanagedArrayUtility.NewAndInit(EcsGroup.PAGE_SIZE); - return x; + var newPage = UnmanagedArrayUtility.NewAndInit(EcsGroup.PAGE_SIZE); + return newPage; } - return _groupSparsePagePool[--_groupSparsePagePoolCount]; + var takedPage = _groupSparsePagePool[--_groupSparsePagePoolCount]; + _groupSparsePagePool[_groupSparsePagePoolCount] = null; + return takedPage; } internal void ReturnPage(int* page) { @@ -214,6 +216,16 @@ namespace DCFApixels.DragonECS } _groupSparsePagePool[_groupSparsePagePoolCount++] = page; } + private void DisposeGroupPages() + { + foreach (var page in _groupSparsePagePool) + { + if (page != null) + { + UnmanagedArrayUtility.Free(page); + } + } + } #endregion #region Groups Pool diff --git a/src/DebugUtils/MetaAttributes/EcsMetaAttribute.cs b/src/DebugUtils/MetaAttributes/EcsMetaAttribute.cs index b95af50..74118f5 100644 --- a/src/DebugUtils/MetaAttributes/EcsMetaAttribute.cs +++ b/src/DebugUtils/MetaAttributes/EcsMetaAttribute.cs @@ -7,13 +7,12 @@ namespace DCFApixels.DragonECS.Core { public abstract class EcsMetaAttribute : Attribute { } - - internal unsafe static class EcsMetaAttributeHalper + internal static class EcsMetaAttributeHalper { internal const string EMPTY_NO_SENSE_MESSAGE = "With empty parameters, this attribute makes no sense."; [ThreadStatic] private static string[] _splitBuffer; - public static string[] Split(char separator, string value) + public static unsafe string[] Split(char separator, string value) { if (_splitBuffer == null) { @@ -46,7 +45,7 @@ namespace DCFApixels.DragonECS.Core } #region SplitStream - private ref struct SplitStream + private unsafe ref struct SplitStream { public string current; public char* ptr; diff --git a/src/EcsAspect.cs b/src/EcsAspect.cs index c1dc6e1..bd4f5d5 100644 --- a/src/EcsAspect.cs +++ b/src/EcsAspect.cs @@ -198,7 +198,7 @@ namespace DCFApixels.DragonECS #region Constructors/New private Builder() { } - internal static unsafe (TAspect aspect, EcsMask mask) New(EcsWorld world) where TAspect : new() + internal static (TAspect aspect, EcsMask mask) New(EcsWorld world) where TAspect : new() { //Get Builder if (_constructorBuildersStack == null) diff --git a/src/EcsMask.cs b/src/EcsMask.cs index bc58ee4..4579813 100644 --- a/src/EcsMask.cs +++ b/src/EcsMask.cs @@ -758,7 +758,7 @@ namespace DCFApixels.DragonECS [Il2CppSetOption(Option.NullChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)] #endif - public unsafe ref struct Enumerator + public ref struct Enumerator { private ReadOnlySpan.Enumerator _span; @@ -784,7 +784,7 @@ namespace DCFApixels.DragonECS get { return _span.Current; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool MoveNext() + public unsafe bool MoveNext() { while (_span.MoveNext()) { diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 1810d02..025f6a3 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -236,6 +236,7 @@ namespace DCFApixels.DragonECS _isDestroyed = true; _poolTypeCode_2_CmpTypeIDs = null; _cmpTypeCode_2_CmpTypeIDs = null; + DisposeGroupPages(); foreach (var item in _executorCoures) { @@ -1054,6 +1055,11 @@ namespace DCFApixels.DragonECS list.Add(_pools[poolIdsPtr[i]]); } } + + if (count >= BUFFER_THRESHOLD) + { + UnmanagedArrayUtility.Free(poolIdsPtr); + } } public ReadOnlySpan GetComponentsFor(int entityID) { diff --git a/src/Utils/DependencyGraph.cs b/src/Utils/DependencyGraph.cs index c1bddb2..063a044 100644 --- a/src/Utils/DependencyGraph.cs +++ b/src/Utils/DependencyGraph.cs @@ -4,6 +4,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS.Core { @@ -70,7 +71,7 @@ namespace DCFApixels.DragonECS.Core - public unsafe class DependencyGraph : IDependencyGraph + public class DependencyGraph : IDependencyGraph { private readonly Dictionary _vertexIDs = new Dictionary(32); private StructList _vertexInfos = new StructList(32); @@ -226,7 +227,7 @@ namespace DCFApixels.DragonECS.Core #endregion #region Sort - public T[] Sort() + public unsafe T[] Sort() { const int BUFFER_THRESHOLD = 256; if (_count <= BUFFER_THRESHOLD) @@ -248,7 +249,7 @@ namespace DCFApixels.DragonECS.Core return ConvertIdsToTsArray(buffer); } } - private void TopoSorting(UnsafeArray sortingBuffer) + private unsafe void TopoSorting(UnsafeArray sortingBuffer) { VertexID[] nodes = new VertexID[_count]; var adjacency = new List<(VertexID To, int DependencyIndex)>[GetVertexInfosCount()]; @@ -336,7 +337,7 @@ namespace DCFApixels.DragonECS.Core throw new InvalidOperationException("Cyclic dependency detected." + details); } } - private void ReoderInsertionIndexes(UnsafeArray sortingBuffer) + private unsafe void ReoderInsertionIndexes(UnsafeArray sortingBuffer) { for (int i = 0; i < GetVertexInfosCount(); i++) { @@ -375,7 +376,7 @@ namespace DCFApixels.DragonECS.Core } } - private static void MoveElement(ref UnsafeArray array, int oldIndex, int newIndex) where TValue : unmanaged + private static unsafe void MoveElement(ref UnsafeArray array, int oldIndex, int newIndex) where TValue : unmanaged { if (oldIndex == newIndex) return; @@ -405,7 +406,7 @@ namespace DCFApixels.DragonECS.Core ptr[newIndex] = item; } - private T[] ConvertIdsToTsArray(UnsafeArray buffer) + private unsafe T[] ConvertIdsToTsArray(UnsafeArray buffer) { T[] result = new T[buffer.Length]; for (int i = 0; i < result.Length; i++)