CellLayer, use span.

This commit is contained in:
Vapre
2026-02-07 21:50:12 +01:00
committed by Gustas Kažukauskas
parent 6b04dbc210
commit 0a933ba09c
3 changed files with 25 additions and 12 deletions

View File

@@ -40,19 +40,19 @@ namespace OpenRA
if (Size != anotherLayer.Size || GridType != anotherLayer.GridType)
throw new ArgumentException("Layers must have a matching size and shape (grid type).", nameof(anotherLayer));
Array.Copy(anotherLayer.Entries, Entries, Entries.Length);
anotherLayer.Entries.AsSpan().CopyTo(Entries);
}
/// <summary>Clears the layer contents with their default value.</summary>
public virtual void Clear()
{
Array.Clear(Entries, 0, Entries.Length);
Entries.AsSpan().Clear();
}
/// <summary>Clears the layer contents with a known value.</summary>
public virtual void Clear(T clearValue)
{
Array.Fill(Entries, clearValue);
Entries.AsSpan().Fill(clearValue);
}
public IEnumerator<T> GetEnumerator()
@@ -64,5 +64,15 @@ namespace OpenRA
{
return GetEnumerator();
}
public ReadOnlyMemory<T> AsReadOnlyMemory()
{
return Entries.AsMemory();
}
public Memory<T> AsMemory()
{
return Entries.AsMemory();
}
}
}

View File

@@ -55,14 +55,9 @@ namespace OpenRA
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);
Entries.AsSpan().Fill(value);
}
}
}

View File

@@ -183,12 +183,20 @@ namespace OpenRA.Traits
else
{
// PERF: Most cells are unchanged, use IndexOf for fast vectorized search.
var index = touched.IndexOf(true, 0);
var span = touched.AsMemory().Span;
var index = span.IndexOf(true);
while (index != -1)
{
touched[index] = false;
span[index] = false;
UpdateCell(index, self);
index = touched.IndexOf(true, index + 1);
if (index < span.Length - 1)
{
var sliceIndex = span[++index..].IndexOf(true);
index = sliceIndex == -1 ? -1 : index + sliceIndex;
}
else
index = -1;
}
}