Minor perf improvements

GetHexStringFromBytes: don't allocate temp slice array on heap
Tile: Span Slice toarray rather than new+copy
This commit is contained in:
Kurt 2021-05-23 10:56:25 -07:00
parent 15afd6f3bc
commit b2e4ca393b
3 changed files with 12 additions and 14 deletions

View file

@ -99,7 +99,7 @@ namespace PKHeX.Core
// Well, (a ^ a) = 0. so (block first ^ subkey) ^ (block last ^ subkey)
// = block first ^ block last ;)
Array.Copy(outdata, ((data.Length / 0x10) - 1) * 0x10, temp, 0, 0x10);
temp = Xor(temp, outdata.Slice(0, 0x10));
temp = Xor(temp, outdata.AsSpan(0, 0x10));
var subkey = GetSubKey(temp);
for (var i = 0; i < data.Length / 0x10; i++)
{
@ -145,7 +145,7 @@ namespace PKHeX.Core
}
// In between - CMAC stuff
var inbet = outdata.Slice(0, 0x10);
var inbet = outdata.AsSpan(0, 0x10);
temp = Xor(temp, inbet);
var subkey = GetSubKey(temp);
@ -183,7 +183,7 @@ namespace PKHeX.Core
return subkey;
}
private static byte[] Xor(byte[] b1, byte[] b2)
private static byte[] Xor(byte[] b1, ReadOnlySpan<byte> b2)
{
Debug.Assert(b1.Length == b2.Length);
var x = new byte[b1.Length];

View file

@ -58,20 +58,18 @@ namespace PKHeX.Core
_cgb = data;
}
byte[] Region1 = data.Slice(0, 0x1FE0);
byte[] ColorData = data.Slice(0x1FE0, 0x20);
byte[] Region2 = data.Slice(0x2000, 0x600);
var Region1 = data.AsSpan(0, 0x1FE0);
var ColorData = data.Slice(0x1FE0, 0x20);
var Region2 = data.Slice(0x2000, 0x600);
ColorPalette = new int[ColorCount];
for (int i = 0; i < ColorPalette.Length; i++)
ColorPalette[i] = GetRGB555_16(BitConverter.ToUInt16(ColorData, i * 2));
Tiles = new Tile[0xFF];
for (int i = 0; i < 0xFF; i++)
for (int i = 0; i < Tiles.Length; i++)
{
byte[] tiledata = new byte[Tile.SIZE_TILE];
Array.Copy(Region1, i * Tile.SIZE_TILE, tiledata, 0, Tile.SIZE_TILE);
byte[] tiledata = Region1.Slice(i * Tile.SIZE_TILE, Tile.SIZE_TILE).ToArray();
Tiles[i] = new Tile(tiledata);
Tiles[i].SetTile(ColorPalette);
}

View file

@ -134,10 +134,10 @@ namespace PKHeX.Core
public static string GetHexStringFromBytes(byte[] data, int offset, int length)
{
data = data.Slice(offset, length);
var sb = new StringBuilder(data.Length * 2);
for (int i = data.Length - 1; i >= 0; i--)
sb.AppendFormat("{0:X2}", data[i]);
var arr = data.AsSpan(offset, length);
var sb = new StringBuilder(arr.Length * 2);
for (int i = arr.Length - 1; i >= 0; i--)
sb.AppendFormat("{0:X2}", arr[i]);
return sb.ToString();
}