sparse set

storage_conversion
cosmonaut 2023-12-19 15:31:10 -08:00
parent cbdcca5bbf
commit 94c195e920
5 changed files with 63 additions and 47 deletions

View File

@ -8,7 +8,7 @@ public unsafe class NativeArray<T> : IDisposable where T : unmanaged
{ {
private T* Elements; private T* Elements;
public int Count { get; private set; } public int Count { get; private set; }
private int Capacity; public int Capacity { get; private set; }
private int ElementSize; private int ElementSize;
public Span<T> ToSpan() => new Span<T>(Elements, Count); public Span<T> ToSpan() => new Span<T>(Elements, Count);

View File

@ -1,4 +1,6 @@
using System; using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using MoonTools.ECS.Collections; using MoonTools.ECS.Collections;
namespace MoonTools.ECS; namespace MoonTools.ECS;
@ -6,36 +8,34 @@ namespace MoonTools.ECS;
public class ComponentStorage : IDisposable public class ComponentStorage : IDisposable
{ {
internal readonly NativeArray<Entity> DenseArray = new NativeArray<Entity>(); internal readonly NativeArray<Entity> DenseArray = new NativeArray<Entity>();
internal readonly NativeArray<Entity> SparseArray = new NativeArray<Entity>(); internal readonly NativeArray<int> SparseArray = new NativeArray<int>();
internal readonly NativeArray ElementArray; internal nint ElementArray;
internal int ElementArrayCapacity;
private bool IsDisposed; private bool IsDisposed;
public int ElementSize { get; private set; } public int ElementSize { get; private set; }
public int Count => DenseArray.Count; public int Count => DenseArray.Count;
public ComponentStorage(int elementSize) public unsafe ComponentStorage(int elementSize)
{ {
for (var i = 0; i < 16; i += 1) for (var i = 0; i < 16; i += 1)
{ {
SparseArray.Append(Entity.Null); // sentinel value SparseArray.Append(Entity.Null.ID); // sentinel value
} }
ElementArray = new NativeArray(elementSize); ElementArrayCapacity = 16;
ElementArray = (nint) NativeMemory.Alloc((nuint) (elementSize * ElementArrayCapacity));
ElementSize = elementSize; ElementSize = elementSize;
} }
public bool Any() => DenseArray.Count > 0; public bool Any() => DenseArray.Count > 0;
public ref T Get<T>(Entity entity) where T : unmanaged public unsafe ref T Get<T>(Entity entity) where T : unmanaged
{ {
if (entity.ID >= ElementArray.Capacity) return ref ((T*) ElementArray)[SparseArray[entity.ID]];
{
throw new Exception("oh noes");
}
return ref ElementArray.Get<T>(entity.ID);
} }
public ref T GetFirst<T>() where T : unmanaged public unsafe ref T GetFirst<T>() where T : unmanaged
{ {
#if DEBUG #if DEBUG
if (DenseArray.Count == 0) if (DenseArray.Count == 0)
@ -43,12 +43,12 @@ public class ComponentStorage : IDisposable
throw new IndexOutOfRangeException("Component storage is empty!"); throw new IndexOutOfRangeException("Component storage is empty!");
} }
#endif #endif
return ref ElementArray.Get<T>(DenseArray[0].ID); return ref ((T*) ElementArray)[0];
} }
public bool Set<T>(Entity entity, T component) where T : unmanaged public unsafe bool Set<T>(Entity entity, T component) where T : unmanaged
{ {
var newEntity = SparseArray[entity.ID] == Entity.Null; var newEntity = entity.ID >= SparseArray.Count || SparseArray[entity.ID] == Entity.Null.ID;
if (newEntity) if (newEntity)
{ {
// the entity is being added! let's do some record keeping // the entity is being added! let's do some record keeping
@ -58,40 +58,47 @@ public class ComponentStorage : IDisposable
{ {
var oldCount = SparseArray.Count; var oldCount = SparseArray.Count;
SparseArray.ResizeTo(entity.ID + 1); SparseArray.ResizeTo(entity.ID + 1);
for (var i = oldCount; i < SparseArray.Count; i += 1) for (var i = oldCount; i < SparseArray.Capacity; i += 1)
{ {
SparseArray.Append(Entity.Null); // sentinel value SparseArray.Append(Entity.Null.ID); // sentinel value
} }
} }
SparseArray[entity.ID] = entity; SparseArray[entity.ID] = index;
// FIXME: something is not right here if (entity.ID >= ElementArrayCapacity)
if (entity.ID >= ElementArray.Count)
{ {
ElementArray.ResizeTo(entity.ID + 1); ElementArrayCapacity = entity.ID + 1;
ElementArray = (nint) NativeMemory.Realloc((void*) ElementArray, (nuint) (ElementArrayCapacity * ElementSize));
} }
} }
ElementArray.Set(entity.ID, component); Unsafe.Write((void*) (ElementArray + ElementSize * SparseArray[entity.ID]), component);
return !newEntity; return !newEntity;
} }
public bool Has(Entity entity) public bool Has(Entity entity)
{ {
return SparseArray[entity.ID] != Entity.Null; return entity.ID < SparseArray.Count && SparseArray[entity.ID] != Entity.Null.ID;
} }
public bool Remove(Entity entity) public unsafe bool Remove(Entity entity)
{ {
if (Has(entity)) if (Has(entity))
{ {
var denseIndex = SparseArray[entity.ID]; var denseIndex = SparseArray[entity.ID];
var lastItem = DenseArray[DenseArray.Count - 1]; var lastItem = DenseArray[DenseArray.Count - 1];
DenseArray[denseIndex.ID] = lastItem; DenseArray[denseIndex] = lastItem;
SparseArray[lastItem.ID] = denseIndex; SparseArray[lastItem.ID] = denseIndex;
SparseArray[entity.ID] = Entity.Null; // sentinel value SparseArray[entity.ID] = Entity.Null.ID; // sentinel value
if (denseIndex != DenseArray.Count - 1)
{
NativeMemory.Copy((void*) (ElementArray + ElementSize * (DenseArray.Count - 1)), (void*) (ElementArray + ElementSize * denseIndex), (nuint) ElementSize);
}
DenseArray.RemoveLastElement(); DenseArray.RemoveLastElement();
ElementArray.RemoveLastElement();
return true; return true;
} }
@ -102,10 +109,9 @@ public class ComponentStorage : IDisposable
public void Clear() public void Clear()
{ {
DenseArray.Clear(); DenseArray.Clear();
ElementArray.Clear(); for (var i = 0; i < SparseArray.Capacity; i += 1)
for (var i = 0; i < SparseArray.Count; i += 1)
{ {
SparseArray[i] = Entity.Null; SparseArray[i] = Entity.Null.ID;
} }
} }
@ -127,7 +133,7 @@ public class ComponentStorage : IDisposable
} }
#endif #endif
protected virtual void Dispose(bool disposing) protected unsafe virtual void Dispose(bool disposing)
{ {
if (!IsDisposed) if (!IsDisposed)
{ {
@ -135,9 +141,10 @@ public class ComponentStorage : IDisposable
{ {
DenseArray.Dispose(); DenseArray.Dispose();
SparseArray.Dispose(); SparseArray.Dispose();
ElementArray.Dispose();
} }
NativeMemory.Free((void*) ElementArray);
IsDisposed = true; IsDisposed = true;
} }
} }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using MoonTools.ECS.Collections; using MoonTools.ECS.Collections;
namespace MoonTools.ECS; namespace MoonTools.ECS;
@ -206,34 +207,40 @@ public class Snapshot : IDisposable
private class ComponentSnapshot : IDisposable private class ComponentSnapshot : IDisposable
{ {
private readonly NativeArray<Entity> DenseArray = new NativeArray<Entity>(); private readonly NativeArray<Entity> DenseArray;
private readonly NativeArray<Entity> SparseArray = new NativeArray<Entity>(); private readonly NativeArray<int> SparseArray;
private readonly NativeArray ElementArray; private nint ElementArray;
private int ElementArrayCapacity;
private bool IsDisposed; private bool IsDisposed;
public ComponentSnapshot(int elementSize) public unsafe ComponentSnapshot(int elementSize)
{ {
ElementArray = new NativeArray(elementSize); ElementArray = (nint) NativeMemory.Alloc((nuint) (16 * elementSize));
DenseArray = new NativeArray<Entity>(elementSize); DenseArray = new NativeArray<Entity>(elementSize);
SparseArray = new NativeArray<Entity>(elementSize); SparseArray = new NativeArray<int>(elementSize);
} }
public void Take(ComponentStorage componentStorage) public unsafe void Take(ComponentStorage componentStorage)
{ {
componentStorage.DenseArray.CopyTo(DenseArray); componentStorage.DenseArray.CopyTo(DenseArray);
componentStorage.SparseArray.CopyTo(SparseArray); componentStorage.SparseArray.CopyTo(SparseArray);
componentStorage.ElementArray.CopyAllTo(ElementArray); if (componentStorage.ElementArrayCapacity > ElementArrayCapacity)
{
ElementArrayCapacity = componentStorage.ElementArrayCapacity;
NativeMemory.Realloc((void*) ElementArray, (nuint) (componentStorage.ElementSize * ElementArrayCapacity));
}
NativeMemory.Copy((void*) componentStorage.ElementArray, (void*) ElementArray, (nuint) (ElementArrayCapacity * componentStorage.ElementSize));
} }
public void Restore(ComponentStorage componentStorage) public unsafe void Restore(ComponentStorage componentStorage)
{ {
DenseArray.CopyTo(componentStorage.DenseArray); DenseArray.CopyTo(componentStorage.DenseArray);
SparseArray.CopyTo(componentStorage.SparseArray); SparseArray.CopyTo(componentStorage.SparseArray);
ElementArray.CopyAllTo(componentStorage.ElementArray); NativeMemory.Copy((void*) ElementArray, (void*) componentStorage.ElementArray, (nuint) (ElementArrayCapacity * componentStorage.ElementSize));
} }
protected virtual void Dispose(bool disposing) protected unsafe virtual void Dispose(bool disposing)
{ {
if (!IsDisposed) if (!IsDisposed)
{ {
@ -241,9 +248,10 @@ public class Snapshot : IDisposable
{ {
DenseArray.Dispose(); DenseArray.Dispose();
SparseArray.Dispose(); SparseArray.Dispose();
ElementArray.Dispose();
} }
NativeMemory.Free((void*) ElementArray);
IsDisposed = true; IsDisposed = true;
} }
} }

View File

@ -36,6 +36,7 @@ public class ComponentTypeIdAssigner<T> : ComponentTypeIdAssigner
#if DEBUG #if DEBUG
World.ComponentTypeToId[typeof(T)] = new TypeId(Id); World.ComponentTypeToId[typeof(T)] = new TypeId(Id);
World.ComponentTypeIdToType.Add(typeof(T));
#endif #endif
} }
} }

View File

@ -148,12 +148,12 @@ public class World : IDisposable
public bool Has<T>(in Entity entity) where T : unmanaged public bool Has<T>(in Entity entity) where T : unmanaged
{ {
return EntityComponentIndex[entity].Contains(new TypeId(ComponentTypeIdAssigner<T>.Id)); return GetComponentStorage<T>().Has(entity);
} }
internal bool Has(in Entity entity, in TypeId typeId) internal bool Has(in Entity entity, in TypeId typeId)
{ {
return EntityComponentIndex[entity].Contains(typeId); return ComponentIndex[(int) typeId.Value].Has(entity);
} }
public bool Some<T>() where T : unmanaged public bool Some<T>() where T : unmanaged