From 0a933ba09ceaab4a9ab376efef4c5c2ed55d9754 Mon Sep 17 00:00:00 2001 From: Vapre Date: Sat, 7 Feb 2026 21:50:12 +0100 Subject: [PATCH] CellLayer, use span. --- OpenRA.Game/Map/CellLayerBase.cs | 16 +++++++++++++--- OpenRA.Game/Map/ProjectedCellLayer.cs | 7 +------ OpenRA.Game/Traits/Player/Shroud.cs | 14 +++++++++++--- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/OpenRA.Game/Map/CellLayerBase.cs b/OpenRA.Game/Map/CellLayerBase.cs index b2f34041bc..a3a6d64934 100644 --- a/OpenRA.Game/Map/CellLayerBase.cs +++ b/OpenRA.Game/Map/CellLayerBase.cs @@ -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); } /// Clears the layer contents with their default value. public virtual void Clear() { - Array.Clear(Entries, 0, Entries.Length); + Entries.AsSpan().Clear(); } /// Clears the layer contents with a known value. public virtual void Clear(T clearValue) { - Array.Fill(Entries, clearValue); + Entries.AsSpan().Fill(clearValue); } public IEnumerator GetEnumerator() @@ -64,5 +64,15 @@ namespace OpenRA { return GetEnumerator(); } + + public ReadOnlyMemory AsReadOnlyMemory() + { + return Entries.AsMemory(); + } + + public Memory AsMemory() + { + return Entries.AsMemory(); + } } } diff --git a/OpenRA.Game/Map/ProjectedCellLayer.cs b/OpenRA.Game/Map/ProjectedCellLayer.cs index 98dd943532..97aab79e11 100644 --- a/OpenRA.Game/Map/ProjectedCellLayer.cs +++ b/OpenRA.Game/Map/ProjectedCellLayer.cs @@ -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); } } } diff --git a/OpenRA.Game/Traits/Player/Shroud.cs b/OpenRA.Game/Traits/Player/Shroud.cs index c96457e11d..8354ab2642 100644 --- a/OpenRA.Game/Traits/Player/Shroud.cs +++ b/OpenRA.Game/Traits/Player/Shroud.cs @@ -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; } }