Skip to content

Commit

Permalink
ECS - set EntityStore.nodes[] length = 128: length was too small for …
Browse files Browse the repository at this point in the history
…common use cases
  • Loading branch information
friflo committed Aug 15, 2024
1 parent 1c29643 commit 206f579
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/ECS/EntityStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ public EntityStore(PidType pidType)
intern = new Intern(pidType);
extension = new StoreExtension(pidType);
nodes = Array.Empty<EntityNode>();
EnsureNodesLength(2);
// length should not be too small to avoid multiple resizes of nodes[] for common use cases
EnsureNodesLength(128);
idBuffer = new int[1];
idBufferSet = new HashSet<int>();
recycleIds = true;
Expand Down
8 changes: 4 additions & 4 deletions src/Tests/ECS/Collections/Test_EntityList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ public static void Test_EntityList_SetStore()
public static void Test_EntityList_Add()
{
var store = new EntityStore();
AreEqual(2, store.Capacity);
AreEqual(128, store.Capacity);
// ReSharper disable once UseObjectOrCollectionInitializer
var list = new EntityList(store);
list.Add(0);
IsTrue(list[0].IsNull);
var e = Throws<ArgumentException>(() => {
list.Add(2);
list.Add(128);
});
AreEqual("id: 2. expect in [0, current max id: 1]", e!.Message);
AreEqual("id: 128. expect in [0, current max id: 127]", e!.Message);
e = Throws<ArgumentException>(() => {
list.Add(-1);
});
AreEqual("id: -1. expect in [0, current max id: 1]", e!.Message);
AreEqual("id: -1. expect in [0, current max id: 127]", e!.Message);
}

[Test]
Expand Down
10 changes: 5 additions & 5 deletions src/Tests/ECS/Entity/Test_Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static void Assert_GetEntityById()
var store = new EntityStore(PidType.RandomPids);
store.CreateEntity(2);

AreEqual(4, store.Capacity);
AreEqual(128, store.Capacity);

IsTrue (store.GetEntityById(0).IsNull);
IsTrue (store.GetEntityById(1).IsNull);
Expand All @@ -154,11 +154,11 @@ public static void Assert_GetEntityById()
var e = Throws<ArgumentException>(() => {
store.GetEntityById(-1);
});
AreEqual("id: -1. expect in [0, current max id: 3]", e!.Message);
AreEqual("id: -1. expect in [0, current max id: 127]", e!.Message);
e = Throws<ArgumentException>(() => {
store.GetEntityById(4);
store.GetEntityById(128);
});
AreEqual("id: 4. expect in [0, current max id: 3]", e!.Message);
AreEqual("id: 128. expect in [0, current max id: 127]", e!.Message);
}

[Test]
Expand All @@ -181,7 +181,7 @@ public static void Assert_TryGetEntityById()
var store = new EntityStore(PidType.RandomPids);
store.CreateEntity(2);

AreEqual(4, store.Capacity);
AreEqual(128, store.Capacity);

IsFalse(store.TryGetEntityById(-1, out Entity entity));
IsTrue (entity.IsNull);
Expand Down
18 changes: 11 additions & 7 deletions src/Tests/ECS/Entity/Test_StructHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ public static void Test_StructHeap_shrink_entity_capacity() // ENTITY_STRUCT
public static void Test_StructHeap_EntityStore_EnsureCapacity()
{
var store = new EntityStore(PidType.UsePidAsId);
Mem.AreEqual(1, store.EnsureCapacity(0)); // 1 => default capacity
store.CreateEntity();
Mem.AreEqual(0, store.EnsureCapacity(0));
var maxId = store.NodeMaxId;
Mem.AreEqual(maxId, store.EnsureCapacity(0)); // 1 => default capacity
var entity = store.CreateEntity();
Mem.AreEqual(1, entity.Id);

var count = maxId - 1;
Mem.AreEqual(count, store.EnsureCapacity(0));

Mem.AreEqual(9, store.EnsureCapacity(9));
for (int n = 0; n < 9; n++) {
Mem.AreEqual(9 - n, store.EnsureCapacity(0));
Mem.AreEqual(count, store.EnsureCapacity(count));
for (int n = 0; n < count; n++) {
Mem.AreEqual(count - n, store.EnsureCapacity(0));
store.CreateEntity();
}
Mem.AreEqual(0, store.EnsureCapacity(0));
Expand Down Expand Up @@ -242,7 +246,7 @@ public static void Test_StructHeap_RecycleIds_CommandBuffer()
Mem.IsTrue (entity1.IsNull);
Mem.IsTrue (entity2.IsNull);
Mem.AreEqual(1, store.Count);
Mem.AreEqual(4, store.Capacity);
Mem.AreEqual(128, store.Capacity);
entity1b.DeleteEntity();
if (n == 0) {
startMem = Mem.GetAllocatedBytes();
Expand Down

0 comments on commit 206f579

Please sign in to comment.