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

@@ -67,12 +67,12 @@ namespace OpenRA.Mods.Common.FileFormats
blockAlign = s.ReadInt16();
sampleBits = s.ReadInt16();
lengthInSeconds = (float)(s.Length * 8) / (channels * sampleRate * sampleBits);
s.ReadBytes(fmtChunkSize - 16);
s.Position += fmtChunkSize - 16;
break;
case "fact":
var chunkSize = s.ReadInt32();
uncompressedSize = s.ReadInt32();
s.ReadBytes(chunkSize - 4);
s.Position += chunkSize - 4;
break;
case "data":
dataSize = s.ReadInt32();
@@ -82,7 +82,7 @@ namespace OpenRA.Mods.Common.FileFormats
case "LIST":
case "cue ":
var listCueChunkSize = s.ReadInt32();
s.ReadBytes(listCueChunkSize);
s.Position += listCueChunkSize;
break;
default:
s.Position = s.Length; // Skip to end of stream
@@ -156,12 +156,13 @@ namespace OpenRA.Mods.Common.FileFormats
// Decode and output remaining data in this block
var blockOffset = 0;
Span<byte> chunk = stackalloc byte[4];
while (blockOffset < blockDataSize)
{
for (var c = 0; c < channels; c++)
{
// Decode 4 bytes (to 16 bytes of output) per channel
var chunk = baseStream.ReadBytes(4);
baseStream.ReadBytes(chunk);
var decoded = ImaAdpcmReader.LoadImaAdpcmSound(chunk, ref index[c], ref predictor[c]);
// Interleave output, one sample per channel