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:
@@ -165,9 +165,9 @@ namespace OpenRA
|
||||
if (length < 0x80)
|
||||
return length;
|
||||
|
||||
var data = new byte[4];
|
||||
s.ReadBytes(data, 0, Math.Min(length & 0x7F, 4));
|
||||
return BitConverter.ToInt32(data.ToArray(), 0);
|
||||
Span<byte> data = stackalloc byte[4];
|
||||
s.ReadBytes(data[..Math.Min(length & 0x7F, 4)]);
|
||||
return BitConverter.ToInt32(data);
|
||||
}
|
||||
|
||||
static int TripletFullLength(int dataLength)
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace OpenRA.FileFormats
|
||||
while (true)
|
||||
{
|
||||
var length = IPAddress.NetworkToHostOrder(s.ReadInt32());
|
||||
var type = Encoding.UTF8.GetString(s.ReadBytes(4));
|
||||
var type = s.ReadASCII(4);
|
||||
var content = s.ReadBytes(length);
|
||||
/*var crc = */s.ReadInt32();
|
||||
|
||||
@@ -287,10 +287,10 @@ namespace OpenRA.FileFormats
|
||||
|
||||
var typeBytes = Encoding.ASCII.GetBytes(type);
|
||||
output.Write(IPAddress.HostToNetworkOrder((int)input.Length));
|
||||
output.WriteArray(typeBytes);
|
||||
output.Write(typeBytes);
|
||||
|
||||
var data = input.ReadAllBytes();
|
||||
output.WriteArray(data);
|
||||
output.Write(data);
|
||||
|
||||
var crc32 = new Crc32();
|
||||
crc32.Update(typeBytes);
|
||||
@@ -302,7 +302,7 @@ namespace OpenRA.FileFormats
|
||||
{
|
||||
using (var output = new MemoryStream())
|
||||
{
|
||||
output.WriteArray(Signature);
|
||||
output.Write(Signature);
|
||||
using (var header = new MemoryStream())
|
||||
{
|
||||
header.Write(IPAddress.HostToNetworkOrder(Width));
|
||||
@@ -371,7 +371,7 @@ namespace OpenRA.FileFormats
|
||||
{
|
||||
using (var text = new MemoryStream())
|
||||
{
|
||||
text.WriteArray(Encoding.ASCII.GetBytes(kv.Key + (char)0 + kv.Value));
|
||||
text.Write(Encoding.ASCII.GetBytes(kv.Key + (char)0 + kv.Value));
|
||||
WritePngChunk(output, "tEXt", text);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace OpenRA.FileFormats
|
||||
throw new NotSupportedException($"Metadata version {version} is not supported");
|
||||
|
||||
// Read game info (max 100K limit as a safeguard against corrupted files)
|
||||
var data = fs.ReadString(Encoding.UTF8, 1024 * 100);
|
||||
var data = fs.ReadLengthPrefixedString(Encoding.UTF8, 1024 * 100);
|
||||
GameInfo = GameInformation.Deserialize(data);
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ namespace OpenRA.FileFormats
|
||||
{
|
||||
// Write lobby info data
|
||||
writer.Flush();
|
||||
dataLength += writer.BaseStream.WriteString(Encoding.UTF8, GameInfo.Serialize());
|
||||
dataLength += writer.BaseStream.WriteLengthPrefixedString(Encoding.UTF8, GameInfo.Serialize());
|
||||
}
|
||||
|
||||
// Write total length & end marker
|
||||
|
||||
@@ -117,10 +117,7 @@ namespace OpenRA.FileSystem
|
||||
|
||||
void Commit()
|
||||
{
|
||||
var pos = pkgStream.Position;
|
||||
pkgStream.Position = 0;
|
||||
File.WriteAllBytes(Name, pkgStream.ReadBytes((int)pkgStream.Length));
|
||||
pkgStream.Position = pos;
|
||||
File.WriteAllBytes(Name, pkgStream.ToArray());
|
||||
}
|
||||
|
||||
public void Update(string filename, byte[] contents)
|
||||
|
||||
@@ -261,14 +261,14 @@ namespace OpenRA.Network
|
||||
{
|
||||
var ms = new MemoryStream();
|
||||
ms.Write(packet.Length);
|
||||
ms.WriteArray(packet);
|
||||
ms.Write(packet);
|
||||
|
||||
foreach (var s in queuedSyncPackets)
|
||||
{
|
||||
var q = OrderIO.SerializeSync(s);
|
||||
|
||||
ms.Write(q.Length);
|
||||
ms.WriteArray(q);
|
||||
ms.Write(q);
|
||||
|
||||
sentSync.Enqueue(s);
|
||||
}
|
||||
|
||||
@@ -122,10 +122,10 @@ namespace OpenRA.Network
|
||||
LastSyncFrame = rs.ReadInt32();
|
||||
lastSyncPacket = rs.ReadBytes(Order.SyncHashOrderLength);
|
||||
|
||||
var globalSettings = MiniYaml.FromString(rs.ReadString(Encoding.UTF8, Connection.MaxOrderLength));
|
||||
var globalSettings = MiniYaml.FromString(rs.ReadLengthPrefixedString(Encoding.UTF8, Connection.MaxOrderLength));
|
||||
GlobalSettings = Session.Global.Deserialize(globalSettings[0].Value);
|
||||
|
||||
var slots = MiniYaml.FromString(rs.ReadString(Encoding.UTF8, Connection.MaxOrderLength));
|
||||
var slots = MiniYaml.FromString(rs.ReadLengthPrefixedString(Encoding.UTF8, Connection.MaxOrderLength));
|
||||
Slots = new Dictionary<string, Session.Slot>();
|
||||
foreach (var s in slots)
|
||||
{
|
||||
@@ -133,7 +133,7 @@ namespace OpenRA.Network
|
||||
Slots.Add(slot.PlayerReference, slot);
|
||||
}
|
||||
|
||||
var slotClients = MiniYaml.FromString(rs.ReadString(Encoding.UTF8, Connection.MaxOrderLength));
|
||||
var slotClients = MiniYaml.FromString(rs.ReadLengthPrefixedString(Encoding.UTF8, Connection.MaxOrderLength));
|
||||
SlotClients = new Dictionary<string, SlotClient>();
|
||||
foreach (var s in slotClients)
|
||||
{
|
||||
@@ -144,7 +144,7 @@ namespace OpenRA.Network
|
||||
if (rs.Position != traitDataOffset || rs.ReadInt32() != TraitDataMarker)
|
||||
throw new InvalidDataException("Invalid orasav file");
|
||||
|
||||
var traitData = MiniYaml.FromString(rs.ReadString(Encoding.UTF8, Connection.MaxOrderLength));
|
||||
var traitData = MiniYaml.FromString(rs.ReadLengthPrefixedString(Encoding.UTF8, Connection.MaxOrderLength));
|
||||
foreach (var td in traitData)
|
||||
TraitData.Add(Exts.ParseInt32Invariant(td.Key), td.Value);
|
||||
|
||||
@@ -229,7 +229,7 @@ namespace OpenRA.Network
|
||||
ordersStream.Write(data.Length + 8);
|
||||
ordersStream.Write(frame);
|
||||
ordersStream.Write(clientSlot);
|
||||
ordersStream.WriteArray(data);
|
||||
ordersStream.Write(data);
|
||||
LastOrdersFrame = frame;
|
||||
}
|
||||
|
||||
@@ -294,17 +294,17 @@ namespace OpenRA.Network
|
||||
file.Write(lastSyncPacket, 0, Order.SyncHashOrderLength);
|
||||
|
||||
var globalSettingsNodes = new List<MiniYamlNode>() { GlobalSettings.Serialize() };
|
||||
file.WriteString(Encoding.UTF8, globalSettingsNodes.WriteToString());
|
||||
file.WriteLengthPrefixedString(Encoding.UTF8, globalSettingsNodes.WriteToString());
|
||||
|
||||
var slotNodes = Slots
|
||||
.Select(s => s.Value.Serialize())
|
||||
.ToList();
|
||||
file.WriteString(Encoding.UTF8, slotNodes.WriteToString());
|
||||
file.WriteLengthPrefixedString(Encoding.UTF8, slotNodes.WriteToString());
|
||||
|
||||
var slotClientNodes = SlotClients
|
||||
.Select(s => s.Value.Serialize(s.Key))
|
||||
.ToList();
|
||||
file.WriteString(Encoding.UTF8, slotClientNodes.WriteToString());
|
||||
file.WriteLengthPrefixedString(Encoding.UTF8, slotClientNodes.WriteToString());
|
||||
|
||||
var traitDataOffset = file.Length;
|
||||
file.Write(TraitDataMarker);
|
||||
@@ -312,7 +312,7 @@ namespace OpenRA.Network
|
||||
var traitDataNodes = TraitData
|
||||
.Select(kv => new MiniYamlNode(kv.Key.ToStringInvariant(), kv.Value))
|
||||
.ToList();
|
||||
file.WriteString(Encoding.UTF8, traitDataNodes.WriteToString());
|
||||
file.WriteLengthPrefixedString(Encoding.UTF8, traitDataNodes.WriteToString());
|
||||
|
||||
file.Write((int)ordersStream.Length);
|
||||
file.Write((int)traitDataOffset);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ namespace OpenRA.Network
|
||||
}
|
||||
}
|
||||
|
||||
file.WriteArray(initialContent);
|
||||
file.Write(initialContent);
|
||||
writer = new BinaryWriter(file);
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ namespace OpenRA.Network
|
||||
{
|
||||
var ms = new MemoryStream(4 + data.Length);
|
||||
ms.Write(frame);
|
||||
ms.WriteArray(data);
|
||||
ms.Write(data);
|
||||
Receive(clientID, ms.GetBuffer());
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace OpenRA.Server
|
||||
ms.Write(0);
|
||||
ms.Write(0);
|
||||
ms.WriteByte((byte)OrderType.Ping);
|
||||
ms.WriteArray(BitConverter.GetBytes(Game.RunTime));
|
||||
ms.Write(Game.RunTime);
|
||||
return ms.GetBuffer();
|
||||
}
|
||||
|
||||
|
||||
@@ -707,7 +707,7 @@ namespace OpenRA.Server
|
||||
ms.Write(data.Length + 4);
|
||||
ms.Write(client);
|
||||
ms.Write(frame);
|
||||
ms.WriteArray(data);
|
||||
ms.Write(data);
|
||||
return ms.GetBuffer();
|
||||
}
|
||||
|
||||
|
||||
@@ -20,10 +20,23 @@ namespace OpenRA
|
||||
{
|
||||
public static class StreamExts
|
||||
{
|
||||
public static void ReadBytes(this Stream s, Span<byte> dest)
|
||||
{
|
||||
while (dest.Length > 0)
|
||||
{
|
||||
var bytesRead = s.Read(dest);
|
||||
if (bytesRead == 0)
|
||||
throw new EndOfStreamException();
|
||||
|
||||
dest = dest[bytesRead..];
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] ReadBytes(this Stream s, int count)
|
||||
{
|
||||
if (count < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(count), "Non-negative number required.");
|
||||
|
||||
var buffer = new byte[count];
|
||||
s.ReadBytes(buffer, 0, count);
|
||||
return buffer;
|
||||
@@ -33,10 +46,11 @@ namespace OpenRA
|
||||
{
|
||||
if (count < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(count), "Non-negative number required.");
|
||||
|
||||
while (count > 0)
|
||||
{
|
||||
int bytesRead;
|
||||
if ((bytesRead = s.Read(buffer, offset, count)) == 0)
|
||||
var bytesRead = s.Read(buffer, offset, count);
|
||||
if (bytesRead == 0)
|
||||
throw new EndOfStreamException();
|
||||
offset += bytesRead;
|
||||
count -= bytesRead;
|
||||
@@ -48,6 +62,7 @@ namespace OpenRA
|
||||
var b = s.ReadByte();
|
||||
if (b == -1)
|
||||
return -1;
|
||||
|
||||
s.Seek(-1, SeekOrigin.Current);
|
||||
return (byte)b;
|
||||
}
|
||||
@@ -57,52 +72,85 @@ namespace OpenRA
|
||||
var b = s.ReadByte();
|
||||
if (b == -1)
|
||||
throw new EndOfStreamException();
|
||||
|
||||
return (byte)b;
|
||||
}
|
||||
|
||||
public static ushort ReadUInt16(this Stream s)
|
||||
{
|
||||
return (ushort)(s.ReadUInt8() | s.ReadUInt8() << 8);
|
||||
Span<byte> buffer = stackalloc byte[2];
|
||||
s.ReadBytes(buffer);
|
||||
return BitConverter.ToUInt16(buffer);
|
||||
}
|
||||
|
||||
public static short ReadInt16(this Stream s)
|
||||
{
|
||||
return (short)(s.ReadUInt8() | s.ReadUInt8() << 8);
|
||||
Span<byte> buffer = stackalloc byte[2];
|
||||
s.ReadBytes(buffer);
|
||||
return BitConverter.ToInt16(buffer);
|
||||
}
|
||||
|
||||
public static uint ReadUInt32(this Stream s)
|
||||
{
|
||||
return (uint)(s.ReadUInt8() | s.ReadUInt8() << 8 | s.ReadUInt8() << 16 | s.ReadUInt8() << 24);
|
||||
Span<byte> buffer = stackalloc byte[4];
|
||||
s.ReadBytes(buffer);
|
||||
return BitConverter.ToUInt32(buffer);
|
||||
}
|
||||
|
||||
public static int ReadInt32(this Stream s)
|
||||
{
|
||||
return s.ReadUInt8() | s.ReadUInt8() << 8 | s.ReadUInt8() << 16 | s.ReadUInt8() << 24;
|
||||
Span<byte> buffer = stackalloc byte[4];
|
||||
s.ReadBytes(buffer);
|
||||
return BitConverter.ToInt32(buffer);
|
||||
}
|
||||
|
||||
public static void Write(this Stream s, int value)
|
||||
{
|
||||
s.WriteArray(BitConverter.GetBytes(value));
|
||||
Span<byte> buffer = stackalloc byte[4];
|
||||
BitConverter.TryWriteBytes(buffer, value);
|
||||
s.Write(buffer);
|
||||
}
|
||||
|
||||
public static void Write(this Stream s, long value)
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[8];
|
||||
BitConverter.TryWriteBytes(buffer, value);
|
||||
s.Write(buffer);
|
||||
}
|
||||
|
||||
public static void Write(this Stream s, ulong value)
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[8];
|
||||
BitConverter.TryWriteBytes(buffer, value);
|
||||
s.Write(buffer);
|
||||
}
|
||||
|
||||
public static void Write(this Stream s, float value)
|
||||
{
|
||||
s.WriteArray(BitConverter.GetBytes(value));
|
||||
Span<byte> buffer = stackalloc byte[4];
|
||||
BitConverter.TryWriteBytes(buffer, value);
|
||||
s.Write(buffer);
|
||||
}
|
||||
|
||||
public static float ReadFloat(this Stream s)
|
||||
public static float ReadSingle(this Stream s)
|
||||
{
|
||||
return BitConverter.ToSingle(s.ReadBytes(4), 0);
|
||||
Span<byte> buffer = stackalloc byte[4];
|
||||
s.ReadBytes(buffer);
|
||||
return BitConverter.ToSingle(buffer);
|
||||
}
|
||||
|
||||
public static double ReadDouble(this Stream s)
|
||||
{
|
||||
return BitConverter.ToDouble(s.ReadBytes(8), 0);
|
||||
Span<byte> buffer = stackalloc byte[8];
|
||||
s.ReadBytes(buffer);
|
||||
return BitConverter.ToDouble(buffer);
|
||||
}
|
||||
|
||||
public static string ReadASCII(this Stream s, int length)
|
||||
{
|
||||
return new string(Encoding.ASCII.GetChars(s.ReadBytes(length)));
|
||||
Span<byte> buffer = length < 128 ? stackalloc byte[length] : new byte[length];
|
||||
s.ReadBytes(buffer);
|
||||
return Encoding.ASCII.GetString(buffer);
|
||||
}
|
||||
|
||||
public static string ReadASCIIZ(this Stream s)
|
||||
@@ -111,7 +159,12 @@ namespace OpenRA
|
||||
byte b;
|
||||
while ((b = s.ReadUInt8()) != 0)
|
||||
bytes.Add(b);
|
||||
return new string(Encoding.ASCII.GetChars(bytes.ToArray()));
|
||||
|
||||
#if NET5_0_OR_GREATER
|
||||
return Encoding.ASCII.GetString(System.Runtime.InteropServices.CollectionsMarshal.AsSpan(bytes));
|
||||
#else
|
||||
return Encoding.ASCII.GetString(bytes.ToArray());
|
||||
#endif
|
||||
}
|
||||
|
||||
public static string ReadAllText(this Stream s)
|
||||
@@ -133,17 +186,11 @@ namespace OpenRA
|
||||
int count;
|
||||
while ((count = s.Read(buffer, 0, buffer.Length)) > 0)
|
||||
bytes.AddRange(buffer.Take(count));
|
||||
|
||||
return bytes.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
// Note: renamed from Write() to avoid being aliased by
|
||||
// System.IO.Stream.Write(System.ReadOnlySpan) (which is not implemented in Mono)
|
||||
public static void WriteArray(this Stream s, byte[] data)
|
||||
{
|
||||
s.Write(data, 0, data.Length);
|
||||
}
|
||||
|
||||
public static IEnumerable<string> ReadAllLines(this Stream s)
|
||||
{
|
||||
string line;
|
||||
@@ -210,19 +257,24 @@ namespace OpenRA
|
||||
}
|
||||
}
|
||||
|
||||
// The string is assumed to be length-prefixed, as written by WriteString()
|
||||
public static string ReadString(this Stream s, Encoding encoding, int maxLength)
|
||||
/// <summary>
|
||||
/// The string is assumed to be length-prefixed, as written by <see cref="WriteLengthPrefixedString"/>.
|
||||
/// </summary>
|
||||
public static string ReadLengthPrefixedString(this Stream s, Encoding encoding, int maxLength)
|
||||
{
|
||||
var length = s.ReadInt32();
|
||||
if (length > maxLength)
|
||||
throw new InvalidOperationException($"The length of the string ({length}) is longer than the maximum allowed ({maxLength}).");
|
||||
|
||||
return encoding.GetString(s.ReadBytes(length));
|
||||
Span<byte> buffer = length < 128 ? stackalloc byte[length] : new byte[length];
|
||||
s.ReadBytes(buffer);
|
||||
return encoding.GetString(buffer);
|
||||
}
|
||||
|
||||
// Writes a length-prefixed string using the specified encoding and returns
|
||||
// the number of bytes written.
|
||||
public static int WriteString(this Stream s, Encoding encoding, string text)
|
||||
/// <summary>
|
||||
/// Writes a length-prefixed string using the specified encoding and returns the number of bytes written.
|
||||
/// </summary>
|
||||
public static int WriteLengthPrefixedString(this Stream s, Encoding encoding, string text)
|
||||
{
|
||||
byte[] bytes;
|
||||
|
||||
@@ -232,7 +284,7 @@ namespace OpenRA
|
||||
bytes = Array.Empty<byte>();
|
||||
|
||||
s.Write(bytes.Length);
|
||||
s.WriteArray(bytes);
|
||||
s.Write(bytes);
|
||||
|
||||
return 4 + bytes.Length;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user