optimize New/DelEntity WIP

This commit is contained in:
Mikhail 2024-02-13 21:00:32 +08:00
parent f1f23181f5
commit d76a699091
2 changed files with 17 additions and 21 deletions

View File

@ -13,12 +13,11 @@ namespace DCFApixels.DragonECS
private bool _isDestroyed; private bool _isDestroyed;
private IntDispenser _entityDispenser; private IdDispenser _entityDispenser;
private int _entitiesCount; private int _entitiesCount;
private int _entitesCapacity; private int _entitesCapacity;
private short[] _gens; //старший бит указывает на то жива ли сущность private short[] _gens; //старший бит указывает на то жива ли сущность
private short[] _componentCounts; private short[] _componentCounts;
private EcsGroup _allEntites;
private int[] _delEntBuffer; private int[] _delEntBuffer;
private int _delEntBufferCount; private int _delEntBufferCount;
@ -73,7 +72,7 @@ namespace DCFApixels.DragonECS
get { return _isEnableAutoReleaseDelEntBuffer; } get { return _isEnableAutoReleaseDelEntBuffer; }
} }
public EcsReadonlyGroup Entities public EcsSpan Entities
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get get
@ -82,7 +81,7 @@ namespace DCFApixels.DragonECS
{ {
ReleaseDelEntityBufferAll(); ReleaseDelEntityBufferAll();
} }
return _allEntites.Readonly; return _entityDispenser.UsedToEcsSpan(id);
} }
} }
public ReadOnlySpan<IEcsPoolImplementation> AllPools public ReadOnlySpan<IEcsPoolImplementation> AllPools
@ -114,7 +113,6 @@ namespace DCFApixels.DragonECS
} }
_poolsMediator = new PoolsMediator(this); _poolsMediator = new PoolsMediator(this);
_entityDispenser = new IntDispenser(0);
int poolsCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.Get_PoolsCapacity()); int poolsCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.Get_PoolsCapacity());
_pools = new IEcsPoolImplementation[poolsCapacity]; _pools = new IEcsPoolImplementation[poolsCapacity];
@ -122,6 +120,7 @@ namespace DCFApixels.DragonECS
ArrayUtility.Fill(_pools, _nullPool); ArrayUtility.Fill(_pools, _nullPool);
_entitesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.Get_EntitiesCapacity()); _entitesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.Get_EntitiesCapacity());
_entityDispenser = new IdDispenser(_entitesCapacity, 0);
_gens = new short[_entitesCapacity]; _gens = new short[_entitesCapacity];
_componentCounts = new short[_entitesCapacity]; _componentCounts = new short[_entitesCapacity];
@ -135,8 +134,6 @@ namespace DCFApixels.DragonECS
{ {
_entitiesComponentMasks[i] = new int[maskLength]; _entitiesComponentMasks[i] = new int[maskLength];
} }
_allEntites = GetFreeGroup();
} }
public void Destroy() public void Destroy()
{ {
@ -195,13 +192,13 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ToSpan() public EcsSpan ToSpan()
{ {
return _allEntites.ToSpan(); return _entityDispenser.UsedToEcsSpan(id);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public int NewEntity() public int NewEntity()
{ {
unchecked { _version++; } unchecked { _version++; }
int entityID = _entityDispenser.GetFree(); int entityID = _entityDispenser.UseFree();
_entitiesCount++; _entitiesCount++;
if (_gens.Length <= entityID) if (_gens.Length <= entityID)
@ -210,7 +207,6 @@ namespace DCFApixels.DragonECS
} }
_gens[entityID] &= GEN_BITS; _gens[entityID] &= GEN_BITS;
_allEntites.AddUnchecked(entityID);
_entityListeners.InvokeOnNewEntity(entityID); _entityListeners.InvokeOnNewEntity(entityID);
return entityID; return entityID;
} }
@ -237,7 +233,6 @@ namespace DCFApixels.DragonECS
Throw.World_EntityIsNotContained(entityID); Throw.World_EntityIsNotContained(entityID);
} }
#endif #endif
_allEntites.RemoveUnchecked(entityID);
_delEntBuffer[_delEntBufferCount++] = entityID; _delEntBuffer[_delEntBufferCount++] = entityID;
_gens[entityID] |= DEATH_GEN_BIT; _gens[entityID] |= DEATH_GEN_BIT;
_entitiesCount--; _entitiesCount--;
@ -279,7 +274,8 @@ namespace DCFApixels.DragonECS
public void DeleteEmptyEntites() public void DeleteEmptyEntites()
{ {
foreach (var e in _allEntites) throw new NotImplementedException();
foreach (var e in Entities)
{ {
if (_componentCounts[e] <= 0) if (_componentCounts[e] <= 0)
{ {

View File

@ -142,15 +142,15 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UnregisterEntityComponent(int entityID, int componentTypeID, EcsMaskChunck maskBit) private void UnregisterEntityComponent(int entityID, int componentTypeID, EcsMaskChunck maskBit)
{ {
_poolComponentCounts[componentTypeID]--; // _poolComponentCounts[componentTypeID]--;
var count = --_componentCounts[entityID]; // var count = --_componentCounts[entityID];
_entitiesComponentMasks[entityID][maskBit.chankIndex] &= ~maskBit.mask; // _entitiesComponentMasks[entityID][maskBit.chankIndex] &= ~maskBit.mask;
//
if (count == 0 && _allEntites.Has(entityID)) // if (count == 0 && _allEntites.Has(entityID))
DelEntity(entityID); // DelEntity(entityID);
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS //#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (count < 0) Throw.World_InvalidIncrementComponentsBalance(); // if (count < 0) Throw.World_InvalidIncrementComponentsBalance();
#endif //#endif
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool HasEntityComponent(int entityID, EcsMaskChunck maskBit) private bool HasEntityComponent(int entityID, EcsMaskChunck maskBit)