fix leaks/refactoring

This commit is contained in:
DCFApixels 2025-05-15 20:41:02 +08:00
parent cf31891a97
commit 73bb779a5a
6 changed files with 34 additions and 16 deletions

View File

@ -196,10 +196,12 @@ namespace DCFApixels.DragonECS
{ {
if (_groupSparsePagePoolCount <= 0) if (_groupSparsePagePoolCount <= 0)
{ {
var x = UnmanagedArrayUtility.NewAndInit<int>(EcsGroup.PAGE_SIZE); var newPage = UnmanagedArrayUtility.NewAndInit<int>(EcsGroup.PAGE_SIZE);
return x; return newPage;
} }
return _groupSparsePagePool[--_groupSparsePagePoolCount]; var takedPage = _groupSparsePagePool[--_groupSparsePagePoolCount];
_groupSparsePagePool[_groupSparsePagePoolCount] = null;
return takedPage;
} }
internal void ReturnPage(int* page) internal void ReturnPage(int* page)
{ {
@ -214,6 +216,16 @@ namespace DCFApixels.DragonECS
} }
_groupSparsePagePool[_groupSparsePagePoolCount++] = page; _groupSparsePagePool[_groupSparsePagePoolCount++] = page;
} }
private void DisposeGroupPages()
{
foreach (var page in _groupSparsePagePool)
{
if (page != null)
{
UnmanagedArrayUtility.Free(page);
}
}
}
#endregion #endregion
#region Groups Pool #region Groups Pool

View File

@ -7,13 +7,12 @@ namespace DCFApixels.DragonECS.Core
{ {
public abstract class EcsMetaAttribute : Attribute { } public abstract class EcsMetaAttribute : Attribute { }
internal static class EcsMetaAttributeHalper
internal unsafe static class EcsMetaAttributeHalper
{ {
internal const string EMPTY_NO_SENSE_MESSAGE = "With empty parameters, this attribute makes no sense."; internal const string EMPTY_NO_SENSE_MESSAGE = "With empty parameters, this attribute makes no sense.";
[ThreadStatic] [ThreadStatic]
private static string[] _splitBuffer; private static string[] _splitBuffer;
public static string[] Split(char separator, string value) public static unsafe string[] Split(char separator, string value)
{ {
if (_splitBuffer == null) if (_splitBuffer == null)
{ {
@ -46,7 +45,7 @@ namespace DCFApixels.DragonECS.Core
} }
#region SplitStream #region SplitStream
private ref struct SplitStream private unsafe ref struct SplitStream
{ {
public string current; public string current;
public char* ptr; public char* ptr;

View File

@ -198,7 +198,7 @@ namespace DCFApixels.DragonECS
#region Constructors/New #region Constructors/New
private Builder() { } private Builder() { }
internal static unsafe (TAspect aspect, EcsMask mask) New<TAspect>(EcsWorld world) where TAspect : new() internal static (TAspect aspect, EcsMask mask) New<TAspect>(EcsWorld world) where TAspect : new()
{ {
//Get Builder //Get Builder
if (_constructorBuildersStack == null) if (_constructorBuildersStack == null)

View File

@ -758,7 +758,7 @@ namespace DCFApixels.DragonECS
[Il2CppSetOption(Option.NullChecks, false)] [Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif #endif
public unsafe ref struct Enumerator public ref struct Enumerator
{ {
private ReadOnlySpan<int>.Enumerator _span; private ReadOnlySpan<int>.Enumerator _span;
@ -784,7 +784,7 @@ namespace DCFApixels.DragonECS
get { return _span.Current; } get { return _span.Current; }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() public unsafe bool MoveNext()
{ {
while (_span.MoveNext()) while (_span.MoveNext())
{ {

View File

@ -236,6 +236,7 @@ namespace DCFApixels.DragonECS
_isDestroyed = true; _isDestroyed = true;
_poolTypeCode_2_CmpTypeIDs = null; _poolTypeCode_2_CmpTypeIDs = null;
_cmpTypeCode_2_CmpTypeIDs = null; _cmpTypeCode_2_CmpTypeIDs = null;
DisposeGroupPages();
foreach (var item in _executorCoures) foreach (var item in _executorCoures)
{ {
@ -1054,6 +1055,11 @@ namespace DCFApixels.DragonECS
list.Add(_pools[poolIdsPtr[i]]); list.Add(_pools[poolIdsPtr[i]]);
} }
} }
if (count >= BUFFER_THRESHOLD)
{
UnmanagedArrayUtility.Free(poolIdsPtr);
}
} }
public ReadOnlySpan<object> GetComponentsFor(int entityID) public ReadOnlySpan<object> GetComponentsFor(int entityID)
{ {

View File

@ -4,6 +4,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS.Core namespace DCFApixels.DragonECS.Core
{ {
@ -70,7 +71,7 @@ namespace DCFApixels.DragonECS.Core
public unsafe class DependencyGraph<T> : IDependencyGraph<T> public class DependencyGraph<T> : IDependencyGraph<T>
{ {
private readonly Dictionary<T, VertexID> _vertexIDs = new Dictionary<T, VertexID>(32); private readonly Dictionary<T, VertexID> _vertexIDs = new Dictionary<T, VertexID>(32);
private StructList<VertexInfo> _vertexInfos = new StructList<VertexInfo>(32); private StructList<VertexInfo> _vertexInfos = new StructList<VertexInfo>(32);
@ -226,7 +227,7 @@ namespace DCFApixels.DragonECS.Core
#endregion #endregion
#region Sort #region Sort
public T[] Sort() public unsafe T[] Sort()
{ {
const int BUFFER_THRESHOLD = 256; const int BUFFER_THRESHOLD = 256;
if (_count <= BUFFER_THRESHOLD) if (_count <= BUFFER_THRESHOLD)
@ -248,7 +249,7 @@ namespace DCFApixels.DragonECS.Core
return ConvertIdsToTsArray(buffer); return ConvertIdsToTsArray(buffer);
} }
} }
private void TopoSorting(UnsafeArray<VertexID> sortingBuffer) private unsafe void TopoSorting(UnsafeArray<VertexID> sortingBuffer)
{ {
VertexID[] nodes = new VertexID[_count]; VertexID[] nodes = new VertexID[_count];
var adjacency = new List<(VertexID To, int DependencyIndex)>[GetVertexInfosCount()]; var adjacency = new List<(VertexID To, int DependencyIndex)>[GetVertexInfosCount()];
@ -336,7 +337,7 @@ namespace DCFApixels.DragonECS.Core
throw new InvalidOperationException("Cyclic dependency detected." + details); throw new InvalidOperationException("Cyclic dependency detected." + details);
} }
} }
private void ReoderInsertionIndexes(UnsafeArray<VertexID> sortingBuffer) private unsafe void ReoderInsertionIndexes(UnsafeArray<VertexID> sortingBuffer)
{ {
for (int i = 0; i < GetVertexInfosCount(); i++) for (int i = 0; i < GetVertexInfosCount(); i++)
{ {
@ -375,7 +376,7 @@ namespace DCFApixels.DragonECS.Core
} }
} }
private static void MoveElement<TValue>(ref UnsafeArray<TValue> array, int oldIndex, int newIndex) where TValue : unmanaged private static unsafe void MoveElement<TValue>(ref UnsafeArray<TValue> array, int oldIndex, int newIndex) where TValue : unmanaged
{ {
if (oldIndex == newIndex) return; if (oldIndex == newIndex) return;
@ -405,7 +406,7 @@ namespace DCFApixels.DragonECS.Core
ptr[newIndex] = item; ptr[newIndex] = item;
} }
private T[] ConvertIdsToTsArray(UnsafeArray<VertexID> buffer) private unsafe T[] ConvertIdsToTsArray(UnsafeArray<VertexID> buffer)
{ {
T[] result = new T[buffer.Length]; T[] result = new T[buffer.Length];
for (int i = 0; i < result.Length; i++) for (int i = 0; i < result.Length; i++)