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

@@ -182,48 +182,22 @@ namespace OpenRA.Traits
// We loop over the direct index that represents the PPos in
// the ProjectedCellLayers, converting to a PPos only if
// it is needed (which is the uncommon case.)
var maxIndex = touched.MaxIndex;
for (var index = 0; index < maxIndex; index++)
if (disabledChanged)
{
// PERF: Most cells are not touched
if (!touched[index] && !disabledChanged)
continue;
touched[index] = false;
var type = ShroudCellType.Shroud;
if (explored[index])
touched.SetAll(false);
var maxIndex = touched.MaxIndex;
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)
{
var count = visibleCount[index];
if (!shroudGenerationEnabled || count > 0 || generatedShroudCount[index] == 0)
{
if (passiveVisibilityEnabled)
count += passiveVisibleCount[index];
type = count > 0 ? ShroudCellType.Visible : ShroudCellType.Fog;
}
}
// PERF: Most cells are unchanged
var oldResolvedType = resolvedType[index];
if (type != oldResolvedType || disabledChanged)
{
resolvedType[index] = type;
var puv = touched.PPosFromIndex(index);
if (map.Contains(puv))
OnShroudChanged(puv);
if (!disabledChanged && (fogEnabled || !ExploreMapEnabled))
{
if (type == ShroudCellType.Visible)
RevealedCells++;
else if (fogEnabled && oldResolvedType == ShroudCellType.Visible)
RevealedCells--;
}
if (self.Owner.WinState == WinState.Lost)
RevealedCells = 0;
touched[index] = false;
UpdateCell(index, self);
index = touched.IndexOf(true, index + 1);
}
}
@@ -231,6 +205,44 @@ namespace OpenRA.Traits
disabledChanged = false;
}
void UpdateCell(int index, Actor self)
{
var type = ShroudCellType.Shroud;
if (explored[index])
{
var count = visibleCount[index];
if (!shroudGenerationEnabled || count > 0 || generatedShroudCount[index] == 0)
{
if (passiveVisibilityEnabled)
count += passiveVisibleCount[index];
type = count > 0 ? ShroudCellType.Visible : ShroudCellType.Fog;
}
}
// PERF: Most cells are unchanged
var oldResolvedType = resolvedType[index];
if (type != oldResolvedType || disabledChanged)
{
resolvedType[index] = type;
var puv = touched.PPosFromIndex(index);
if (map.Contains(puv))
OnShroudChanged(puv);
if (!disabledChanged && (fogEnabled || !ExploreMapEnabled))
{
if (type == ShroudCellType.Visible)
RevealedCells++;
else if (fogEnabled && oldResolvedType == ShroudCellType.Visible)
RevealedCells--;
}
if (self.Owner.WinState == WinState.Lost)
RevealedCells = 0;
}
}
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