Skip to content

Commit

Permalink
simplify - increment EntityNode.revision when entity is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Nov 29, 2024
1 parent d77bc63 commit 1271c11
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/ECS/Entity/EntityNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public struct EntityNode
[Browse(Never)] internal Archetype archetype; // 8 can be null. Could use int to relieve GC tracing reference types

[Browse(Never)] internal int compIndex; // 4 index within Archetype.entityIds & StructHeap<>.components

/// revision is incremented if entity id deleted
[Browse(Never)] internal short revision; // 2

// /// <summary> Use <see cref="Is"/> or <see cref="IsNot"/> for read access. </summary>
Expand Down
3 changes: 0 additions & 3 deletions src/ECS/Entity/Store/Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,8 @@ private int CreateEntityNode(Archetype archetype, int id, out short revision)
return node.compIndex;
}
entityCount++;
revision++;
node.compIndex = Archetype.AddEntity(archetype, id);
node.archetype = archetype;
node.revision = revision;
// node.flags = Created;
return node.compIndex;
}
Expand All @@ -195,7 +193,6 @@ internal void CreateEntityNodes(Archetype archetype, int count)
ref var node = ref localNodes[id];
node.compIndex = index;
node.archetype = archetype;
node.revision++;
// node.flags = Created;
}
if (intern.pidType == PidType.RandomPids) {
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Entity/Store/NodeTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ internal void DeleteNode(Entity entity)
// --- clear node entry.
var revision = node.revision;
node = default;
node.revision = revision;
node.revision = ++revision;
extension.RemoveEntity(id);

// --- remove child from parent
Expand Down
7 changes: 1 addition & 6 deletions src/ECS/EntityStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,7 @@ internal Entity CreateEntityReference(long key)
if (extension.pid2Id == null) {
id = (int)key;
EnsureNodesLength(id + 1);
ref var node = ref nodes[id];
var revision = node.revision;
if (node.archetype != null) {
return new Entity(this, id, revision);
}
return new Entity(this, id, ++revision); // assign same revision which will be assigned in CreateEntityNode()
return new Entity(this, id, nodes[id].revision);
}
throw new NotSupportedException("Entity serialization using PidType.RandomPids currently not supported");
/* var pid = key;
Expand Down
2 changes: 1 addition & 1 deletion src/Tests-internal/ECS/Test_Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static void Test_Entity_Info()
AreEqual(json, entity.Info.JSON.Value);
AreEqual("event types: 0, handlers: 0", entity.Info.EventHandlers.ToString());
AreEqual("", entity.Info.JSON.ToString());
AreEqual(1, entity.Info.Revision);
AreEqual(0, entity.Info.Revision);
}

[Test]
Expand Down
16 changes: 8 additions & 8 deletions src/Tests/ECS/Entity/Test_StructHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ public static void Test_StructHeap_CreateEntity_RecycleIds()
var entity2 = store.CreateEntity(2);
entity1.DeleteEntity();
entity2.DeleteEntity();
Assert.AreEqual(1, entity1.Revision);
Assert.AreEqual(1, entity2.Revision);
Assert.AreEqual(0, entity1.Revision);
Assert.AreEqual(0, entity2.Revision);

var entity2b = store.CreateEntity();
var entity1b = store.CreateEntity();

Assert.AreEqual(2, entity2b.Id); Assert.AreEqual(2, entity2b.Revision);
Assert.AreEqual(1, entity1b.Id); Assert.AreEqual(2, entity1b.Revision);
Assert.AreEqual(2, entity2b.Id); Assert.AreEqual(1, entity2b.Revision);
Assert.AreEqual(1, entity1b.Id); Assert.AreEqual(1, entity1b.Revision);

entity1b.DeleteEntity();
entity2b.DeleteEntity();
Expand All @@ -203,14 +203,14 @@ public static void Test_StructHeap_CreateEntity_RecycleIds()

Assert.AreEqual(3, entities.Length);
Assert.AreEqual("{ 2, 1, 3 }", entities.Debug()); // recycle: 2, 1 new id: 3
Assert.AreEqual(3, entities[0].Revision);
Assert.AreEqual(3, entities[1].Revision);
Assert.AreEqual(1, entities[2].Revision);
Assert.AreEqual(2, entities[0].Revision);
Assert.AreEqual(2, entities[1].Revision);
Assert.AreEqual(0, entities[2].Revision);

// --- cover case: recycled id is created manually
entities[0].DeleteEntity(); // id: 2
var entity2d = store.CreateEntity(2);
Assert.AreEqual(2, entity2d.Id); Assert.AreEqual(4, entity2d.Revision);
Assert.AreEqual(2, entity2d.Id); Assert.AreEqual(3, entity2d.Revision);
Assert.IsFalse(entities[0] == entity2d);

var entity4 = store.CreateEntity();
Expand Down

0 comments on commit 1271c11

Please sign in to comment.