snapshot system

rev2
cosmonaut 2023-10-24 18:44:41 -07:00
parent b3ff7e3f1c
commit 5419fbd72d
12 changed files with 300 additions and 90 deletions

View File

@ -1,24 +1,38 @@
using System.Collections.Generic;
namespace MoonTools.ECS.Rev2
namespace MoonTools.ECS.Rev2;
internal class Archetype
{
public class Archetype
public World World;
public ArchetypeSignature Signature;
public List<Column> ComponentColumns = new List<Column>();
public List<EntityId> RowToEntity = new List<EntityId>();
public Dictionary<ComponentId, int> ComponentToColumnIndex =
new Dictionary<ComponentId, int>();
public SortedDictionary<ComponentId, ArchetypeEdge> Edges = new SortedDictionary<ComponentId, ArchetypeEdge>();
public int Count => RowToEntity.Count;
public Archetype(World world, ArchetypeSignature signature)
{
public ArchetypeSignature Signature;
public ArchetypeId Id { get; private set; }
public List<Column> Components = new List<Column>();
public List<EntityId> RowToEntity = new List<EntityId>();
World = world;
Signature = signature;
}
public Dictionary<ComponentId, int> ComponentToColumnIndex =
new Dictionary<ComponentId, int>();
public SortedDictionary<ComponentId, ArchetypeEdge> Edges = new SortedDictionary<ComponentId, ArchetypeEdge>();
public int Count => RowToEntity.Count;
public Archetype(ArchetypeId id, ArchetypeSignature signature)
public void ClearAll()
{
for (int i = 0; i < ComponentColumns.Count; i += 1)
{
Id = id;
Signature = signature;
ComponentColumns[i].Count = 0;
}
foreach (var entityId in RowToEntity)
{
World.FreeEntity(entityId);
}
RowToEntity.Clear();
}
}

View File

@ -1,4 +1,4 @@
namespace MoonTools.ECS.Rev2
{
public readonly record struct ArchetypeEdge(Archetype Add, Archetype Remove);
internal readonly record struct ArchetypeEdge(Archetype Add, Archetype Remove);
}

View File

@ -1,4 +0,0 @@
namespace MoonTools.ECS.Rev2
{
public readonly record struct ArchetypeId(int Id) : IHasId;
}

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
namespace MoonTools.ECS.Rev2
{
public class ArchetypeSignature : IEquatable<ArchetypeSignature>
internal class ArchetypeSignature : IEquatable<ArchetypeSignature>
{
public static ArchetypeSignature Empty = new ArchetypeSignature(0);

View File

@ -3,12 +3,13 @@ using System.Runtime.InteropServices;
namespace MoonTools.ECS.Rev2
{
public unsafe class Column : IDisposable
internal unsafe class Column : IDisposable
{
public nint Elements;
public int ElementSize;
public int Count;
public int Capacity;
private int Capacity;
private readonly int ElementSize;
private bool IsDisposed;
@ -27,6 +28,12 @@ namespace MoonTools.ECS.Rev2
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
}
private void ResizeTo(int capacity)
{
Capacity = capacity;
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
}
// Fills gap by copying final element to the deleted index
public void Delete(int index)
{
@ -53,7 +60,7 @@ namespace MoonTools.ECS.Rev2
Count += 1;
}
public void CopyToEnd(int index, Column other)
public void CopyElementToEnd(int index, Column other)
{
if (other.Count >= other.Capacity)
{
@ -69,6 +76,22 @@ namespace MoonTools.ECS.Rev2
other.Count += 1;
}
public void CopyAllTo(Column other)
{
if (Count >= other.Capacity)
{
other.ResizeTo(Count);
}
NativeMemory.Copy(
(void*) Elements,
(void*) other.Elements,
(nuint) (ElementSize * Count)
);
other.Count = Count;
}
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)

View File

@ -2,7 +2,7 @@ using System;
namespace MoonTools.ECS.Rev2
{
public readonly record struct ComponentId(int Id) : IHasId, IComparable<ComponentId>
internal readonly record struct ComponentId(int Id) : IHasId, IComparable<ComponentId>
{
public int CompareTo(ComponentId other)
{

View File

@ -47,6 +47,15 @@ namespace MoonTools.ECS.Rev2
}
}
public EntityId RandomEntity
{
get
{
var randomIndex = RandomManager.Next(Count);
return NthEntity(randomIndex);
}
}
// WARNING: this WILL crash if the index is out of range!
public EntityId NthEntity(int index)
{
@ -66,16 +75,25 @@ namespace MoonTools.ECS.Rev2
throw new InvalidOperationException("Filter index out of range!");
}
public EntityId RandomEntity
public void DestroyAllEntities()
{
get
foreach (var archetype in Archetypes)
{
var randomIndex = RandomManager.Next(Count);
return NthEntity(randomIndex);
archetype.ClearAll();
}
}
public Filter(Archetype emptyArchetype, HashSet<ComponentId> included, HashSet<ComponentId> excluded)
public void TakeSnapshot(Snapshot snapshot)
{
snapshot.Reset();
foreach (var archetype in Archetypes)
{
snapshot.TakeArchetypeSnapshot(archetype);
}
}
internal Filter(Archetype emptyArchetype, HashSet<ComponentId> included, HashSet<ComponentId> excluded)
{
EmptyArchetype = emptyArchetype;
Included = included;
@ -85,7 +103,6 @@ namespace MoonTools.ECS.Rev2
internal ref struct ArchetypeEnumerator
{
private Archetype CurrentArchetype;
private bool Active;
// TODO: pool these
private Queue<Archetype> ArchetypeQueue = new Queue<Archetype>();
@ -96,8 +113,6 @@ namespace MoonTools.ECS.Rev2
public ArchetypeEnumerator(Filter filter)
{
Active = false;
var empty = filter.EmptyArchetype;
ArchetypeSearchQueue.Enqueue(empty);
@ -127,27 +142,14 @@ namespace MoonTools.ECS.Rev2
ArchetypeQueue.Enqueue(current);
}
// if the current archetype satisfies the filter, we need to add all edges that
// do not have an excluded component
// if the current archetype does not satisfy the filter, we need to add all edges that
// include an included component
// breadth-first search
// ignore excluded component edges
foreach (var (componentId, edge) in current.Edges)
{
if (satisfiesFilter)
if (!Explored.Contains(edge.Add) && !filter.Excluded.Contains(componentId))
{
if (!filter.Excluded.Contains(componentId) && !Explored.Contains(edge.Add))
{
Explored.Add(edge.Add);
ArchetypeSearchQueue.Enqueue(edge.Add);
}
}
else
{
if (filter.Included.Contains(componentId) && !Explored.Contains(edge.Add))
{
Explored.Add(edge.Add);
ArchetypeSearchQueue.Enqueue(edge.Add);
}
Explored.Add(edge.Add);
ArchetypeSearchQueue.Enqueue(edge.Add);
}
}
}

42
src/Rev2/FilterBuilder.cs Normal file
View File

@ -0,0 +1,42 @@
using System.Collections.Generic;
namespace MoonTools.ECS.Rev2
{
public ref struct FilterBuilder
{
World World;
HashSet<ComponentId> Included;
HashSet<ComponentId> Excluded;
internal FilterBuilder(World world)
{
World = world;
Included = new HashSet<ComponentId>();
Excluded = new HashSet<ComponentId>();
}
private FilterBuilder(World world, HashSet<ComponentId> included, HashSet<ComponentId> excluded)
{
World = world;
Included = included;
Excluded = excluded;
}
public FilterBuilder Include<T>() where T : unmanaged
{
Included.Add(World.TypeToComponentId[typeof(T)]);
return new FilterBuilder(World, Included, Excluded);
}
public FilterBuilder Exclude<T>() where T : unmanaged
{
Excluded.Add(World.TypeToComponentId[typeof(T)]);
return new FilterBuilder(World, Included, Excluded);
}
public Filter Build()
{
return new Filter(World.EmptyArchetype, Included, Excluded);
}
}
}

View File

@ -2,7 +2,7 @@ using System.Collections.Generic;
namespace MoonTools.ECS.Rev2
{
public class IdAssigner<T> where T : struct, IHasId
internal class IdAssigner<T> where T : struct, IHasId
{
int Next;
Queue<int> AvailableIds = new Queue<int>();

View File

@ -1,4 +1,4 @@
namespace MoonTools.ECS.Rev2
{
public readonly record struct Record(Archetype Archetype, int Row);
internal readonly record struct Record(Archetype Archetype, int Row);
}

110
src/Rev2/Snapshot.cs Normal file
View File

@ -0,0 +1,110 @@
using System.Collections.Generic;
namespace MoonTools.ECS.Rev2;
public class Snapshot
{
private Dictionary<ArchetypeSignature, ArchetypeSnapshot> ArchetypeSnapshots =
new Dictionary<ArchetypeSignature, ArchetypeSnapshot>();
public int Count
{
get
{
var count = 0;
foreach (var snapshot in ArchetypeSnapshots.Values)
{
count += snapshot.Count;
}
return count;
}
}
public void Restore(World world)
{
foreach (var (archetypeSignature, archetypeSnapshot) in ArchetypeSnapshots)
{
var archetype = world.ArchetypeIndex[archetypeSignature];
RestoreArchetypeSnapshot(archetype);
}
}
internal void Reset()
{
foreach (var archetypeSnapshot in ArchetypeSnapshots.Values)
{
archetypeSnapshot.Count = 0;
}
}
internal void TakeArchetypeSnapshot(Archetype archetype)
{
if (!ArchetypeSnapshots.TryGetValue(archetype.Signature, out var archetypeSnapshot))
{
archetypeSnapshot = new ArchetypeSnapshot(archetype.Signature);
ArchetypeSnapshots.Add(archetype.Signature, archetypeSnapshot);
}
archetypeSnapshot.Take(archetype);
}
private void RestoreArchetypeSnapshot(Archetype archetype)
{
var archetypeSnapshot = ArchetypeSnapshots[archetype.Signature];
archetypeSnapshot.Restore(archetype);
}
private class ArchetypeSnapshot
{
public ArchetypeSignature Signature;
public readonly List<Column> ComponentColumns;
public int Count;
public ArchetypeSnapshot(ArchetypeSignature signature)
{
Signature = signature;
ComponentColumns = new List<Column>(signature.Count);
for (int i = 0; i < signature.Count; i += 1)
{
var componentId = signature[i];
ComponentColumns.Add(new Column(World.ElementSizes[componentId]));
}
}
public void Take(Archetype archetype)
{
for (int i = 0; i < ComponentColumns.Count; i += 1)
{
archetype.ComponentColumns[i].CopyAllTo(ComponentColumns[i]);
}
Count = archetype.Count;
}
public void Restore(Archetype archetype)
{
// Clear out existing entities
archetype.ClearAll();
// Copy all component data
for (int i = 0; i < ComponentColumns.Count; i += 1)
{
ComponentColumns[i].CopyAllTo(archetype.ComponentColumns[i]);
}
// Clear the row to entity list
archetype.RowToEntity.Clear();
// Create new entities and repopulate the row to entity list
for (int i = 0; i < Count; i += 1)
{
var entityId = archetype.World.CreateEntityOnArchetype(archetype);
archetype.RowToEntity.Add(entityId);
}
}
}
}

View File

@ -7,8 +7,13 @@ namespace MoonTools.ECS.Rev2
{
public class World : IDisposable
{
// Get ComponentId from a Type
internal static Dictionary<Type, ComponentId> TypeToComponentId = new Dictionary<Type, ComponentId>();
// Get element size from a ComponentId
internal static Dictionary<ComponentId, int> ElementSizes = new Dictionary<ComponentId, int>();
// Lookup from ArchetypeSignature to Archetype
Dictionary<ArchetypeSignature, Archetype> ArchetypeIndex = new Dictionary<ArchetypeSignature, Archetype>();
internal Dictionary<ArchetypeSignature, Archetype> ArchetypeIndex = new Dictionary<ArchetypeSignature, Archetype>();
// Going from EntityId to Archetype and storage row
Dictionary<EntityId, Record> EntityIndex = new Dictionary<EntityId, Record>();
@ -16,18 +21,13 @@ namespace MoonTools.ECS.Rev2
// Going from ComponentId to Archetype list
Dictionary<ComponentId, List<Archetype>> ComponentIndex = new Dictionary<ComponentId, List<Archetype>>();
// Get ComponentId from a Type
Dictionary<Type, ComponentId> TypeToComponentId = new Dictionary<Type, ComponentId>();
// Get element size from a ComponentId
Dictionary<ComponentId, int> ElementSizes = new Dictionary<ComponentId, int>();
// ID Management
IdAssigner<ArchetypeId> ArchetypeIdAssigner = new IdAssigner<ArchetypeId>();
IdAssigner<EntityId> EntityIdAssigner = new IdAssigner<EntityId>();
IdAssigner<ComponentId> ComponentIdAssigner = new IdAssigner<ComponentId>();
public readonly Archetype EmptyArchetype;
internal readonly Archetype EmptyArchetype;
public FilterBuilder FilterBuilder => new FilterBuilder(this);
private bool IsDisposed;
@ -39,12 +39,11 @@ namespace MoonTools.ECS.Rev2
EmptyArchetype = CreateArchetype(ArchetypeSignature.Empty);
}
private Archetype CreateArchetype(ArchetypeSignature signature)
internal Archetype CreateArchetype(ArchetypeSignature signature)
{
var archetypeId = ArchetypeIdAssigner.Assign();
var archetype = new Archetype(archetypeId, signature)
var archetype = new Archetype(this, signature)
{
Components = new List<Column>(signature.Count)
ComponentColumns = new List<Column>(signature.Count)
};
ArchetypeIndex.Add(signature, archetype);
@ -52,9 +51,9 @@ namespace MoonTools.ECS.Rev2
for (int i = 0; i < signature.Count; i += 1)
{
var componentId = signature[i];
ComponentIndex[componentId].Add(archetype); //, new ArchetypeRecord(i));
archetype.ComponentToColumnIndex.Add(componentId, archetype.Components.Count);
archetype.Components.Add(new Column(ElementSizes[componentId]));
ComponentIndex[componentId].Add(archetype);
archetype.ComponentToColumnIndex.Add(componentId, archetype.ComponentColumns.Count);
archetype.ComponentColumns.Add(new Column(ElementSizes[componentId]));
}
return archetype;
@ -63,12 +62,27 @@ namespace MoonTools.ECS.Rev2
public EntityId CreateEntity()
{
var entityId = EntityIdAssigner.Assign();
var emptyArchetype = ArchetypeIndex[ArchetypeSignature.Empty];
EntityIndex.Add(entityId, new Record(emptyArchetype, emptyArchetype.Count));
emptyArchetype.RowToEntity.Add(entityId);
EntityIndex.Add(entityId, new Record(EmptyArchetype, EmptyArchetype.Count));
EmptyArchetype.RowToEntity.Add(entityId);
return entityId;
}
// used as a fast path by Archetype.Transfer
internal EntityId CreateEntityOnArchetype(Archetype archetype)
{
var entityId = EntityIdAssigner.Assign();
EntityIndex.Add(entityId, new Record(archetype, archetype.Count));
archetype.RowToEntity.Add(entityId);
return entityId;
}
// used as a fast path by Archetype.ClearAll
internal void FreeEntity(EntityId entityId)
{
EntityIndex.Remove(entityId);
EntityIdAssigner.Unassign(entityId);
}
// FIXME: would be much more efficient to do all this at load time somehow
private void RegisterComponent<T>() where T : unmanaged
{
@ -80,12 +94,21 @@ namespace MoonTools.ECS.Rev2
private void TryRegisterComponentId<T>() where T : unmanaged
{
if (!TypeToComponentId.TryGetValue(typeof(T), out var componentId))
if (!TypeToComponentId.ContainsKey(typeof(T)))
{
RegisterComponent<T>();
}
}
// non-generic variant for use with Transfer
internal void AddComponentIndexEntry(ComponentId componentId)
{
if (!ComponentIndex.ContainsKey(componentId))
{
ComponentIndex.Add(componentId, new List<Archetype>());
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ComponentId GetComponentId<T>() where T : unmanaged
{
@ -106,7 +129,7 @@ namespace MoonTools.ECS.Rev2
var record = EntityIndex[entityId];
var columnIndex = record.Archetype.ComponentToColumnIndex[componentId];
var column = record.Archetype.Components[columnIndex];
var column = record.Archetype.ComponentColumns[columnIndex];
return ref ((T*) column.Elements)[record.Row];
}
@ -120,7 +143,7 @@ namespace MoonTools.ECS.Rev2
{
var record = EntityIndex[entityId];
var columnIndex = record.Archetype.ComponentToColumnIndex[componentId];
var column = record.Archetype.Components[columnIndex];
var column = record.Archetype.ComponentColumns[columnIndex];
((T*) column.Elements)[record.Row] = component;
}
@ -165,7 +188,7 @@ namespace MoonTools.ECS.Rev2
// add the new component to the new archetype
var columnIndex = nextArchetype.ComponentToColumnIndex[componentId];
var column = nextArchetype.Components[columnIndex];
var column = nextArchetype.ComponentColumns[columnIndex];
column.Append(component);
}
@ -207,9 +230,9 @@ namespace MoonTools.ECS.Rev2
var archetype = record.Archetype;
var row = record.Row;
for (int i = 0; i < archetype.Components.Count; i += 1)
for (int i = 0; i < archetype.ComponentColumns.Count; i += 1)
{
archetype.Components[i].Delete(row);
archetype.ComponentColumns[i].Delete(row);
}
if (row != archetype.Count - 1)
@ -227,16 +250,16 @@ namespace MoonTools.ECS.Rev2
private void MoveEntityToHigherArchetype(EntityId entityId, int row, Archetype from, Archetype to)
{
for (int i = 0; i < from.Components.Count; i += 1)
for (int i = 0; i < from.ComponentColumns.Count; i += 1)
{
var componentId = from.Signature[i];
var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
// copy all components to higher archetype
from.Components[i].CopyToEnd(row, to.Components[destinationColumnIndex]);
from.ComponentColumns[i].CopyElementToEnd(row, to.ComponentColumns[destinationColumnIndex]);
// delete row on from archetype
from.Components[i].Delete(row);
from.ComponentColumns[i].Delete(row);
}
if (row != from.Count - 1)
@ -256,18 +279,18 @@ namespace MoonTools.ECS.Rev2
private void MoveEntityToLowerArchetype(EntityId entityId, int row, Archetype from, Archetype to, ComponentId removed)
{
for (int i = 0; i < from.Components.Count; i += 1)
for (int i = 0; i < from.ComponentColumns.Count; i += 1)
{
var componentId = from.Signature[i];
// delete the row
from.Components[i].Delete(row);
from.ComponentColumns[i].Delete(row);
// if this isn't the removed component, copy to the lower archetype
if (componentId != removed)
{
var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
from.Components[i].CopyToEnd(row, to.Components[destinationColumnIndex]);
from.ComponentColumns[i].CopyElementToEnd(row, to.ComponentColumns[destinationColumnIndex]);
}
}
@ -293,11 +316,11 @@ namespace MoonTools.ECS.Rev2
{
var componentIdOne = archetype.Signature[0];
var columnIndexOne = archetype.ComponentToColumnIndex[componentIdOne];
var columnOneElements = archetype.Components[columnIndexOne].Elements;
var columnOneElements = archetype.ComponentColumns[columnIndexOne].Elements;
var componentIdTwo = archetype.Signature[1];
var columnIndexTwo = archetype.ComponentToColumnIndex[componentIdTwo];
var columnTwoElements = archetype.Components[columnIndexTwo].Elements;
var columnTwoElements = archetype.ComponentColumns[columnIndexTwo].Elements;
for (int i = archetype.Count - 1; i >= 0; i -= 1)
{
@ -312,11 +335,11 @@ namespace MoonTools.ECS.Rev2
{
var componentIdOne = archetype.Signature[0];
var columnIndexOne = archetype.ComponentToColumnIndex[componentIdOne];
var columnOneElements = archetype.Components[columnIndexOne].Elements;
var columnOneElements = archetype.ComponentColumns[columnIndexOne].Elements;
var componentIdTwo = archetype.Signature[1];
var columnIndexTwo = archetype.ComponentToColumnIndex[componentIdTwo];
var columnTwoElements = archetype.Components[columnIndexTwo].Elements;
var columnTwoElements = archetype.ComponentColumns[columnIndexTwo].Elements;
for (int i = archetype.Count - 1; i >= 0; i -= 1)
{
@ -336,7 +359,7 @@ namespace MoonTools.ECS.Rev2
{
for (var i = 0; i < archetype.Signature.Count; i += 1)
{
archetype.Components[i].Dispose();
archetype.ComponentColumns[i].Dispose();
}
}
}