Use Array.IndexOf to speed up Shroud.Tick.

As the `touched` cell layer uses Boolean values, Array.IndexOf is able to use a fast vectorised search. Most values in the array are false, so the search is able to significantly improve the performance of finding the next true value in the array.
This commit is contained in:
RoosterDragon
2023-11-04 12:11:31 +00:00
committed by Gustas
parent 5157bc375d
commit 0c2d060d43
2 changed files with 63 additions and 40 deletions

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using OpenRA.Primitives; using OpenRA.Primitives;
namespace OpenRA namespace OpenRA
@@ -53,5 +54,15 @@ namespace OpenRA
{ {
return Bounds.Contains(uv.U, uv.V); return Bounds.Contains(uv.U, uv.V);
} }
public int IndexOf(T value, int startIndex)
{
return Array.IndexOf(Entries, value, startIndex);
}
public void SetAll(T value)
{
Array.Fill(Entries, value);
}
} }
} }

View File

@@ -182,15 +182,31 @@ namespace OpenRA.Traits
// We loop over the direct index that represents the PPos in // We loop over the direct index that represents the PPos in
// the ProjectedCellLayers, converting to a PPos only if // the ProjectedCellLayers, converting to a PPos only if
// it is needed (which is the uncommon case.) // it is needed (which is the uncommon case.)
if (disabledChanged)
{
touched.SetAll(false);
var maxIndex = touched.MaxIndex; var maxIndex = touched.MaxIndex;
for (var index = 0; index < maxIndex; index++) for (var index = 0; index < maxIndex; index++)
UpdateCell(index, self);
}
else
{
// PERF: Most cells are unchanged, use IndexOf for fast vectorized search.
var index = touched.IndexOf(true, 0);
while (index != -1)
{ {
// PERF: Most cells are not touched
if (!touched[index] && !disabledChanged)
continue;
touched[index] = false; touched[index] = false;
UpdateCell(index, self);
index = touched.IndexOf(true, index + 1);
}
}
Hash = Sync.HashPlayer(self.Owner) + self.World.WorldTick;
disabledChanged = false;
}
void UpdateCell(int index, Actor self)
{
var type = ShroudCellType.Shroud; var type = ShroudCellType.Shroud;
if (explored[index]) if (explored[index])
@@ -227,10 +243,6 @@ namespace OpenRA.Traits
} }
} }
Hash = Sync.HashPlayer(self.Owner) + self.World.WorldTick;
disabledChanged = false;
}
public static IEnumerable<PPos> ProjectedCellsInRange(Map map, WPos pos, WDist minRange, WDist maxRange, int maxHeightDelta = -1) public static IEnumerable<PPos> ProjectedCellsInRange(Map map, WPos pos, WDist minRange, WDist maxRange, int maxHeightDelta = -1)
{ {
// Account for potential extra half-cell from odd-height terrain // Account for potential extra half-cell from odd-height terrain