id-preserving snapshots

rev2
cosmonaut 2023-10-30 12:11:50 -07:00
parent b118cffb83
commit 4d40103a6f
6 changed files with 129 additions and 79 deletions

View File

@ -9,17 +9,19 @@ namespace MoonTools.ECS.Collections
private T* Array; private T* Array;
private int count; private int count;
private int capacity; private int capacity;
private int elementSize;
public int Count => count; public int Count => count;
public ReverseSpanEnumerator<T> GetEnumerator() => new ReverseSpanEnumerator<T>(new Span<T>(Array, Count)); public Span<T>.Enumerator GetEnumerator() => new Span<T>(Array, count).GetEnumerator();
private bool disposed; private bool disposed;
public NativeArray(int capacity = 16) public NativeArray(int capacity = 16)
{ {
this.capacity = capacity; this.capacity = capacity;
Array = (T*) NativeMemory.Alloc((nuint) (capacity * Unsafe.SizeOf<T>())); elementSize = Unsafe.SizeOf<T>();
Array = (T*) NativeMemory.Alloc((nuint) (capacity * elementSize));
count = 0; count = 0;
} }
@ -37,11 +39,51 @@ namespace MoonTools.ECS.Collections
count += 1; count += 1;
} }
public void RemoveLastElement()
{
count -= 1;
}
public bool TryPop(out T element)
{
if (count > 0)
{
element = Array[count - 1];
count -= 1;
return true;
}
element = default;
return false;
}
public void Clear() public void Clear()
{ {
count = 0; count = 0;
} }
private void ResizeTo(int size)
{
capacity = size;
Array = (T*) NativeMemory.Realloc((void*) Array, (nuint) (elementSize * capacity));
}
public void CopyTo(NativeArray<T> other)
{
if (count >= other.capacity)
{
other.ResizeTo(Count);
}
NativeMemory.Copy(
(void*) Array,
(void*) other.Array,
(nuint) (elementSize * Count)
);
other.count = count;
}
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
if (!disposed) if (!disposed)

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using MoonTools.ECS.Collections;
namespace MoonTools.ECS.Rev2; namespace MoonTools.ECS.Rev2;
@ -6,8 +7,8 @@ internal class Archetype
{ {
public World World; public World World;
public ArchetypeSignature Signature; public ArchetypeSignature Signature;
public List<Column> ComponentColumns = new List<Column>(); public Column[] ComponentColumns;
public List<Id> RowToEntity = new List<Id>(); public NativeArray<Id> RowToEntity = new NativeArray<Id>();
public Dictionary<Id, int> ComponentToColumnIndex = public Dictionary<Id, int> ComponentToColumnIndex =
new Dictionary<Id, int>(); new Dictionary<Id, int>();
@ -19,11 +20,12 @@ internal class Archetype
{ {
World = world; World = world;
Signature = signature; Signature = signature;
ComponentColumns = new Column[signature.Count];
} }
public void ClearAll() public void ClearAll()
{ {
for (int i = 0; i < ComponentColumns.Count; i += 1) for (int i = 0; i < ComponentColumns.Length; i += 1)
{ {
ComponentColumns[i].Count = 0; ComponentColumns[i].Count = 0;
} }

View File

@ -83,16 +83,6 @@ namespace MoonTools.ECS.Rev2
} }
} }
public void TakeSnapshot(Snapshot snapshot)
{
snapshot.Reset();
foreach (var archetype in Archetypes)
{
snapshot.TakeArchetypeSnapshot(archetype);
}
}
internal Filter(Archetype emptyArchetype, HashSet<Id> included, HashSet<Id> excluded) internal Filter(Archetype emptyArchetype, HashSet<Id> included, HashSet<Id> excluded)
{ {
EmptyArchetype = emptyArchetype; EmptyArchetype = emptyArchetype;

View File

@ -1,26 +1,31 @@
using System.Collections.Generic; using MoonTools.ECS.Collections;
namespace MoonTools.ECS.Rev2 namespace MoonTools.ECS.Rev2;
internal class IdAssigner
{ {
internal class IdAssigner ulong Next;
NativeArray<ulong> AvailableIds = new NativeArray<ulong>();
public Id Assign()
{ {
ulong Next; if (!AvailableIds.TryPop(out var id))
Queue<ulong> AvailableIds = new Queue<ulong>();
public Id Assign()
{ {
if (!AvailableIds.TryDequeue(out var id)) id = Next;
{ Next += 1;
id = Next;
Next += 1;
}
return new Id(id);
} }
public void Unassign(Id id) return new Id(id);
{ }
AvailableIds.Enqueue(id.Value);
} public void Unassign(Id id)
{
AvailableIds.Add(id.Value);
}
public void CopyTo(IdAssigner other)
{
AvailableIds.CopyTo(other.AvailableIds);
other.Next = Next;
} }
} }

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using MoonTools.ECS.Collections;
namespace MoonTools.ECS.Rev2; namespace MoonTools.ECS.Rev2;
@ -7,6 +8,9 @@ public class Snapshot
private Dictionary<ArchetypeSignature, ArchetypeSnapshot> ArchetypeSnapshots = private Dictionary<ArchetypeSignature, ArchetypeSnapshot> ArchetypeSnapshots =
new Dictionary<ArchetypeSignature, ArchetypeSnapshot>(); new Dictionary<ArchetypeSignature, ArchetypeSnapshot>();
private Dictionary<Id, Record> EntityIndex = new Dictionary<Id, Record>();
private IdAssigner IdAssigner = new IdAssigner();
public int Count public int Count
{ {
get get
@ -24,18 +28,40 @@ public class Snapshot
public void Restore(World world) public void Restore(World world)
{ {
// restore archetype storage
foreach (var (archetypeSignature, archetypeSnapshot) in ArchetypeSnapshots) foreach (var (archetypeSignature, archetypeSnapshot) in ArchetypeSnapshots)
{ {
var archetype = world.ArchetypeIndex[archetypeSignature]; var archetype = world.ArchetypeIndex[archetypeSignature];
RestoreArchetypeSnapshot(archetype); RestoreArchetypeSnapshot(archetype);
} }
// restore entity index
world.EntityIndex.Clear();
foreach (var (id, record) in EntityIndex)
{
world.EntityIndex[id] = record;
}
// restore id assigner state
IdAssigner.CopyTo(world.IdAssigner);
} }
internal void Reset() public void Take(World world)
{ {
foreach (var archetypeSnapshot in ArchetypeSnapshots.Values) // copy id assigner state
world.IdAssigner.CopyTo(IdAssigner);
// copy entity index
EntityIndex.Clear();
foreach (var (id, record) in world.EntityIndex)
{ {
archetypeSnapshot.Count = 0; EntityIndex[id] = record;
}
// copy archetypes
foreach (var archetype in world.ArchetypeIndex.Values)
{
TakeArchetypeSnapshot(archetype);
} }
} }
@ -59,59 +85,48 @@ public class Snapshot
private class ArchetypeSnapshot private class ArchetypeSnapshot
{ {
public ArchetypeSignature Signature; public ArchetypeSignature Signature;
public readonly List<Column> ComponentColumns; private readonly Column[] ComponentColumns;
private readonly NativeArray<Id> RowToEntity;
public int Count; public int Count => RowToEntity.Count;
public ArchetypeSnapshot(ArchetypeSignature signature) public ArchetypeSnapshot(ArchetypeSignature signature)
{ {
Signature = signature; Signature = signature;
ComponentColumns = new List<Column>(signature.Count); ComponentColumns = new Column[signature.Count];
RowToEntity = new NativeArray<Id>();
for (int i = 0; i < signature.Count; i += 1) for (int i = 0; i < signature.Count; i += 1)
{ {
var componentId = signature[i]; var componentId = signature[i];
ComponentColumns.Add(new Column(World.ElementSizes[componentId])); ComponentColumns[i] = new Column(World.ElementSizes[componentId]);
} }
} }
public void Clear()
{
RowToEntity.Clear();
}
public void Take(Archetype archetype) public void Take(Archetype archetype)
{ {
for (int i = 0; i < ComponentColumns.Count; i += 1) for (int i = 0; i < ComponentColumns.Length; i += 1)
{ {
archetype.ComponentColumns[i].CopyAllTo(ComponentColumns[i]); archetype.ComponentColumns[i].CopyAllTo(ComponentColumns[i]);
} }
Count = archetype.Count; archetype.RowToEntity.CopyTo(RowToEntity);
} }
public void Restore(Archetype archetype) public void Restore(Archetype archetype)
{ {
// Copy all component data // Copy all component data
for (int i = 0; i < ComponentColumns.Count; i += 1) for (int i = 0; i < ComponentColumns.Length; i += 1)
{ {
ComponentColumns[i].CopyAllTo(archetype.ComponentColumns[i]); ComponentColumns[i].CopyAllTo(archetype.ComponentColumns[i]);
} }
var archetypeCount = archetype.Count; RowToEntity.CopyTo(archetype.RowToEntity);
if (Count < archetypeCount)
{
// if snapshot has fewer entities than archetype, remove extra entities
for (int i = archetypeCount - 1; i >= Count; i -= 1)
{
archetype.World.FreeEntity(archetype.RowToEntity[i]);
archetype.RowToEntity.RemoveAt(i);
}
}
else if (Count > archetypeCount)
{
// if snapshot has more entities than archetype, add entities
for (int i = archetypeCount; i < Count; i += 1)
{
archetype.World.CreateEntityOnArchetype(archetype);
}
}
} }
} }
} }

View File

@ -15,22 +15,22 @@ namespace MoonTools.ECS.Rev2
internal 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 // Going from EntityId to Archetype and storage row
Dictionary<Id, Record> EntityIndex = new Dictionary<Id, Record>(); internal Dictionary<Id, Record> EntityIndex = new Dictionary<Id, Record>();
// Going from ComponentId to Archetype list // Going from ComponentId to Archetype list
Dictionary<Id, List<Archetype>> ComponentIndex = new Dictionary<Id, List<Archetype>>(); Dictionary<Id, List<Archetype>> ComponentIndex = new Dictionary<Id, List<Archetype>>();
// ID Management // ID Management
IdAssigner IdAssigner = new IdAssigner(); internal IdAssigner IdAssigner = new IdAssigner();
internal readonly Archetype EmptyArchetype; internal readonly Archetype EmptyArchetype;
public FilterBuilder FilterBuilder => new FilterBuilder(this); public FilterBuilder FilterBuilder => new FilterBuilder(this);
private bool IsDisposed;
public delegate void RefAction<T1, T2>(ref T1 arg1, ref T2 arg2); public delegate void RefAction<T1, T2>(ref T1 arg1, ref T2 arg2);
private bool IsDisposed;
public World() public World()
{ {
// Create the Empty Archetype // Create the Empty Archetype
@ -39,10 +39,7 @@ namespace MoonTools.ECS.Rev2
internal Archetype CreateArchetype(ArchetypeSignature signature) internal Archetype CreateArchetype(ArchetypeSignature signature)
{ {
var archetype = new Archetype(this, signature) var archetype = new Archetype(this, signature);
{
ComponentColumns = new List<Column>(signature.Count)
};
ArchetypeIndex.Add(signature, archetype); ArchetypeIndex.Add(signature, archetype);
@ -50,8 +47,8 @@ namespace MoonTools.ECS.Rev2
{ {
var componentId = signature[i]; var componentId = signature[i];
ComponentIndex[componentId].Add(archetype); ComponentIndex[componentId].Add(archetype);
archetype.ComponentToColumnIndex.Add(componentId, archetype.ComponentColumns.Count); archetype.ComponentToColumnIndex.Add(componentId, i);
archetype.ComponentColumns.Add(new Column(ElementSizes[componentId])); archetype.ComponentColumns[i] = new Column(ElementSizes[componentId]);
} }
return archetype; return archetype;
@ -69,7 +66,6 @@ namespace MoonTools.ECS.Rev2
internal void CreateEntityOnArchetype(Archetype archetype) internal void CreateEntityOnArchetype(Archetype archetype)
{ {
var entityId = IdAssigner.Assign(); var entityId = IdAssigner.Assign();
EntityIndex.Add(entityId, new Record(archetype, archetype.Count));
archetype.RowToEntity.Add(entityId); archetype.RowToEntity.Add(entityId);
} }
@ -330,7 +326,7 @@ namespace MoonTools.ECS.Rev2
var archetype = record.Archetype; var archetype = record.Archetype;
var row = record.Row; var row = record.Row;
for (int i = 0; i < archetype.ComponentColumns.Count; i += 1) for (int i = 0; i < archetype.Signature.Count; i += 1)
{ {
archetype.ComponentColumns[i].Delete(row); archetype.ComponentColumns[i].Delete(row);
} }
@ -343,14 +339,14 @@ namespace MoonTools.ECS.Rev2
EntityIndex[lastRowEntity] = new Record(archetype, row); EntityIndex[lastRowEntity] = new Record(archetype, row);
} }
archetype.RowToEntity.RemoveAt(archetype.Count - 1); archetype.RowToEntity.RemoveLastElement();
EntityIndex.Remove(entityId); EntityIndex.Remove(entityId);
IdAssigner.Unassign(entityId); IdAssigner.Unassign(entityId);
} }
private void MoveEntityToHigherArchetype(Id entityId, int row, Archetype from, Archetype to) private void MoveEntityToHigherArchetype(Id entityId, int row, Archetype from, Archetype to)
{ {
for (int i = 0; i < from.ComponentColumns.Count; i += 1) for (int i = 0; i < from.Signature.Count; i += 1)
{ {
var componentId = from.Signature[i]; var componentId = from.Signature[i];
var destinationColumnIndex = to.ComponentToColumnIndex[componentId]; var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
@ -370,7 +366,7 @@ namespace MoonTools.ECS.Rev2
EntityIndex[lastRowEntity] = new Record(from, row); EntityIndex[lastRowEntity] = new Record(from, row);
} }
from.RowToEntity.RemoveAt(from.Count - 1); from.RowToEntity.RemoveLastElement();
// update row to entity lookup on to archetype // update row to entity lookup on to archetype
EntityIndex[entityId] = new Record(to, to.Count); EntityIndex[entityId] = new Record(to, to.Count);
@ -379,7 +375,7 @@ namespace MoonTools.ECS.Rev2
private void MoveEntityToLowerArchetype(Id entityId, int row, Archetype from, Archetype to, Id removed) private void MoveEntityToLowerArchetype(Id entityId, int row, Archetype from, Archetype to, Id removed)
{ {
for (int i = 0; i < from.ComponentColumns.Count; i += 1) for (int i = 0; i < from.Signature.Count; i += 1)
{ {
var componentId = from.Signature[i]; var componentId = from.Signature[i];
@ -402,7 +398,7 @@ namespace MoonTools.ECS.Rev2
EntityIndex[lastRowEntity] = new Record(from, row); EntityIndex[lastRowEntity] = new Record(from, row);
} }
from.RowToEntity.RemoveAt(from.Count - 1); from.RowToEntity.RemoveLastElement();
// update row to entity lookup on to archetype // update row to entity lookup on to archetype
EntityIndex[entityId] = new Record(to, to.Count); EntityIndex[entityId] = new Record(to, to.Count);