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

@@ -9,7 +9,7 @@
*/
#endregion
using System.IO;
using System;
namespace OpenRA.Mods.Common.FileFormats
{
@@ -56,15 +56,14 @@ namespace OpenRA.Mods.Common.FileFormats
return (short)current;
}
public static byte[] LoadImaAdpcmSound(byte[] raw, ref int index)
public static byte[] LoadImaAdpcmSound(ReadOnlySpan<byte> raw, ref int index)
{
var currentSample = 0;
return LoadImaAdpcmSound(raw, ref index, ref currentSample);
}
public static byte[] LoadImaAdpcmSound(byte[] raw, ref int index, ref int currentSample)
public static byte[] LoadImaAdpcmSound(ReadOnlySpan<byte> raw, ref int index, ref int currentSample)
{
var s = new MemoryStream(raw);
var dataSize = raw.Length;
var outputSize = raw.Length * 4;
@@ -73,7 +72,7 @@ namespace OpenRA.Mods.Common.FileFormats
while (dataSize-- > 0)
{
var b = s.ReadUInt8();
var b = raw[offset / 4];
var t = DecodeImaAdpcmSample(b, ref index, ref currentSample);
output[offset++] = (byte)t;