storage tweak

pull/1/head
cosmonaut 2022-04-03 14:12:13 -07:00
parent b4c862d568
commit 31083994ab
4 changed files with 27 additions and 39 deletions

View File

@ -10,11 +10,18 @@ internal class ComponentDepot
private Dictionary<int, HashSet<Type>> entityComponentMap = new Dictionary<int, HashSet<Type>>();
#if DEBUG
private Dictionary<Type, Filter> singleComponentFilters = new Dictionary<Type, Filter>();
#endif
internal void Register<TComponent>() where TComponent : struct
{
if (!storages.ContainsKey(typeof(TComponent)))
{
storages.Add(typeof(TComponent), new ComponentStorage<TComponent>());
#if DEBUG
singleComponentFilters.Add(typeof(TComponent), CreateFilter(new HashSet<Type>() { typeof(TComponent) }, new HashSet<Type>()));
#endif
}
}
@ -79,14 +86,9 @@ internal class ComponentDepot
}
}
public ref readonly Entity GetEntity<TComponent>() where TComponent : struct
public Entity GetSingletonEntity<TComponent>() where TComponent : struct
{
return ref Lookup<TComponent>().FirstEntity();
}
public ReadOnlySpan<Entity> ReadEntities<TComponent>() where TComponent : struct
{
return Lookup<TComponent>().AllEntities();
return Lookup<TComponent>().FirstEntity();
}
public ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : struct
@ -226,8 +228,7 @@ internal class ComponentDepot
filterSignatureToEntityIDs[filterSignature].Add(entityID);
}
// debug use only!
#if DEBUG
public IEnumerable<object> Debug_GetAllComponents(int entityID)
{
foreach (var (type, storage) in storages)
@ -239,9 +240,9 @@ internal class ComponentDepot
}
}
public ReadOnlySpan<Entity> Debug_GetEntities(Type componentType)
public IEnumerable<Entity> Debug_GetEntities(Type componentType)
{
return Lookup(componentType).AllEntities();
return singleComponentFilters[componentType].Entities;
}
public IEnumerable<Type> Debug_SearchComponentType(string typeString)
@ -254,4 +255,5 @@ internal class ComponentDepot
}
}
}
#endif
}

View File

@ -4,8 +4,6 @@ internal abstract class ComponentStorage
{
public abstract bool Has(int entityID);
public abstract void Remove(int entityID);
public abstract ReadOnlySpan<Entity> AllEntities();
public abstract object Debug_Get(int entityID);
}
@ -14,9 +12,9 @@ internal class ComponentStorage<TComponent> : ComponentStorage where TComponent
{
private int nextID;
private IDStorage idStorage = new IDStorage();
private readonly Dictionary<int, int> entityIDToStorageIndex = new Dictionary<int, int>();
private Entity[] storageIndexToEntities = new Entity[64];
private TComponent[] components = new TComponent[64];
private readonly Dictionary<int, int> entityIDToStorageIndex = new Dictionary<int, int>(16);
private int[] entityIDs = new int[16];
private TComponent[] components = new TComponent[16];
public bool Any()
{
@ -59,11 +57,11 @@ internal class ComponentStorage<TComponent> : ComponentStorage where TComponent
if (index >= components.Length)
{
Array.Resize(ref components, components.Length * 2);
Array.Resize(ref storageIndexToEntities, storageIndexToEntities.Length * 2);
Array.Resize(ref entityIDs, entityIDs.Length * 2);
}
entityIDToStorageIndex[entityID] = index;
storageIndexToEntities[index] = new Entity(entityID);
entityIDs[index] = entityID;
}
components[entityIDToStorageIndex[entityID]] = component;
@ -79,13 +77,12 @@ internal class ComponentStorage<TComponent> : ComponentStorage where TComponent
var lastElementIndex = nextID - 1;
// move a component into the hole to maintain contiguous memory
if (entityIDToStorageIndex.Count > 0 && storageIndex != lastElementIndex)
if (lastElementIndex != storageIndex)
{
var lastEntity = storageIndexToEntities[lastElementIndex];
entityIDToStorageIndex[lastEntity.ID] = storageIndex;
storageIndexToEntities[storageIndex] = lastEntity;
var lastEntityID = entityIDs[lastElementIndex];
entityIDToStorageIndex[lastEntityID] = storageIndex;
components[storageIndex] = components[lastElementIndex];
entityIDs[storageIndex] = lastEntityID;
}
nextID -= 1;
@ -98,18 +95,13 @@ internal class ComponentStorage<TComponent> : ComponentStorage where TComponent
entityIDToStorageIndex.Clear();
}
public override ReadOnlySpan<Entity> AllEntities()
{
return new ReadOnlySpan<Entity>(storageIndexToEntities, 0, nextID);
}
public ReadOnlySpan<TComponent> AllComponents()
{
return new ReadOnlySpan<TComponent>(components, 0, nextID);
}
public ref readonly Entity FirstEntity()
public Entity FirstEntity()
{
return ref storageIndexToEntities[0];
return new Entity(entityIDs[0]);
}
}

View File

@ -13,7 +13,7 @@ namespace MoonTools.ECS
return ComponentDepot.Debug_GetAllComponents(entity.ID);
}
protected ReadOnlySpan<Entity> Debug_GetEntities(Type componentType)
protected IEnumerable<Entity> Debug_GetEntities(Type componentType)
{
return ComponentDepot.Debug_GetEntities(componentType);
}

View File

@ -16,12 +16,6 @@ public abstract class EntityComponentReader
ComponentDepot = componentDepot;
}
// TODO: is this faster or slower than a single-component Filter?
protected ReadOnlySpan<Entity> ReadEntities<TComponent>() where TComponent : struct
{
return ComponentDepot.ReadEntities<TComponent>();
}
protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : struct
{
return ComponentDepot.ReadComponents<TComponent>();
@ -47,9 +41,9 @@ public abstract class EntityComponentReader
return ref ComponentDepot.Get<TComponent>();
}
protected ref readonly Entity GetEntity<TComponent>() where TComponent : struct
protected Entity GetSingletonEntity<TComponent>() where TComponent : struct
{
return ref ComponentDepot.GetEntity<TComponent>();
return ComponentDepot.GetSingletonEntity<TComponent>();
}
protected bool Exists(in Entity entity)