eliminate more dictionaries

pull/7/head
cosmonaut 2023-12-20 15:07:19 -08:00
parent d2b8e68b08
commit c58a5a24b8
2 changed files with 58 additions and 49 deletions

View File

@ -15,11 +15,11 @@ public class Snapshot : IDisposable
private List<RelationSnapshot> RelationSnapshots = new List<RelationSnapshot>(); private List<RelationSnapshot> RelationSnapshots = new List<RelationSnapshot>();
private Dictionary<Entity, IndexableSet<TypeId>> EntityRelationIndex = private List<IndexableSet<TypeId>> EntityRelationIndex =
new Dictionary<Entity, IndexableSet<TypeId>>(); new List<IndexableSet<TypeId>>();
private Dictionary<Entity, IndexableSet<TypeId>> EntityComponentIndex = private List<IndexableSet<TypeId>> EntityComponentIndex =
new Dictionary<Entity, IndexableSet<TypeId>>(); new List<IndexableSet<TypeId>>();
private List<string> EntityTags = new List<string>(); private List<string> EntityTags = new List<string>();
@ -76,32 +76,36 @@ public class Snapshot : IDisposable
// restore entity relation index state // restore entity relation index state
// FIXME: arghhhh this is so slow // FIXME: arghhhh this is so slow
foreach (var (id, relationTypeSet) in world.EntityRelationIndex) foreach (var relationTypeSet in world.EntityRelationIndex)
{ {
relationTypeSet.Clear(); relationTypeSet.Clear();
} }
foreach (var (id, relationTypeSet) in EntityRelationIndex) for (var i = 0; i < EntityRelationIndex.Count; i += 1)
{ {
var relationTypeSet = EntityRelationIndex[i];
foreach (var typeId in relationTypeSet) foreach (var typeId in relationTypeSet)
{ {
world.EntityRelationIndex[id].Add(typeId); world.EntityRelationIndex[i].Add(typeId);
} }
} }
// restore entity component index state // restore entity component index state
// FIXME: arrghghhh this is so slow // FIXME: arrghghhh this is so slow
foreach (var (id, componentTypeSet) in world.EntityComponentIndex) foreach (var componentTypeSet in world.EntityComponentIndex)
{ {
componentTypeSet.Clear(); componentTypeSet.Clear();
} }
foreach (var (id, componentTypeSet) in EntityComponentIndex) for (var i = 0; i < EntityComponentIndex.Count; i += 1)
{ {
var componentTypeSet = EntityComponentIndex[i];
foreach (var typeId in componentTypeSet) foreach (var typeId in componentTypeSet)
{ {
world.EntityComponentIndex[id].Add(typeId); world.EntityComponentIndex[i].Add(typeId);
} }
} }
@ -145,37 +149,39 @@ public class Snapshot : IDisposable
RelationSnapshots[i].Take(world.RelationIndex[i]); RelationSnapshots[i].Take(world.RelationIndex[i]);
} }
// fill in missing index structures
for (var i = EntityComponentIndex.Count; i < world.EntityComponentIndex.Count; i += 1)
{
EntityComponentIndex.Add(new IndexableSet<TypeId>());
}
for (var i = EntityRelationIndex.Count; i < world.EntityRelationIndex.Count; i += 1)
{
EntityRelationIndex.Add(new IndexableSet<TypeId>());
}
// copy entity relation index // copy entity relation index
// FIXME: arghhhh this is so slow // FIXME: arghhhh this is so slow
foreach (var (id, relationTypeSet) in world.EntityRelationIndex) for (var i = 0; i < world.EntityRelationIndex.Count; i += 1)
{ {
if (!EntityRelationIndex.ContainsKey(id)) EntityRelationIndex[i].Clear();
{
EntityRelationIndex.Add(id, new IndexableSet<TypeId>());
}
EntityRelationIndex[id].Clear(); foreach (var typeId in world.EntityRelationIndex[i])
foreach (var typeId in relationTypeSet)
{ {
EntityRelationIndex[id].Add(typeId); EntityRelationIndex[i].Add(typeId);
} }
} }
// copy entity component index // copy entity component index
// FIXME: arghhhh this is so slow // FIXME: arghhhh this is so slow
foreach (var (id, componentTypeSet) in world.EntityComponentIndex) for (var i = 0; i < world.EntityComponentIndex.Count; i += 1)
{ {
if (!EntityComponentIndex.ContainsKey(id)) EntityComponentIndex[i].Clear();
{
EntityComponentIndex.Add(id, new IndexableSet<TypeId>());
}
EntityComponentIndex[id].Clear(); foreach (var typeId in world.EntityComponentIndex[i])
foreach (var typeId in componentTypeSet)
{ {
EntityComponentIndex[id].Add(typeId); EntityComponentIndex[i].Add(typeId);
} }
} }
@ -346,12 +352,12 @@ public class Snapshot : IDisposable
relationSnapshot.Dispose(); relationSnapshot.Dispose();
} }
foreach (var componentSet in EntityComponentIndex.Values) foreach (var componentSet in EntityComponentIndex)
{ {
componentSet.Dispose(); componentSet.Dispose();
} }
foreach (var relationSet in EntityRelationIndex.Values) foreach (var relationSet in EntityRelationIndex)
{ {
relationSet.Dispose(); relationSet.Dispose();
} }

View File

@ -26,7 +26,7 @@ public class World : IDisposable
// Relation Storages // Relation Storages
internal List<RelationStorage> RelationIndex = new List<RelationStorage>(); internal List<RelationStorage> RelationIndex = new List<RelationStorage>();
internal Dictionary<Entity, IndexableSet<TypeId>> EntityRelationIndex = new Dictionary<Entity, IndexableSet<TypeId>>(); internal List<IndexableSet<TypeId>> EntityRelationIndex = new List<IndexableSet<TypeId>>();
// Message Storages // Message Storages
private List<MessageStorage> MessageIndex = new List<MessageStorage>(); private List<MessageStorage> MessageIndex = new List<MessageStorage>();
@ -34,7 +34,7 @@ public class World : IDisposable
public FilterBuilder FilterBuilder => new FilterBuilder(this); public FilterBuilder FilterBuilder => new FilterBuilder(this);
internal readonly List<ComponentStorage> ComponentIndex = new List<ComponentStorage>(); internal readonly List<ComponentStorage> ComponentIndex = new List<ComponentStorage>();
internal Dictionary<Entity, IndexableSet<TypeId>> EntityComponentIndex = new Dictionary<Entity, IndexableSet<TypeId>>(); internal List<IndexableSet<TypeId>> EntityComponentIndex = new List<IndexableSet<TypeId>>();
internal IdAssigner EntityIdAssigner = new IdAssigner(); internal IdAssigner EntityIdAssigner = new IdAssigner();
@ -97,10 +97,10 @@ public class World : IDisposable
{ {
var entity = new Entity(EntityIdAssigner.Assign()); var entity = new Entity(EntityIdAssigner.Assign());
if (!EntityComponentIndex.ContainsKey(entity)) if (entity.ID == EntityComponentIndex.Count)
{ {
EntityRelationIndex.Add(entity, new IndexableSet<TypeId>()); EntityRelationIndex.Add(new IndexableSet<TypeId>());
EntityComponentIndex.Add(entity, new IndexableSet<TypeId>()); EntityComponentIndex.Add(new IndexableSet<TypeId>());
EntityTags.Add(tag); EntityTags.Add(tag);
} }
@ -119,8 +119,11 @@ public class World : IDisposable
public void Destroy(in Entity entity) public void Destroy(in Entity entity)
{ {
var componentSet = EntityComponentIndex[(int) entity.ID];
var relationSet = EntityRelationIndex[(int) entity.ID];
// remove all components from storages // remove all components from storages
foreach (var componentTypeIndex in EntityComponentIndex[entity]) foreach (var componentTypeIndex in componentSet)
{ {
var componentStorage = ComponentIndex[componentTypeIndex]; var componentStorage = ComponentIndex[componentTypeIndex];
componentStorage.Remove(entity); componentStorage.Remove(entity);
@ -132,14 +135,14 @@ public class World : IDisposable
} }
// remove all relations from storage // remove all relations from storage
foreach (var relationTypeIndex in EntityRelationIndex[entity]) foreach (var relationTypeIndex in relationSet)
{ {
var relationStorage = RelationIndex[relationTypeIndex]; var relationStorage = RelationIndex[relationTypeIndex];
relationStorage.RemoveEntity(entity); relationStorage.RemoveEntity(entity);
} }
EntityComponentIndex[entity].Clear(); componentSet.Clear();
EntityRelationIndex[entity].Clear(); relationSet.Clear();
// recycle ID // recycle ID
EntityIdAssigner.Unassign(entity.ID); EntityIdAssigner.Unassign(entity.ID);
@ -155,7 +158,7 @@ public class World : IDisposable
internal bool Has(in Entity entity, in TypeId typeId) internal bool Has(in Entity entity, in TypeId typeId)
{ {
return EntityComponentIndex[entity].Contains(typeId); return EntityComponentIndex[(int) entity.ID].Contains(typeId);
} }
public bool Some<T>() where T : unmanaged public bool Some<T>() where T : unmanaged
@ -188,7 +191,7 @@ public class World : IDisposable
if (!componentStorage.Set(entity, component)) if (!componentStorage.Set(entity, component))
{ {
EntityComponentIndex[entity].Add(componentStorage.TypeId); EntityComponentIndex[(int) entity.ID].Add(componentStorage.TypeId);
foreach (var filter in ComponentTypeToFilter[componentStorage.TypeId]) foreach (var filter in ComponentTypeToFilter[componentStorage.TypeId])
{ {
@ -203,7 +206,7 @@ public class World : IDisposable
if (componentStorage.Remove(entity)) if (componentStorage.Remove(entity))
{ {
EntityComponentIndex[entity].Remove(componentStorage.TypeId); EntityComponentIndex[(int) entity.ID].Remove(componentStorage.TypeId);
foreach (var filter in ComponentTypeToFilter[componentStorage.TypeId]) foreach (var filter in ComponentTypeToFilter[componentStorage.TypeId])
{ {
@ -235,8 +238,8 @@ public class World : IDisposable
{ {
var relationStorage = GetRelationStorage<T>(); var relationStorage = GetRelationStorage<T>();
relationStorage.Set(entityA, entityB, relation); relationStorage.Set(entityA, entityB, relation);
EntityRelationIndex[entityA].Add(new TypeId(RelationTypeIdAssigner<T>.Id)); EntityRelationIndex[(int) entityA.ID].Add(new TypeId(RelationTypeIdAssigner<T>.Id));
EntityRelationIndex[entityB].Add(new TypeId(RelationTypeIdAssigner<T>.Id)); EntityRelationIndex[(int) entityB.ID].Add(new TypeId(RelationTypeIdAssigner<T>.Id));
} }
public void Unrelate<T>(in Entity entityA, in Entity entityB) where T : unmanaged public void Unrelate<T>(in Entity entityA, in Entity entityB) where T : unmanaged
@ -388,7 +391,7 @@ public class World : IDisposable
#if DEBUG #if DEBUG
public ComponentTypeEnumerator Debug_GetAllComponentTypes(Entity entity) public ComponentTypeEnumerator Debug_GetAllComponentTypes(Entity entity)
{ {
return new ComponentTypeEnumerator(this, EntityComponentIndex[entity]); return new ComponentTypeEnumerator(this, EntityComponentIndex[(int) entity.ID]);
} }
public IEnumerable<Entity> Debug_GetEntities(Type componentType) public IEnumerable<Entity> Debug_GetEntities(Type componentType)
@ -457,14 +460,14 @@ public class World : IDisposable
messageStorage.Dispose(); messageStorage.Dispose();
} }
foreach (var typeSet in EntityComponentIndex.Values) foreach (var componentSet in EntityComponentIndex)
{ {
typeSet.Dispose(); componentSet.Dispose();
} }
foreach (var typeSet in EntityRelationIndex.Values) foreach (var relationSet in EntityRelationIndex)
{ {
typeSet.Dispose(); relationSet.Dispose();
} }
foreach (var filter in FilterIndex.Values) foreach (var filter in FilterIndex.Values)