Use spans to improve performance in StreamExts.

Also avoid ReadBytes calls that allocate a buffer by either updating the stream position (if not interested in the bytes), by reusing an input buffer (if interested in the bytes), or using a stackalloc buffer to avoid the allocation (for small reads).
This commit is contained in:
RoosterDragon
2023-09-19 18:10:09 +01:00
committed by Gustas
parent b3ee3551ca
commit 5d91b678bb
20 changed files with 153 additions and 92 deletions

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Network
// the Order objects directly on the local client.
data = new MemoryStream();
foreach (var o in orders)
data.WriteArray(o.Serialize());
data.Write(o.Serialize());
}
public OrderPacket(MemoryStream data)
@@ -86,7 +86,7 @@ namespace OpenRA.Network
ms.Write(data.Frame);
ms.WriteByte((byte)OrderType.SyncHash);
ms.Write(data.SyncHash);
ms.WriteArray(BitConverter.GetBytes(data.DefeatState));
ms.Write(data.DefeatState);
return ms.GetBuffer();
}
@@ -95,7 +95,7 @@ namespace OpenRA.Network
var ms = new MemoryStream(14);
ms.Write(0);
ms.WriteByte((byte)OrderType.Ping);
ms.WriteArray(BitConverter.GetBytes(timestamp));
ms.Write(timestamp);
ms.WriteByte(queueLength);
return ms.GetBuffer();
}