Span-ify the unpacking of mini arcs

cuts out some of the internal c# sanity checking for better perf
This commit is contained in:
Kurt 2021-12-20 18:37:45 -08:00
parent 644addac3d
commit 38a4135bcd

View file

@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;
namespace PKHeX.Core
{
@ -10,22 +11,22 @@ namespace PKHeX.Core
/// <param name="fileData">Packed data</param>
/// <param name="identifier">Signature expected in the first two bytes (ASCII)</param>
/// <returns>Unpacked array containing all files that were packed.</returns>
public static byte[][] Unpack(byte[] fileData, string identifier)
public static byte[][] Unpack(ReadOnlySpan<byte> fileData, string identifier)
{
#if DEBUG
System.Diagnostics.Debug.Assert(fileData.Length > 4);
System.Diagnostics.Debug.Assert(identifier[0] == fileData[0] && identifier[1] == fileData[1]);
#endif
int count = BitConverter.ToUInt16(fileData, 2); int ctr = 4;
int start = BitConverter.ToInt32(fileData, ctr); ctr += 4;
MemoryMarshal.TryRead(fileData[4..], out int start);
MemoryMarshal.TryRead(fileData[2..], out ushort count);
var offsetBytes = fileData[8..(8 + (count * sizeof(int)))];
var offsets = MemoryMarshal.Cast<byte, int>(offsetBytes);
byte[][] returnData = new byte[count][];
for (int i = 0; i < count; i++)
for (int i = 0; i < offsets.Length; i++)
{
int end = BitConverter.ToInt32(fileData, ctr); ctr += 4;
int len = end - start;
byte[] data = new byte[len];
Buffer.BlockCopy(fileData, start, data, 0, len);
returnData[i] = data;
int end = offsets[i];
returnData[i] = fileData[start..end].ToArray();
start = end;
}
return returnData;