Class EntityPool
- Namespace
- KeenEyes
- Assembly
- KeenEyes.Core.dll
Manages entity ID recycling with version tracking. Reduces allocations by reusing entity IDs after entities are destroyed.
public sealed class EntityPool
- Inheritance
-
EntityPool
- Inherited Members
Remarks
When an entity is destroyed, its ID is returned to the pool for reuse. The version number is incremented to invalidate any stale handles to the previous entity that used that ID.
The pool uses a LIFO (stack) strategy for recycling, which improves cache locality since recently freed IDs are more likely to still be warm in cache.
This class is fully thread-safe and supports concurrent access from multiple threads. This enables parallel processing of systems that may allocate or release entities simultaneously.
Properties
ActiveCount
Gets the number of entities currently in use.
public int ActiveCount { get; }
Property Value
AvailableCount
Gets the number of entity IDs currently available for reuse.
public int AvailableCount { get; }
Property Value
RecycleCount
Gets the number of times entity IDs have been recycled.
public long RecycleCount { get; }
Property Value
TotalAllocated
Gets the total number of entity IDs that have been allocated.
public int TotalAllocated { get; }
Property Value
Methods
Acquire()
Acquires an entity ID, either from the recycled pool or by allocating a new one.
public Entity Acquire()
Returns
- Entity
A new entity with a unique ID and appropriate version.
Remarks
This method is thread-safe and can be called concurrently from multiple threads.
Clear()
Clears the pool, resetting all state.
public void Clear()
Remarks
This method is NOT thread-safe with respect to other operations. Ensure no other threads are accessing the pool during Clear.
GetVersion(int)
Gets the current version for an entity ID.
public int GetVersion(int entityId)
Parameters
entityIdintThe entity ID.
Returns
- int
The current version, or -1 if the ID is invalid.
Remarks
This method is thread-safe and can be called concurrently from multiple threads.
IsValid(Entity)
Checks if an entity is currently valid (alive).
public bool IsValid(Entity entity)
Parameters
entityEntityThe entity to check.
Returns
- bool
True if the entity is valid.
Remarks
This method is thread-safe and can be called concurrently from multiple threads.
Release(Entity)
Releases an entity ID back to the pool for reuse.
public bool Release(Entity entity)
Parameters
entityEntityThe entity to release.
Returns
- bool
True if the entity was released, false if it was already released or invalid.
Remarks
This method is thread-safe and can be called concurrently from multiple threads. If multiple threads attempt to release the same entity, only one will succeed.