Improve shroud performance

This commit is contained in:
Adam Mitchell
2021-01-18 19:34:53 +00:00
committed by reaperrr
parent 5cf622fb6e
commit 9ee7294c81
2 changed files with 18 additions and 3 deletions

View File

@@ -15,6 +15,8 @@ namespace OpenRA
{ {
public sealed class ProjectedCellLayer<T> : CellLayerBase<T> public sealed class ProjectedCellLayer<T> : CellLayerBase<T>
{ {
public int MaxIndex { get { return Size.Width * Size.Height; } }
public ProjectedCellLayer(Map map) public ProjectedCellLayer(Map map)
: base(map) { } : base(map) { }
@@ -27,6 +29,11 @@ namespace OpenRA
return uv.V * Size.Width + uv.U; return uv.V * Size.Width + uv.U;
} }
public PPos PPosFromIndex(int index)
{
return new PPos(index % Size.Width, index / Size.Width);
}
public T this[int index] public T this[int index]
{ {
get get

View File

@@ -165,9 +165,14 @@ namespace OpenRA.Traits
if (OnShroudChanged == null) if (OnShroudChanged == null)
return; return;
foreach (var puv in map.ProjectedCells) // PERF: Parts of this loop are very hot.
// 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++)
{ {
var index = touched.Index(puv); // PERF: Most cells are not touched
if (!touched[index]) if (!touched[index])
continue; continue;
@@ -187,11 +192,14 @@ namespace OpenRA.Traits
} }
} }
// PERF: Most cells are unchanged
var oldResolvedType = resolvedType[index]; var oldResolvedType = resolvedType[index];
if (type != oldResolvedType) if (type != oldResolvedType)
{ {
resolvedType[index] = type; resolvedType[index] = type;
OnShroudChanged(puv); var uv = touched.PPosFromIndex(index);
if (map.Contains(uv))
OnShroudChanged(uv);
} }
} }