Skip to content

Commit

Permalink
Engine - ECS: optimize Entity.RemoveChild() & InsertChild()
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Jul 22, 2024
1 parent 46872dc commit fbd9726
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Engine/src/ECS/Entity/Store/NodeTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ internal void InsertChild (int parentId, int childId, int childIndex)
goto InsertNode;
}
// case: entity with given id is already a child of this entity => move child
curIndex = GetChildIndex(curParentEntity, childId);
var childIds = GetChildIds(curParentEntity);
curIndex = childIds.LastIndexOf(childId); // enable O(1) if childIndex == last child
if (curIndex == childIndex) {
// case: child entity is already at the requested childIndex
return;
Expand Down Expand Up @@ -215,7 +216,7 @@ private int RemoveChildNode (int parentId, int childId)
var parent = new Entity(this, parentId);
ref var treeNode = ref GetTreeNodeRef(parent);
var childIds = treeNode.GetChildIds(this);
int index = childIds.IndexOf(childId);
int index = childIds.LastIndexOf(childId); // enable O(1) if childId == last child
if (index != -1) {
treeNode.childIds.RemoveAt(index, extension.childHeap, keepOrder: true);
return index;
Expand Down
2 changes: 1 addition & 1 deletion Engine/src/Tests/ECS/Entity/Test_Entity_Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ public static void Test_Entity_Tree_TreeNode()
public static void Test_Entity_Tree_Allocation()
{
int count = 10; // 2000
// Test_Entity_Tree_Allocation - count: 2000 entities: 4002001 duration: 486 ms
// Test_Entity_Tree_Allocation - count: 2000 entities: 4002001 duration: 202 ms
var store = new EntityStore();
var root = store.CreateEntity(1);
var type = store.GetArchetype(default);
Expand Down

0 comments on commit fbd9726

Please sign in to comment.