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

@@ -199,16 +199,16 @@ namespace OpenRA.Mods.Cnc.FileFormats
else if (AudioChannels == 1)
{
var rawAudio = stream.ReadBytes((int)length);
audio1.WriteArray(rawAudio);
audio1.Write(rawAudio);
}
else
{
var rawAudio = stream.ReadBytes((int)length / 2);
audio1.WriteArray(rawAudio);
audio1.Write(rawAudio);
rawAudio = stream.ReadBytes((int)length / 2);
audio2.WriteArray(rawAudio);
audio2.Write(rawAudio);
if (length % 2 != 0)
stream.ReadBytes(2);
stream.Position += 2;
}
compressed = type == "SND2";
@@ -216,7 +216,7 @@ namespace OpenRA.Mods.Cnc.FileFormats
default:
if (length + stream.Position > stream.Length)
throw new NotSupportedException($"Vqa uses unknown Subtype: {type}");
stream.ReadBytes((int)length);
stream.Position += length;
break;
}
@@ -308,7 +308,7 @@ namespace OpenRA.Mods.Cnc.FileFormats
break;
default:
// Don't parse sound here.
stream.ReadBytes((int)length);
stream.Position += length;
break;
}
@@ -382,8 +382,7 @@ namespace OpenRA.Mods.Cnc.FileFormats
// frame-modifier chunk
case "CBP0":
case "CBPZ":
var bytes = s.ReadBytes(subchunkLength);
bytes.CopyTo(cbp, chunkBufferOffset);
s.ReadBytes(cbp, chunkBufferOffset, subchunkLength);
chunkBufferOffset += subchunkLength;
currentChunkBuffer++;
cbpIsCompressed = type == "CBPZ";