From 2428b967bf13a6d596bb4d11c16e6b5a12d60116 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Thu, 12 Jan 2017 21:28:51 +0000 Subject: [PATCH] Cache coloring delegate in AppearsOnRadar.RadarSignatureCells. --- OpenRA.Mods.Common/Traits/Radar/AppearsOnRadar.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/OpenRA.Mods.Common/Traits/Radar/AppearsOnRadar.cs b/OpenRA.Mods.Common/Traits/Radar/AppearsOnRadar.cs index c846c9fcd9..c583986495 100644 --- a/OpenRA.Mods.Common/Traits/Radar/AppearsOnRadar.cs +++ b/OpenRA.Mods.Common/Traits/Radar/AppearsOnRadar.cs @@ -9,6 +9,7 @@ */ #endregion +using System; using System.Collections.Generic; using System.Drawing; using System.Linq; @@ -29,6 +30,9 @@ namespace OpenRA.Mods.Common.Traits.Radar readonly AppearsOnRadarInfo info; IRadarColorModifier modifier; + Color currentColor = Color.Transparent; + Func, Pair> cellToSignature; + public AppearsOnRadar(AppearsOnRadarInfo info) { this.info = info; @@ -48,7 +52,14 @@ namespace OpenRA.Mods.Common.Traits.Radar if (info.UseLocation) return new[] { Pair.New(self.Location, color) }; - return self.OccupiesSpace.OccupiedCells().Select(c => Pair.New(c.First, color)); + // PERF: Cache cellToSignature delegate to avoid allocations as color does not change often. + if (currentColor != color) + { + currentColor = color; + cellToSignature = c => Pair.New(c.First, color); + } + + return self.OccupiesSpace.OccupiedCells().Select(cellToSignature); } } } \ No newline at end of file