From 1953680eb20a7354cde76789a7ef5bfbc4ad7588 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 22 Mar 2026 22:35:20 +0800 Subject: [PATCH] Update EcsPool.cs --- src/Pools/EcsPool.cs | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/Pools/EcsPool.cs b/src/Pools/EcsPool.cs index 6b326f2..6e6b588 100644 --- a/src/Pools/EcsPool.cs +++ b/src/Pools/EcsPool.cs @@ -10,6 +10,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Runtime.CompilerServices; +using static DCFApixels.DragonECS.Core.Internal.MemoryAllocator; #if ENABLE_IL2CPP using Unity.IL2CPP.CompilerServices; #endif @@ -46,8 +47,7 @@ namespace DCFApixels.DragonECS private MappingSlot[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID. private T[] _items; // dense; _items[0] - fake component. private int _itemsCount = 0; - private int[] _recycledItems; - private int _recycledItemsCount = 0; + private int _recycledItemsCount; private readonly IEcsComponentLifecycle _customLifecycle = EcsComponentLifecycle.CustomHandler; private readonly bool _isCustomLifecycle = EcsComponentLifecycle.IsCustom; @@ -105,7 +105,6 @@ namespace DCFApixels.DragonECS } _items = new T[capacity]; _dense = new int[capacity]; - _recycledItems = new int[recycledCapacity]; } void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID) { @@ -122,10 +121,6 @@ namespace DCFApixels.DragonECS _items = new T[ArrayUtility.CeilPow2Safe(worldConfig.PoolComponentsCapacity)]; _dense = new int[ArrayUtility.CeilPow2Safe(worldConfig.PoolComponentsCapacity)]; } - if (_recycledItems == null) - { - _recycledItems = new int[worldConfig.PoolRecycledComponentsCapacity]; - } } void IEcsPoolImplementation.OnWorldDestroy() { } #endregion @@ -145,7 +140,10 @@ namespace DCFApixels.DragonECS #endif if (_recycledItemsCount > 0) { - mappingSlot.ComponentPosIndex = _recycledItems[--_recycledItemsCount]; + //mappingSlot.ComponentPosIndex = _recycledItems[--_recycledItemsCount]; + //mappingSlot.ComponentPosIndex = _dense[_dense.Length - _recycledItemsCount] & int.MaxValue; + mappingSlot.ComponentPosIndex = _dense[_dense.Length - _recycledItemsCount]; + _recycledItemsCount--; _itemsCount++; } else @@ -161,7 +159,7 @@ namespace DCFApixels.DragonECS _dense[_denseCount++] = entityID; _mediator.RegisterComponent(entityID, _componentTypeID, _maskBit); ref T result = ref _items[mappingSlot.ComponentPosIndex]; - EcsComponentLifecycle.OnAdd( _isCustomLifecycle, _customLifecycle, ref result, _worldID, entityID); + EcsComponentLifecycle.OnAdd(_isCustomLifecycle, _customLifecycle, ref result, _worldID, entityID); #if !DRAGONECS_DISABLE_POOLS_EVENTS if (_hasAnyListener) { _listeners.InvokeOnAddAndGet(entityID); } #endif @@ -201,7 +199,9 @@ namespace DCFApixels.DragonECS #endif if (_recycledItemsCount > 0) { - mappingSlot.ComponentPosIndex = _recycledItems[--_recycledItemsCount]; + //mappingSlot.ComponentPosIndex = _dense[_dense.Length - _recycledItemsCount] & int.MaxValue; + mappingSlot.ComponentPosIndex = _dense[_dense.Length - _recycledItemsCount]; + _recycledItemsCount--; _itemsCount++; } else @@ -229,7 +229,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Has(int entityID) { - return _mapping[entityID].ComponentPosIndex != 0; + return _mapping[entityID].ComponentPosIndex > 0; } public void Del(int entityID) { @@ -242,22 +242,28 @@ namespace DCFApixels.DragonECS if (itemIndex <= 0) { return; } if (_isLocked) { return; } #endif - EcsComponentLifecycle.OnDel( _isCustomLifecycle, _customLifecycle, ref _items[mappingSlot.ComponentPosIndex], _worldID, entityID); - if (_recycledItemsCount >= _recycledItems.Length) - { - Array.Resize(ref _recycledItems, ArrayUtility.NextPow2Safe(_recycledItemsCount)); - } + EcsComponentLifecycle.OnDel(_isCustomLifecycle, _customLifecycle, ref _items[mappingSlot.ComponentPosIndex], _worldID, entityID); + + //_dense[_sparse[entityID]] = _dense[_count]; + //_sparse[_dense[_count--]] = _sparse[entityID]; //_dense[_mapping[entityID].EntityPosIndex] = _dense[_denseCount]; //_mapping[_dense[_denseCount--]].EntityPosIndex = _mapping[entityID].EntityPosIndex; - _dense[mappingSlot.EntityPosIndex] = _dense[_denseCount]; - _mapping[_dense[_denseCount--]].EntityPosIndex = mappingSlot.EntityPosIndex; + + //_dense[mappingSlot.EntityPosIndex] = _dense[_denseCount]; + //_mapping[_dense[_denseCount--]].EntityPosIndex = mappingSlot.EntityPosIndex; - _recycledItems[_recycledItemsCount++] = mappingSlot.ComponentPosIndex; + _dense[mappingSlot.EntityPosIndex] = _dense[--_denseCount]; + _mapping[_dense[_denseCount]].EntityPosIndex = mappingSlot.EntityPosIndex; + + _recycledItemsCount++; + //_dense[_dense.Length - _recycledItemsCount] = mappingSlot.ComponentPosIndex | int.MinValue; + _dense[_dense.Length - _recycledItemsCount] = mappingSlot.ComponentPosIndex; mappingSlot.ComponentPosIndex = 0; _itemsCount--; _mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit); + #if !DRAGONECS_DISABLE_POOLS_EVENTS if (_hasAnyListener) { _listeners.InvokeOnDel(entityID); } #endif