From 31a096dcf18bdab047cb59f0e3a4c96bab59cb41 Mon Sep 17 00:00:00 2001 From: atlimit8 Date: Wed, 8 Oct 2014 15:25:21 -0500 Subject: [PATCH] HashSet => BitArray resourceTypeIndices BitArray can store 256 bits in 32 bytes plus overhead (pointer & length), which gives it better memory locality too (<= 2 locations in memory). Also, the actual count will be much less, probably at most around a dozen. This might not impact performance, but the AI is dumb & HashSet is clearly inefficient. --- OpenRA.Mods.RA/AI/HackyAI.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/OpenRA.Mods.RA/AI/HackyAI.cs b/OpenRA.Mods.RA/AI/HackyAI.cs index 73188d5263..e614219ae0 100644 --- a/OpenRA.Mods.RA/AI/HackyAI.cs +++ b/OpenRA.Mods.RA/AI/HackyAI.cs @@ -9,6 +9,7 @@ #endregion using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using OpenRA.Mods.Common; @@ -158,7 +159,7 @@ namespace OpenRA.Mods.RA.AI bool enabled; int ticks; - HashSet resourceTypeIndices; + BitArray resourceTypeIndices; RushFuzzy rushFuzzy = new RushFuzzy(); @@ -208,10 +209,9 @@ namespace OpenRA.Mods.RA.AI random = new MersenneTwister((int)p.PlayerActor.ActorID); - resourceTypeIndices = new HashSet( - Map.Rules.Actors["world"].Traits - .WithInterface() - .Select(t => world.TileSet.GetTerrainIndex(t.TerrainType))); + resourceTypeIndices = new BitArray(world.TileSet.TerrainInfo.Length); // Big enough + foreach (var t in Map.Rules.Actors["world"].Traits.WithInterface()) + resourceTypeIndices.Set(world.TileSet.GetTerrainIndex(t.TerrainType), true); } ActorInfo ChooseRandomUnitToBuild(ProductionQueue queue) @@ -371,7 +371,7 @@ namespace OpenRA.Mods.RA.AI // Try and place the refinery near a resource field var nearbyResources = Map.FindTilesInCircle(baseCenter, Info.MaxBaseRadius) - .Where(a => resourceTypeIndices.Contains(Map.GetTerrainIndex(a))) + .Where(a => resourceTypeIndices.Get(Map.GetTerrainIndex(a))) .Shuffle(random); foreach (var c in nearbyResources)