more hashset pools

pull/5/head
Evan Hemsley 2019-08-09 19:29:08 -07:00
parent e808565342
commit fc36d0ace7
4 changed files with 21 additions and 40 deletions

View File

@ -12,7 +12,7 @@ namespace Encompass
private readonly Dictionary<Guid, Type> componentIDToType = new Dictionary<Guid, Type>(); private readonly Dictionary<Guid, Type> componentIDToType = new Dictionary<Guid, Type>();
private readonly Dictionary<Guid, IComponent> IDToComponent = new Dictionary<Guid, IComponent>(); private readonly Dictionary<Guid, IComponent> IDToComponent = new Dictionary<Guid, IComponent>();
private readonly Dictionary<Guid, HashSet<Guid>> entityIDToComponentIDs = new Dictionary<Guid, HashSet<Guid>>(); private readonly Dictionary<Guid, PooledSet<Guid>> entityIDToComponentIDs = new Dictionary<Guid, PooledSet<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, PooledSet<Guid>>> entityIDToComponentTypeToComponentIDs = new Dictionary<Guid, PooledDictionary<Type, PooledSet<Guid>>>(); private readonly Dictionary<Guid, PooledDictionary<Type, PooledSet<Guid>>> entityIDToComponentTypeToComponentIDs = new Dictionary<Guid, PooledDictionary<Type, PooledSet<Guid>>>();
@ -30,7 +30,7 @@ namespace Encompass
internal void RegisterEntity(Guid entityID) internal void RegisterEntity(Guid entityID)
{ {
entityIDToComponentIDs.Add(entityID, new HashSet<Guid>()); entityIDToComponentIDs.Add(entityID, new PooledSet<Guid>());
entityIDToComponentTypeToComponentIDs.Add(entityID, new PooledDictionary<Type, PooledSet<Guid>>()); entityIDToComponentTypeToComponentIDs.Add(entityID, new PooledDictionary<Type, PooledSet<Guid>>());
} }
@ -72,8 +72,7 @@ namespace Encompass
internal IEnumerable<Guid> GetComponentIDsByEntityID(Guid entityID) internal IEnumerable<Guid> GetComponentIDsByEntityID(Guid entityID)
{ {
HashSet<Guid> idSet; if (entityIDToComponentIDs.TryGetValue(entityID, out PooledSet<Guid> idSet))
if (entityIDToComponentIDs.TryGetValue(entityID, out idSet))
{ {
return idSet; return idSet;
} }
@ -82,8 +81,7 @@ namespace Encompass
internal IEnumerable<(Guid, TComponent)> GetComponentsByType<TComponent>() where TComponent : struct, IComponent internal IEnumerable<(Guid, TComponent)> GetComponentsByType<TComponent>() where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; if (typeToComponentIDs.TryGetValue(typeof(TComponent), out HashSet<Guid> idSet))
if (typeToComponentIDs.TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Select(id => (id, (TComponent)IDToComponent[id])); return idSet.Select(id => (id, (TComponent)IDToComponent[id]));
} }
@ -92,8 +90,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
{ {
PooledSet<Guid> idSet; if (entityIDToComponentTypeToComponentIDs.ContainsKey(entityID) && entityIDToComponentTypeToComponentIDs[entityID].TryGetValue(typeof(TComponent), out PooledSet<Guid> 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,8 +99,7 @@ namespace Encompass
internal bool EntityHasComponentOfType<TComponent>(Guid entityID) where TComponent : struct, IComponent internal bool EntityHasComponentOfType<TComponent>(Guid entityID) where TComponent : struct, IComponent
{ {
PooledSet<Guid> idSet; if (entityIDToComponentTypeToComponentIDs.ContainsKey(entityID) && entityIDToComponentTypeToComponentIDs[entityID].TryGetValue(typeof(TComponent), out PooledSet<Guid> idSet))
if (entityIDToComponentTypeToComponentIDs.ContainsKey(entityID) && entityIDToComponentTypeToComponentIDs[entityID].TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Count > 0; return idSet.Count > 0;
} }
@ -113,8 +109,7 @@ namespace Encompass
internal bool ComponentOfTypeExists<TComponent>() where TComponent : struct, IComponent internal bool ComponentOfTypeExists<TComponent>() where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; if (typeToComponentIDs.TryGetValue(typeof(TComponent), out HashSet<Guid> idSet))
if (typeToComponentIDs.TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Count > 0; return idSet.Count > 0;
} }
@ -200,6 +195,7 @@ namespace Encompass
public void RegisterDestroyedEntity(Guid entityID) public void RegisterDestroyedEntity(Guid entityID)
{ {
entityIDToComponentIDs[entityID].Dispose();
entityIDToComponentIDs.Remove(entityID); entityIDToComponentIDs.Remove(entityID);
foreach (var set in entityIDToComponentTypeToComponentIDs[entityID].Values) foreach (var set in entityIDToComponentTypeToComponentIDs[entityID].Values)

View File

@ -152,8 +152,7 @@ namespace Encompass
internal IEnumerable<(Guid, TComponent)> ReadExistingAndPendingComponentsByType<TComponent>() where TComponent : struct, IComponent internal IEnumerable<(Guid, TComponent)> ReadExistingAndPendingComponentsByType<TComponent>() where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; if (componentMessageTypeToComponentIDs.TryGetValue(typeof(TComponent), out HashSet<Guid> idSet))
if (componentMessageTypeToComponentIDs.TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Select(id => (id, (TComponent)componentIDToComponent[id])); return idSet.Select(id => (id, (TComponent)componentIDToComponent[id]));
} }
@ -163,8 +162,7 @@ namespace Encompass
internal IEnumerable<(Guid, TComponent)> ReadExistingComponentsByType<TComponent>() where TComponent: struct, IComponent internal IEnumerable<(Guid, TComponent)> ReadExistingComponentsByType<TComponent>() where TComponent: struct, IComponent
{ {
HashSet<Guid> idSet; if (componentMessageTypeToExistingComponentIDs.TryGetValue(typeof(TComponent), out HashSet<Guid> idSet))
if (componentMessageTypeToExistingComponentIDs.TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Select(id => (id, (TComponent)componentIDToComponent[id])); return idSet.Select(id => (id, (TComponent)componentIDToComponent[id]));
} }
@ -174,8 +172,7 @@ namespace Encompass
internal IEnumerable<(Guid, TComponent)> ReadPendingComponentsByType<TComponent>() where TComponent : struct, IComponent internal IEnumerable<(Guid, TComponent)> ReadPendingComponentsByType<TComponent>() where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; if (componentMessageTypeToPendingComponentIDs.TryGetValue(typeof(TComponent), out HashSet<Guid> idSet))
if (componentMessageTypeToPendingComponentIDs.TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Select(id => (id, (TComponent)componentIDToComponent[id])); return idSet.Select(id => (id, (TComponent)componentIDToComponent[id]));
} }
@ -204,8 +201,7 @@ namespace Encompass
internal bool SomeExistingOrPendingComponent<TComponent>() where TComponent : struct, IComponent internal bool SomeExistingOrPendingComponent<TComponent>() where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; if (componentMessageTypeToComponentIDs.TryGetValue(typeof(TComponent), out HashSet<Guid> idSet))
if (componentMessageTypeToComponentIDs.TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Count > 0; return idSet.Count > 0;
} }
@ -215,8 +211,7 @@ namespace Encompass
internal bool SomeExistingComponent<TComponent>() where TComponent : struct, IComponent internal bool SomeExistingComponent<TComponent>() where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; if (componentMessageTypeToExistingComponentIDs.TryGetValue(typeof(TComponent), out HashSet<Guid> idSet))
if (componentMessageTypeToExistingComponentIDs.TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Count > 0; return idSet.Count > 0;
} }
@ -226,8 +221,7 @@ namespace Encompass
internal bool SomePendingComponent<TComponent>() where TComponent : struct, IComponent internal bool SomePendingComponent<TComponent>() where TComponent : struct, IComponent
{ {
HashSet<Guid> idSet; if (componentMessageTypeToPendingComponentIDs.TryGetValue(typeof(TComponent), out HashSet<Guid> idSet))
if (componentMessageTypeToPendingComponentIDs.TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Count > 0; return idSet.Count > 0;
} }
@ -239,8 +233,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
{ {
PooledSet<Guid> idSet; if (entityToTypeToComponentIDs.ContainsKey(entity) && entityToTypeToComponentIDs[entity].TryGetValue(typeof(TComponent), out PooledSet<Guid> 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]));
} }
@ -250,8 +243,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
{ {
PooledSet<Guid> idSet; if (entityToTypeToExistingComponentIDs.ContainsKey(entity) && entityToTypeToExistingComponentIDs[entity].TryGetValue(typeof(TComponent), out PooledSet<Guid> 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]));
} }
@ -261,8 +253,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
{ {
PooledSet<Guid> idSet; if (entityToTypeToPendingComponentIDs.ContainsKey(entity) && entityToTypeToPendingComponentIDs[entity].TryGetValue(typeof(TComponent), out PooledSet<Guid> 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]));
} }
@ -291,8 +282,7 @@ namespace Encompass
internal bool HasExistingOrPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent internal bool HasExistingOrPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
PooledSet<Guid> idSet; if (entityToTypeToComponentIDs.TryGetValue(entity, out _) && entityToTypeToComponentIDs[entity].TryGetValue(typeof(TComponent), out PooledSet<Guid> idSet))
if (entityToTypeToComponentIDs.TryGetValue(entity, out _) && entityToTypeToComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Count > 0; return idSet.Count > 0;
} }
@ -302,8 +292,7 @@ namespace Encompass
internal bool HasExistingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent internal bool HasExistingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
PooledSet<Guid> idSet; if (entityToTypeToExistingComponentIDs.TryGetValue(entity, out _) && entityToTypeToExistingComponentIDs[entity].TryGetValue(typeof(TComponent), out PooledSet<Guid> idSet))
if (entityToTypeToExistingComponentIDs.TryGetValue(entity, out _) && entityToTypeToExistingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Count > 0; return idSet.Count > 0;
} }
@ -313,8 +302,7 @@ namespace Encompass
internal bool HasPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent internal bool HasPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
PooledSet<Guid> idSet; if (entityToTypeToPendingComponentIDs.TryGetValue(entity, out _) && entityToTypeToPendingComponentIDs[entity].TryGetValue(typeof(TComponent), out PooledSet<Guid> idSet))
if (entityToTypeToPendingComponentIDs.TryGetValue(entity, out _) && entityToTypeToPendingComponentIDs[entity].TryGetValue(typeof(TComponent), out idSet))
{ {
return idSet.Count > 0; return idSet.Count > 0;
} }

View File

@ -6,19 +6,16 @@ namespace Encompass
{ {
internal class RenderManager internal class RenderManager
{ {
private readonly EntityManager entityManager;
private readonly ComponentManager componentManager; private readonly ComponentManager componentManager;
private readonly DrawLayerManager drawLayerManager; private readonly DrawLayerManager drawLayerManager;
private readonly Dictionary<Type, Action<Guid, IComponent>> drawComponentTypeToOrderedRenderer = new Dictionary<Type, Action<Guid, IComponent>>(); private readonly Dictionary<Type, Action<Guid, IComponent>> drawComponentTypeToOrderedRenderer = new Dictionary<Type, Action<Guid, IComponent>>();
public RenderManager( public RenderManager(
EntityManager entityManager,
ComponentManager componentManager, ComponentManager componentManager,
DrawLayerManager drawLayerManager DrawLayerManager drawLayerManager
) )
{ {
this.entityManager = entityManager;
this.componentManager = componentManager; this.componentManager = componentManager;
this.drawLayerManager = drawLayerManager; this.drawLayerManager = drawLayerManager;
} }

View File

@ -32,7 +32,7 @@ namespace Encompass
messageManager = new MessageManager(); messageManager = new MessageManager();
componentMessageManager = new ComponentMessageManager(); componentMessageManager = new ComponentMessageManager();
entityManager = new EntityManager(componentManager, componentMessageManager); entityManager = new EntityManager(componentManager, componentMessageManager);
renderManager = new RenderManager(entityManager, componentManager, drawLayerManager); renderManager = new RenderManager(componentManager, drawLayerManager);
} }
public Entity CreateEntity() public Entity CreateEntity()