Improve performance of AIUtils.FindQueues

The AI would often invoke this method inside of loops, searching for a different category of queue each time. This would result in multiple searches against the trait dictionary to locate matching queues. Now we alter the method to create a lookup of all the queues keyed by category. This allows a single trait search to be performed.

UnitBuilderBotModule and BaseBuilderBotModule are updated to fetch this lookup once when required, and pass the results along to avoid calling the method more times than necessary. This improves their performance.
This commit is contained in:
RoosterDragon
2024-07-05 19:42:22 +01:00
committed by Gustas
parent 62d1f002dd
commit c45e78cf1d
4 changed files with 22 additions and 14 deletions

View File

@@ -34,11 +34,12 @@ namespace OpenRA.Mods.Common
.Any(availableCells => availableCells > 0);
}
public static IEnumerable<ProductionQueue> FindQueues(Player player, string category)
public static ILookup<string, ProductionQueue> FindQueuesByCategory(Player player)
{
return player.World.ActorsWithTrait<ProductionQueue>()
.Where(a => a.Actor.Owner == player && a.Trait.Info.Type == category && a.Trait.Enabled)
.Select(a => a.Trait);
.Where(a => a.Actor.Owner == player && a.Trait.Enabled)
.Select(a => a.Trait)
.ToLookup(pq => pq.Info.Type);
}
public static int CountActorsWithNameAndTrait<T>(string actorName, Player owner)