From a3898db66018e1b3ac3b11ec59d2ebefbdb145d4 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sat, 14 Feb 2015 00:39:03 +0000 Subject: [PATCH] Speed up TraitContainer.RemoveActor. Use the binary search invariant of the lists to find the actor quickly and remove the range of entries from the list in a single operation to avoid redundant copying otherwise caused by removing entries one by one. --- OpenRA.Game/TraitDictionary.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/OpenRA.Game/TraitDictionary.cs b/OpenRA.Game/TraitDictionary.cs index 4e36219b37..2d7e546c13 100755 --- a/OpenRA.Game/TraitDictionary.cs +++ b/OpenRA.Game/TraitDictionary.cs @@ -231,14 +231,15 @@ namespace OpenRA public void RemoveActor(uint actor) { - for (var i = actors.Count - 1; i >= 0; i--) - { - if (actors[i].ActorID == actor) - { - actors.RemoveAt(i); - traits.RemoveAt(i); - } - } + var startIndex = actors.BinarySearchMany(actor); + if (startIndex >= actors.Count || actors[startIndex].ActorID != actor) + return; + var endIndex = startIndex + 1; + while (endIndex < actors.Count && actors[endIndex].ActorID == actor) + endIndex++; + var count = endIndex - startIndex; + actors.RemoveRange(startIndex, count); + traits.RemoveRange(startIndex, count); } } }