From 218531fc2b65ebd904fe89c9ba142de221f58dc2 Mon Sep 17 00:00:00 2001 From: Ullrich Praetz Date: Mon, 29 Jul 2024 15:56:13 +0200 Subject: [PATCH] ECS - removed redundant EntityNode.flags; archetype is sufficient --- src/ECS/Entity/EntityNode.cs | 20 +++++++++----------- src/ECS/Entity/Store/Entities.cs | 10 +++++----- src/ECS/Entity/Store/NodeTree.cs | 12 ++++++------ src/ECS/EntityStore.cs | 2 +- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/ECS/Entity/EntityNode.cs b/src/ECS/Entity/EntityNode.cs index 7c031bab4..9fe319081 100644 --- a/src/ECS/Entity/EntityNode.cs +++ b/src/ECS/Entity/EntityNode.cs @@ -42,7 +42,7 @@ public struct EntityNode public Archetype Archetype => archetype; /// Internally used flags assigned to the entity. - public NodeFlags Flags => flags; + public NodeFlags Flags => archetype != null ? Created : default; /// Property only used to see component names encoded by . internal ComponentTypes IsOwner => new ComponentTypes{ bitSet = new BitSet { l0 = isOwner } }; @@ -59,8 +59,8 @@ public struct EntityNode [Browse(Never)] internal int compIndex; // 4 index within Archetype.entityIds & StructHeap<>.components - /// Use or for read access. - [Browse(Never)] internal NodeFlags flags; // 1 + // /// Use or for read access. + // [Browse(Never)] internal NodeFlags flags; // 1 /// /// Bit mask for all and all instances.
@@ -93,23 +93,21 @@ public struct EntityNode #endregion #region internal getter - internal readonly bool Is (NodeFlags flag) => (flags & flag) != 0; - internal readonly bool IsNot (NodeFlags flag) => (flags & flag) == 0; + // internal readonly bool Is (NodeFlags flag) => (flags & flag) != 0; + // internal readonly bool IsNot (NodeFlags flag) => (flags & flag) == 0; #endregion #region internal methods private readonly string GetString() { var sb = new StringBuilder(); - if (flags != 0) { + if (archetype != null) { sb.Append("flags: "); var startPos = sb.Length; - if (Is(Created)) { - if (startPos < sb.Length) { - sb.Append(" | "); - } - sb.Append("Created"); + if (startPos < sb.Length) { + sb.Append(" | "); } + sb.Append("Created"); } return sb.ToString(); } diff --git a/src/ECS/Entity/Store/Entities.cs b/src/ECS/Entity/Store/Entities.cs index 35cd18032..4c3a77323 100644 --- a/src/ECS/Entity/Store/Entities.cs +++ b/src/ECS/Entity/Store/Entities.cs @@ -62,7 +62,7 @@ internal void CheckEntityId(int id) if (id < Static.MinNodeId) { throw InvalidEntityIdException(id, nameof(id)); } - if (id < nodes.Length && nodes[id].Is(Created)) { + if (id < nodes.Length && nodes[id].archetype != null) { throw IdAlreadyInUseException(id, nameof(id)); } } @@ -168,13 +168,13 @@ private int CreateEntityNode(Archetype archetype, int id) { AssertIdInNodes(id); ref var node = ref nodes[id]; - if ((node.flags & Created) != 0) { + if (node.archetype != null) { return node.compIndex; } entityCount++; node.compIndex = Archetype.AddEntity(archetype, id); node.archetype = archetype; - node.flags = Created; + // node.flags = Created; return node.compIndex; } @@ -183,7 +183,7 @@ internal void CreateEntityNodes(Archetype archetype, int count) archetype.EnsureCapacity(count); int compIndexStart = archetype.entityCount; var entityIds = archetype.entityIds; - NewIds(entityIds, compIndexStart, count); + NewIds(entityIds, compIndexStart, count, archetype); EnsureNodesLength(intern.sequenceId + 1); // may resize nodes var localNodes = nodes; var maxIndex = compIndexStart + count; @@ -193,7 +193,7 @@ internal void CreateEntityNodes(Archetype archetype, int count) ref var node = ref localNodes[id]; node.compIndex = index; node.archetype = archetype; - node.flags = Created; + // node.flags = Created; } if (intern.pidType == PidType.RandomPids) { for (int index = compIndexStart; index < maxIndex; index++) { diff --git a/src/ECS/Entity/Store/NodeTree.cs b/src/ECS/Entity/Store/NodeTree.cs index 4dcfd2e00..43603001b 100644 --- a/src/ECS/Entity/Store/NodeTree.cs +++ b/src/ECS/Entity/Store/NodeTree.cs @@ -452,7 +452,7 @@ protected internal override void UpdateEntityCompIndex(int id, int compIndex) } /// Note! Sync implementation with . - private void NewIds(int[] ids, int start, int count) + private void NewIds(int[] ids, int start, int count, Archetype archetype) { var localNodes = nodes; int n = 0; @@ -461,8 +461,8 @@ private void NewIds(int[] ids, int start, int count) if (!intern.recycleIds.TryPop(out int id)) { break; } - localNodes[id].flags = Created; // mark created. So id is not used twice by loop below - ids[n + start] = id; + localNodes[id].archetype = archetype; // mark created. So id is not used twice by loop below + ids[n + start] = id; } int max = localNodes.Length; int sequenceId = intern.sequenceId; @@ -470,7 +470,7 @@ private void NewIds(int[] ids, int start, int count) { for (; ++sequenceId < max;) { - if ((localNodes[sequenceId].flags & Created) != 0) { + if (localNodes[sequenceId].archetype != null) { continue; } break; @@ -487,7 +487,7 @@ internal int NewId() int id; while (intern.recycleIds.TryPop(out id)) { - if ((localNodes[id].flags & Created) != 0) { + if (localNodes[id].archetype != null) { continue; } return id; @@ -496,7 +496,7 @@ internal int NewId() id = ++intern.sequenceId; for (; id < max;) { - if ((localNodes[id].flags & Created) != 0) { + if (localNodes[id].archetype != null) { id = ++intern.sequenceId; continue; } diff --git a/src/ECS/EntityStore.cs b/src/ECS/EntityStore.cs index 67c194852..d5693f863 100644 --- a/src/ECS/EntityStore.cs +++ b/src/ECS/EntityStore.cs @@ -246,7 +246,7 @@ public bool TryGetEntityByPid(long pid, out Entity value) } if (0 < pid && pid < nodes.Length) { var id = (int)pid; - if (nodes[id].Is(NodeFlags.Created)) { + if (nodes[id].archetype != null) { value = new Entity(this, id); return true; }