This commit is contained in:
Mikhail 2026-03-21 23:53:58 +08:00
parent 2f8dc4a5ed
commit 4f676a76bb
3 changed files with 65 additions and 72 deletions

View File

@ -32,6 +32,7 @@ namespace DCFApixels.DragonECS.Core.Internal
} }
internal static HandlerDebugInfo[] CreateCurrentHandlersList_Debug() internal static HandlerDebugInfo[] CreateCurrentHandlersList_Debug()
{ {
#if DEBUG
var result = new HandlerDebugInfo[_idDispenser.Count]; var result = new HandlerDebugInfo[_idDispenser.Count];
int i = 0; int i = 0;
foreach (var id in _idDispenser) foreach (var id in _idDispenser)
@ -40,6 +41,9 @@ namespace DCFApixels.DragonECS.Core.Internal
} }
SortHalper.SortBy<HandlerDebugInfo, ulong>(result, o => o.increment); SortHalper.SortBy<HandlerDebugInfo, ulong>(result, o => o.increment);
return result; return result;
#else
return Array.Empty<HandlerDebugInfo>();
#endif
} }
#region AllocAndInit #region AllocAndInit
@ -171,7 +175,9 @@ namespace DCFApixels.DragonECS.Core.Internal
// id = _idDispenser.UseFree(); // id = _idDispenser.UseFree();
// } // }
//#endif //#endif
#if DEBUG
var id = target.GetHandledPtr()->ID; var id = target.GetHandledPtr()->ID;
#endif
Meta* newHandledPtr = (Meta*)Marshal.ReAllocHGlobal( Meta* newHandledPtr = (Meta*)Marshal.ReAllocHGlobal(
(IntPtr)target.GetHandledPtr(), (IntPtr)target.GetHandledPtr(),
@ -189,7 +195,7 @@ namespace DCFApixels.DragonECS.Core.Internal
#endif #endif
return handler; return handler;
} }
#endregion #endregion
#region Clone #region Clone
public static HMem<T> From<T>(HMem<T> source) public static HMem<T> From<T>(HMem<T> source)
@ -528,7 +534,7 @@ namespace DCFApixels.DragonECS.Core.Internal
} }
#pragma warning restore IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling. #pragma warning restore IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.
#endif #endif
#endregion #endregion
} }
#endregion #endregion
} }

View File

@ -4,7 +4,6 @@
using DCFApixels.DragonECS.Core; using DCFApixels.DragonECS.Core;
using DCFApixels.DragonECS.Core.Internal; using DCFApixels.DragonECS.Core.Internal;
using DCFApixels.DragonECS.PoolsCore; using DCFApixels.DragonECS.PoolsCore;
using DCFApixels.DragonECS.UncheckedCore;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -100,15 +99,10 @@ namespace DCFApixels.DragonECS
#region Constructors/Init/Destroy #region Constructors/Init/Destroy
public EcsPool() { } public EcsPool() { }
public EcsPool(int capacity, int recycledCapacity = -1) public EcsPool(int capacity)
{ {
capacity = ArrayUtility.CeilPow2Safe(capacity); capacity = ArrayUtility.CeilPow2Safe(capacity);
if (recycledCapacity < 0)
{
recycledCapacity = capacity / 2;
}
_items = new T[capacity]; _items = new T[capacity];
_recycledItems = new int[recycledCapacity];
} }
void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID) void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID)
{ {
@ -118,20 +112,13 @@ namespace DCFApixels.DragonECS
_componentTypeID = componentTypeID; _componentTypeID = componentTypeID;
_maskBit = EcsMaskChunck.FromID(componentTypeID); _maskBit = EcsMaskChunck.FromID(componentTypeID);
_mapping = new int[world.Capacity];
var worldConfig = world.Configs.GetWorldConfigOrDefault(); var worldConfig = world.Configs.GetWorldConfigOrDefault();
if (_items == null) if (_items == null)
{ {
_items = new T[ArrayUtility.CeilPow2Safe(worldConfig.PoolComponentsCapacity)]; _items = new T[ArrayUtility.CeilPow2Safe(worldConfig.PoolComponentsCapacity)];
} }
if (_recycledItems == null)
{
_recycledItems = new int[worldConfig.PoolRecycledComponentsCapacity];
}
_mapping = new int[world.Capacity]; _mapping = new int[world.Capacity];
Resize(ArrayUtility.NextPow2(world.Configs.GetWorldConfigOrDefault().PoolComponentsCapacity)); Resize(ArrayUtility.NextPow2(worldConfig.PoolComponentsCapacity));
} }
void IEcsPoolImplementation.OnWorldDestroy() { } void IEcsPoolImplementation.OnWorldDestroy() { }
#endregion #endregion
@ -189,48 +176,48 @@ namespace DCFApixels.DragonECS
} }
public ref T TryAddOrGet(int entityID) public ref T TryAddOrGet(int entityID)
{ {
//if (Has(entityID)) if (Has(entityID))
//{ {
// return ref Get(entityID); return ref Get(entityID);
//} }
//else else
//{ {
// return ref Add(entityID); return ref Add(entityID);
//} }
#if DEBUG //#if DEBUG
if (entityID == EcsConsts.NULL_ENTITY_ID) { EcsPoolThrowHelper.ThrowEntityIsNotAlive(_world, entityID); } // if (entityID == EcsConsts.NULL_ENTITY_ID) { EcsPoolThrowHelper.ThrowEntityIsNotAlive(_world, entityID); }
#endif //#endif
ref int itemIndex = ref _mapping[entityID]; // ref int itemIndex = ref _mapping[entityID];
if (itemIndex <= 0) // if (itemIndex <= 0)
{ //Add block // { //Add block
#if DEBUG //#if DEBUG
if (_isLocked) { EcsPoolThrowHelper.ThrowPoolLocked(); } // if (_isLocked) { EcsPoolThrowHelper.ThrowPoolLocked(); }
#elif DRAGONECS_STABILITY_MODE //#elif DRAGONECS_STABILITY_MODE
if (_isLocked) { return ref _items[0]; } // if (_isLocked) { return ref _items[0]; }
#endif //#endif
itemIndex = GetFreeItemIndex(entityID); // itemIndex = GetFreeItemIndex(entityID);
_mediator.RegisterComponent(entityID, _componentTypeID, _maskBit); // _mediator.RegisterComponent(entityID, _componentTypeID, _maskBit);
_sparseEntities[itemIndex] = entityID; // _sparseEntities[itemIndex] = entityID;
EcsComponentLifecycle<T>.OnAdd(_isCustomLifecycle, _customLifecycle, ref _items[itemIndex], _worldID, entityID); // EcsComponentLifecycle<T>.OnAdd(_isCustomLifecycle, _customLifecycle, ref _items[itemIndex], _worldID, entityID);
//
#if !DRAGONECS_DISABLE_POOLS_EVENTS //#if !DRAGONECS_DISABLE_POOLS_EVENTS
if (_hasAnyListener) { _listeners.InvokeOnAdd(entityID); } // if (_hasAnyListener) { _listeners.InvokeOnAdd(entityID); }
#endif //#endif
} //Add block end // } //Add block end
#if !DRAGONECS_DISABLE_POOLS_EVENTS //#if !DRAGONECS_DISABLE_POOLS_EVENTS
if (_hasAnyListener) { _listeners.InvokeOnGet(entityID); } // if (_hasAnyListener) { _listeners.InvokeOnGet(entityID); }
#endif //#endif
//
//
#if DRAGONECS_DEEP_DEBUG //#if DRAGONECS_DEEP_DEBUG
if (_mediator.GetComponentCount(_componentTypeID) != _itemsCount) // if (_mediator.GetComponentCount(_componentTypeID) != _itemsCount)
{ // {
Throw.UndefinedException(); // Throw.UndefinedException();
} // }
#endif //#endif
return ref _items[itemIndex]; // return ref _items[itemIndex];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Has(int entityID) public bool Has(int entityID)
@ -298,21 +285,20 @@ namespace DCFApixels.DragonECS
#elif DRAGONECS_STABILITY_MODE #elif DRAGONECS_STABILITY_MODE
if (_isLocked) { return; } if (_isLocked) { return; }
#endif #endif
_recycledItemsCount = 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD> <20><><EFBFBD> Del <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (_itemsCount <= 0) { return; } if (_itemsCount <= 0) { return; }
var span = _world.Where(out SingleAspect<T> _); var span = _world.Where(out SingleAspect<T> _);
foreach (var entityID in span) foreach (var entityID in span)
{ {
ref int itemIndex = ref _mapping[entityID]; Del(entityID);
EcsComponentLifecycle<T>.OnDel(_isCustomLifecycle, _customLifecycle, ref _items[itemIndex], _worldID, entityID); // ref int itemIndex = ref _mapping[entityID];
itemIndex = 0; // EcsComponentLifecycle<T>.OnDel(_isCustomLifecycle, _customLifecycle, ref _items[itemIndex], _worldID, entityID);
_mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit); // itemIndex = 0;
#if !DRAGONECS_DISABLE_POOLS_EVENTS // _mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit);
if (_hasAnyListener) { _listeners.InvokeOnDel(entityID); } //#if !DRAGONECS_DISABLE_POOLS_EVENTS
#endif // if (_hasAnyListener) { _listeners.InvokeOnDel(entityID); }
//#endif
} }
_itemsCount = 0; _itemsCount = 0;
_recycledItemsCount = 0;
} }
#endregion #endregion
@ -356,13 +342,13 @@ namespace DCFApixels.DragonECS
#if DRAGONECS_DEEP_DEBUG #if DRAGONECS_DEEP_DEBUG
if (_lockToSpan) if (_lockToSpan)
{ {
return _source.Entities; return _world.Entities;
} }
#endif #endif
UpdateDenseEntities(); UpdateDenseEntities();
var span = new EcsSpan(_source.ID, _denseEntitiesDelayed, 1, _itemsCount); var span = new EcsSpan(_worldID, _denseEntitiesDelayed, 1, _itemsCount);
#if DRAGONECS_DEEP_DEBUG #if DRAGONECS_DEEP_DEBUG
if (UncheckedCoreUtility.CheckSpanValideDebug(span) == false) if (Core.Unchecked.UncheckedUtility.CheckSpanValideDebug(span) == false)
{ {
Throw.UndefinedException(); Throw.UndefinedException();
} }
@ -403,10 +389,10 @@ namespace DCFApixels.DragonECS
{ {
Throw.UndefinedException(); Throw.UndefinedException();
} }
using (_source.DisableAutoReleaseDelEntBuffer()) using (var group = EcsGroup.New(_source)) using (_world.DisableAutoReleaseDelEntBuffer()) using (var group = EcsGroup.New(_world))
{ {
EcsStaticMask.Inc<T>().Build().ToMask(_source).GetIterator().IterateTo(World.Entities, group); EcsStaticMask.Inc<T>().Build().ToMask(_world).GetIterator().CacheTo(World.Entities, group);
var span = new EcsSpan(_source.ID, _denseEntitiesDelayed, 1, _itemsCount); var span = new EcsSpan(_world.ID, _denseEntitiesDelayed, 1, _itemsCount);
if (group.SetEquals(span) == false) if (group.SetEquals(span) == false)
{ {
Throw.UndefinedException(); Throw.UndefinedException();

View File

@ -126,6 +126,7 @@ namespace DCFApixels.DragonECS
void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID) void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID)
{ {
_world = world; _world = world;
_worldID = world.ID;
_mediator = mediator; _mediator = mediator;
_componentTypeID = componentTypeID; _componentTypeID = componentTypeID;
_maskBit = EcsMaskChunck.FromID(componentTypeID); _maskBit = EcsMaskChunck.FromID(componentTypeID);