Optimize actor pasting performance with huge amount of actors

- Name lookups are cached to avoid checking the name from same Actor multiple times
- Avoid getting actor children multiple times for same parent
This commit is contained in:
2023-08-16 20:23:08 +03:00
parent 73076dc5cb
commit 3e33c63957

View File

@@ -138,6 +138,8 @@ namespace FlaxEditor.Actions
} }
} }
// Store previously looked up names and the results
Dictionary<string, bool> foundNamesResults = new();
for (int i = 0; i < nodeParents.Count; i++) for (int i = 0; i < nodeParents.Count; i++)
{ {
var node = nodeParents[i]; var node = nodeParents[i];
@@ -145,15 +147,28 @@ namespace FlaxEditor.Actions
var parent = actor?.Parent; var parent = actor?.Parent;
if (parent != null) if (parent != null)
{ {
bool IsNameValid(string name)
{
if (!foundNamesResults.TryGetValue(name, out bool found))
{
found = parent.GetChild(name) != null;
foundNamesResults.Add(name, found);
}
return !found;
}
// Fix name collisions // Fix name collisions
var name = actor.Name; var name = actor.Name;
for (int j = 0; j < parent.ChildrenCount; j++) var children = parent.Children;
for (int j = 0; j < children.Length; j++)
{ {
var child = parent.Children[j]; var child = children[j];
if (child != actor && child.Name == actor.Name) if (child != actor && child.Name == name)
{ {
var children = parent.Children; string newName = Utilities.Utils.IncrementNameNumber(name, x => IsNameValid(x));
actor.Name = Utilities.Utils.IncrementNameNumber(name, x => children.All(y => y.Name != x)); foundNamesResults[newName] = true;
actor.Name = newName;
// Multiple actors may have the same name, continue
} }
} }
} }
@@ -162,10 +177,7 @@ namespace FlaxEditor.Actions
} }
for (int i = 0; i < nodeParents.Count; i++) for (int i = 0; i < nodeParents.Count; i++)
{ nodeParents[i].PostPaste();
var node = nodeParents[i];
node.PostPaste();
}
} }
/// <summary> /// <summary>