optimize GetComponents debug method

This commit is contained in:
Mikhail 2024-03-03 04:49:35 +08:00
parent 23c7b62ee8
commit ec4142bc7d
2 changed files with 52 additions and 16 deletions

View File

@ -43,7 +43,7 @@ namespace DCFApixels.DragonECS
internal int _entityComponentMaskLength;
internal int[] _entityComponentMasks = Array.Empty<int>();
private const int COMPONENT_MATRIX_MASK_BITSIZE = 32;
private const int COMPONENT_MASK_CHUNK_SIZE = 32;
//"лениво" обновляется только для NewEntity
private long _deleteLeakedEntitesLastVersion = 0;
@ -528,7 +528,7 @@ namespace DCFApixels.DragonECS
{
Array.Resize(ref _entities, newSize);
Array.Resize(ref _delEntBuffer, newSize);
_entityComponentMaskLength = _pools.Length / COMPONENT_MATRIX_MASK_BITSIZE + 1;
_entityComponentMaskLength = _pools.Length / COMPONENT_MASK_CHUNK_SIZE + 1;
Array.Resize(ref _entityComponentMasks, newSize * _entityComponentMaskLength);
ArrayUtility.Fill(_entities, EntitySlot.Empty, _entitiesCapacity);
@ -623,40 +623,76 @@ namespace DCFApixels.DragonECS
}
#endregion
#region Debug
#region Debug Components
public void GetComponents(int entityID, List<object> list)
{
list.Clear();
var itemsCount = GetComponentsCount(entityID);
for (var i = 0; i < _poolsCount; i++)
int poolIndex = entityID;
uint bit;
for (int chunkIndex = 0; chunkIndex < _entityComponentMaskLength; chunkIndex++)
{
if (_pools[i].Has(entityID))
bit = 0x8000_0000;
int chunk = _entityComponentMasks[chunkIndex];
if (chunk == 0)
{
poolIndex += COMPONENT_MASK_CHUNK_SIZE;
}
else
{
while (bit != 0)
{
if ((chunk & bit) != 0)
{
itemsCount--;
list.Add(_pools[i].GetRaw(entityID));
list.Add(_pools[poolIndex].GetRaw(entityID));
if (itemsCount <= 0)
{
break;
goto exit;
}
}
bit >>= 1;
poolIndex++;
}
}
}
exit:;
}
public void GetComponentTypes(int entityID, HashSet<Type> typeSet)
{
typeSet.Clear();
var itemsCount = GetComponentsCount(entityID);
for (var i = 0; i < _poolsCount; i++)
int poolIndex = entityID;
uint bit;
for (int chunkIndex = 0; chunkIndex < _entityComponentMaskLength; chunkIndex++)
{
if (_pools[i].Has(entityID))
bit = 0x8000_0000;
int chunk = _entityComponentMasks[chunkIndex];
if (chunk == 0)
{
poolIndex += COMPONENT_MASK_CHUNK_SIZE;
}
else
{
while (bit != 0)
{
if ((chunk & bit) != 0)
{
itemsCount--;
typeSet.Add(_pools[i].ComponentType);
typeSet.Add(_pools[poolIndex].ComponentType);
if (itemsCount <= 0)
{
break;
goto exit;
}
}
bit >>= 1;
poolIndex++;
}
}
}
exit:;
}
#endregion
}

View File

@ -166,7 +166,7 @@ namespace DCFApixels.DragonECS
Array.Resize(ref _poolComponentCounts, _pools.Length);
ArrayUtility.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length);
int newEntityComponentMaskLength = _pools.Length / COMPONENT_MATRIX_MASK_BITSIZE + 1;
int newEntityComponentMaskLength = _pools.Length / COMPONENT_MASK_CHUNK_SIZE + 1;
int dif = newEntityComponentMaskLength - _entityComponentMaskLength;
if (dif > 0)
{