change sets inside pooled dictionaries to be pooled

pull/5/head
Evan Hemsley 2019-08-08 22:38:48 -07:00
parent 5c5c62cfcd
commit fa7d1eeb77
2 changed files with 36 additions and 20 deletions

View File

@ -15,7 +15,7 @@ namespace Encompass
private readonly Dictionary<Guid, HashSet<Guid>> entityIDToComponentIDs = new Dictionary<Guid, HashSet<Guid>>(); private readonly Dictionary<Guid, HashSet<Guid>> entityIDToComponentIDs = new Dictionary<Guid, HashSet<Guid>>();
private readonly Dictionary<Guid, Guid> componentIDToEntityID = new Dictionary<Guid, Guid>(); private readonly Dictionary<Guid, Guid> componentIDToEntityID = new Dictionary<Guid, Guid>();
private readonly Dictionary<Guid, PooledDictionary<Type, HashSet<Guid>>> entityIDToComponentTypeToComponentIDs = new Dictionary<Guid, PooledDictionary<Type, HashSet<Guid>>>(); private readonly Dictionary<Guid, PooledDictionary<Type, PooledSet<Guid>>> entityIDToComponentTypeToComponentIDs = new Dictionary<Guid, PooledDictionary<Type, PooledSet<Guid>>>();
private readonly Dictionary<Type, HashSet<Guid>> typeToComponentIDs = new Dictionary<Type, HashSet<Guid>>(); private readonly Dictionary<Type, HashSet<Guid>> typeToComponentIDs = new Dictionary<Type, HashSet<Guid>>();
@ -31,7 +31,7 @@ namespace Encompass
internal void RegisterEntity(Guid entityID) internal void RegisterEntity(Guid entityID)
{ {
entityIDToComponentIDs.Add(entityID, new HashSet<Guid>()); entityIDToComponentIDs.Add(entityID, new HashSet<Guid>());
entityIDToComponentTypeToComponentIDs.Add(entityID, new PooledDictionary<Type, HashSet<Guid>>()); entityIDToComponentTypeToComponentIDs.Add(entityID, new PooledDictionary<Type, PooledSet<Guid>>());
} }
internal Guid NextID() internal Guid NextID()
@ -53,7 +53,7 @@ namespace Encompass
entityIDToComponentIDs[entity.ID].Add(componentID); entityIDToComponentIDs[entity.ID].Add(componentID);
if (!entityIDToComponentTypeToComponentIDs[entity.ID].ContainsKey(typeof(TComponent))) { if (!entityIDToComponentTypeToComponentIDs[entity.ID].ContainsKey(typeof(TComponent))) {
entityIDToComponentTypeToComponentIDs[entity.ID].Add(typeof(TComponent), new HashSet<Guid>()); entityIDToComponentTypeToComponentIDs[entity.ID].Add(typeof(TComponent), new PooledSet<Guid>());
} }
entityIDToComponentTypeToComponentIDs[entity.ID][typeof(TComponent)].Add(componentID); entityIDToComponentTypeToComponentIDs[entity.ID][typeof(TComponent)].Add(componentID);
@ -92,7 +92,7 @@ namespace Encompass
internal IEnumerable<ValueTuple<Guid, TComponent>> GetComponentsByEntityAndType<TComponent>(Guid entityID) where TComponent : struct, IComponent internal IEnumerable<ValueTuple<Guid, TComponent>> GetComponentsByEntityAndType<TComponent>(Guid entityID) where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; PooledSet<Guid> idSet;
if (entityIDToComponentTypeToComponentIDs.ContainsKey(entityID) && entityIDToComponentTypeToComponentIDs[entityID].TryGetValue(typeof(TComponent), out idSet)) if (entityIDToComponentTypeToComponentIDs.ContainsKey(entityID) && entityIDToComponentTypeToComponentIDs[entityID].TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Select(id => (id, (TComponent)IDToComponent[id])); return idSet.Select(id => (id, (TComponent)IDToComponent[id]));
@ -102,7 +102,7 @@ namespace Encompass
internal bool EntityHasComponentOfType<TComponent>(Guid entityID) where TComponent : struct, IComponent internal bool EntityHasComponentOfType<TComponent>(Guid entityID) where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; PooledSet<Guid> idSet;
if (entityIDToComponentTypeToComponentIDs.ContainsKey(entityID) && entityIDToComponentTypeToComponentIDs[entityID].TryGetValue(typeof(TComponent), out idSet)) if (entityIDToComponentTypeToComponentIDs.ContainsKey(entityID) && entityIDToComponentTypeToComponentIDs[entityID].TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Count > 0; return idSet.Count > 0;
@ -202,6 +202,10 @@ namespace Encompass
{ {
entityIDToComponentIDs.Remove(entityID); entityIDToComponentIDs.Remove(entityID);
foreach (var set in entityIDToComponentTypeToComponentIDs[entityID].Values)
{
set.Dispose();
}
entityIDToComponentTypeToComponentIDs[entityID].Dispose(); entityIDToComponentTypeToComponentIDs[entityID].Dispose();
entityIDToComponentTypeToComponentIDs.Remove(entityID); entityIDToComponentTypeToComponentIDs.Remove(entityID);
} }

View File

@ -13,25 +13,37 @@ namespace Encompass
private readonly Dictionary<Type, HashSet<Guid>> componentMessageTypeToPendingComponentIDs = new Dictionary<Type, HashSet<Guid>>(); private readonly Dictionary<Type, HashSet<Guid>> componentMessageTypeToPendingComponentIDs = new Dictionary<Type, HashSet<Guid>>();
private readonly Dictionary<Type, HashSet<Guid>> componentMessageTypeToComponentIDs = new Dictionary<Type, HashSet<Guid>>(); private readonly Dictionary<Type, HashSet<Guid>> componentMessageTypeToComponentIDs = new Dictionary<Type, HashSet<Guid>>();
private readonly Dictionary<Entity, PooledDictionary<Type, HashSet<Guid>>> entityToTypeToExistingComponentIDs = new Dictionary<Entity, PooledDictionary<Type, HashSet<Guid>>>(); private readonly Dictionary<Entity, PooledDictionary<Type, PooledSet<Guid>>> entityToTypeToExistingComponentIDs = new Dictionary<Entity, PooledDictionary<Type, PooledSet<Guid>>>();
private readonly Dictionary<Entity, PooledDictionary<Type, HashSet<Guid>>> entityToTypeToPendingComponentIDs = new Dictionary<Entity, PooledDictionary<Type, HashSet<Guid>>>(); private readonly Dictionary<Entity, PooledDictionary<Type, PooledSet<Guid>>> entityToTypeToPendingComponentIDs = new Dictionary<Entity, PooledDictionary<Type, PooledSet<Guid>>>();
private readonly Dictionary<Entity, PooledDictionary<Type, HashSet<Guid>>> entityToTypeToComponentIDs = new Dictionary<Entity, PooledDictionary<Type, HashSet<Guid>>>(); private readonly Dictionary<Entity, PooledDictionary<Type, PooledSet<Guid>>> entityToTypeToComponentIDs = new Dictionary<Entity, PooledDictionary<Type, PooledSet<Guid>>>();
internal void RegisterEntity(Entity entity) internal void RegisterEntity(Entity entity)
{ {
entityToTypeToComponentIDs[entity] = new PooledDictionary<Type, HashSet<Guid>>(); entityToTypeToComponentIDs[entity] = new PooledDictionary<Type, PooledSet<Guid>>();
entityToTypeToPendingComponentIDs[entity] = new PooledDictionary<Type, HashSet<Guid>>(); entityToTypeToPendingComponentIDs[entity] = new PooledDictionary<Type, PooledSet<Guid>>();
entityToTypeToExistingComponentIDs[entity] = new PooledDictionary<Type, HashSet<Guid>>(); entityToTypeToExistingComponentIDs[entity] = new PooledDictionary<Type, PooledSet<Guid>>();
} }
internal void RegisterDestroyedEntity(Entity entity) internal void RegisterDestroyedEntity(Entity entity)
{ {
foreach (var set in entityToTypeToComponentIDs[entity].Values)
{
set.Dispose();
}
entityToTypeToComponentIDs[entity].Dispose(); entityToTypeToComponentIDs[entity].Dispose();
entityToTypeToComponentIDs.Remove(entity); entityToTypeToComponentIDs.Remove(entity);
foreach (var set in entityToTypeToPendingComponentIDs[entity].Values)
{
set.Dispose();
}
entityToTypeToPendingComponentIDs[entity].Dispose(); entityToTypeToPendingComponentIDs[entity].Dispose();
entityToTypeToPendingComponentIDs.Remove(entity); entityToTypeToPendingComponentIDs.Remove(entity);
foreach (var set in entityToTypeToExistingComponentIDs[entity].Values)
{
set.Dispose();
}
entityToTypeToExistingComponentIDs[entity].Dispose(); entityToTypeToExistingComponentIDs[entity].Dispose();
entityToTypeToExistingComponentIDs.Remove(entity); entityToTypeToExistingComponentIDs.Remove(entity);
} }
@ -93,7 +105,7 @@ namespace Encompass
if (!entityToTypeToExistingComponentIDs[componentMessage.entity].ContainsKey(typeof(TComponent))) if (!entityToTypeToExistingComponentIDs[componentMessage.entity].ContainsKey(typeof(TComponent)))
{ {
entityToTypeToExistingComponentIDs[componentMessage.entity].Add(typeof(TComponent), new HashSet<Guid>()); entityToTypeToExistingComponentIDs[componentMessage.entity].Add(typeof(TComponent), new PooledSet<Guid>());
} }
entityToTypeToExistingComponentIDs[componentMessage.entity][typeof(TComponent)].Add(componentMessage.componentID); entityToTypeToExistingComponentIDs[componentMessage.entity][typeof(TComponent)].Add(componentMessage.componentID);
@ -112,7 +124,7 @@ namespace Encompass
if (!entityToTypeToPendingComponentIDs[pendingComponentMessage.entity].ContainsKey(typeof(TComponent))) if (!entityToTypeToPendingComponentIDs[pendingComponentMessage.entity].ContainsKey(typeof(TComponent)))
{ {
entityToTypeToPendingComponentIDs[pendingComponentMessage.entity].Add(typeof(TComponent), new HashSet<Guid>()); entityToTypeToPendingComponentIDs[pendingComponentMessage.entity].Add(typeof(TComponent), new PooledSet<Guid>());
} }
entityToTypeToPendingComponentIDs[pendingComponentMessage.entity][typeof(TComponent)].Add(pendingComponentMessage.componentID); entityToTypeToPendingComponentIDs[pendingComponentMessage.entity][typeof(TComponent)].Add(pendingComponentMessage.componentID);
@ -130,7 +142,7 @@ namespace Encompass
if (!entityToTypeToComponentIDs[entity].ContainsKey(typeof(TComponent))) if (!entityToTypeToComponentIDs[entity].ContainsKey(typeof(TComponent)))
{ {
entityToTypeToComponentIDs[entity].Add(typeof(TComponent), new HashSet<Guid>()); entityToTypeToComponentIDs[entity].Add(typeof(TComponent), new PooledSet<Guid>());
} }
entityToTypeToComponentIDs[entity][typeof(TComponent)].Add(componentID); entityToTypeToComponentIDs[entity][typeof(TComponent)].Add(componentID);
@ -227,7 +239,7 @@ namespace Encompass
internal IEnumerable<(Guid, TComponent)> ReadExistingAndPendingComponentsByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent internal IEnumerable<(Guid, TComponent)> ReadExistingAndPendingComponentsByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; PooledSet<Guid> idSet;
if (entityToTypeToComponentIDs.ContainsKey(entity) && entityToTypeToComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet)) if (entityToTypeToComponentIDs.ContainsKey(entity) && entityToTypeToComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Select(id => (id, (TComponent)componentIDToComponent[id])); return idSet.Select(id => (id, (TComponent)componentIDToComponent[id]));
@ -238,7 +250,7 @@ namespace Encompass
internal IEnumerable<(Guid, TComponent)> ReadExistingComponentsByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent internal IEnumerable<(Guid, TComponent)> ReadExistingComponentsByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; PooledSet<Guid> idSet;
if (entityToTypeToExistingComponentIDs.ContainsKey(entity) && entityToTypeToExistingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet)) if (entityToTypeToExistingComponentIDs.ContainsKey(entity) && entityToTypeToExistingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Select(id => (id, (TComponent)componentIDToComponent[id])); return idSet.Select(id => (id, (TComponent)componentIDToComponent[id]));
@ -249,7 +261,7 @@ namespace Encompass
internal IEnumerable<(Guid, TComponent)> ReadPendingComponentsByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent internal IEnumerable<(Guid, TComponent)> ReadPendingComponentsByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; PooledSet<Guid> idSet;
if (entityToTypeToPendingComponentIDs.ContainsKey(entity) && entityToTypeToPendingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet)) if (entityToTypeToPendingComponentIDs.ContainsKey(entity) && entityToTypeToPendingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Select(id => (id, (TComponent)componentIDToComponent[id])); return idSet.Select(id => (id, (TComponent)componentIDToComponent[id]));
@ -279,7 +291,7 @@ namespace Encompass
internal bool HasExistingOrPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent internal bool HasExistingOrPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; PooledSet<Guid> idSet;
if (entityToTypeToComponentIDs.TryGetValue(entity, out _) && entityToTypeToComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet)) if (entityToTypeToComponentIDs.TryGetValue(entity, out _) && entityToTypeToComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Count > 0; return idSet.Count > 0;
@ -290,7 +302,7 @@ namespace Encompass
internal bool HasExistingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent internal bool HasExistingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; PooledSet<Guid> idSet;
if (entityToTypeToExistingComponentIDs.TryGetValue(entity, out _) && entityToTypeToExistingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet)) if (entityToTypeToExistingComponentIDs.TryGetValue(entity, out _) && entityToTypeToExistingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Count > 0; return idSet.Count > 0;
@ -301,7 +313,7 @@ namespace Encompass
internal bool HasPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent internal bool HasPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; PooledSet<Guid> idSet;
if (entityToTypeToPendingComponentIDs.TryGetValue(entity, out _) && entityToTypeToPendingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet)) if (entityToTypeToPendingComponentIDs.TryGetValue(entity, out _) && entityToTypeToPendingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Count > 0; return idSet.Count > 0;