Preallocate some holding objects to prevent doubling allocations

Saves 12 doubling reallocations for new List to hold blocks
Saves 21 for the memorystream on writing
This commit is contained in:
Kurt 2021-01-07 09:37:27 -08:00
parent 6e741a0cf0
commit 1c19bc86f4
2 changed files with 7 additions and 4 deletions

View file

@ -7,7 +7,7 @@ namespace PKHeX.Core
/// MemeCrypto V1 - The Original Series
/// </summary>
/// <remarks>
/// A variant of <see cref="SaveFile"/> encryption and obfuscation, used in <see cref="GameVersion.Gen7"/>.
/// A variant of <see cref="SaveFile"/> encryption and obfuscation used in <see cref="GameVersion.Gen7"/>.
/// <br> The save file stores a dedicated block to contain a hash of the savedata, computed when the block is zeroed. </br>
/// <br> This signing logic is reused for other authentication; refer to <see cref="MemeKeyIndex"/>. </br>
/// <br> The save file first computes a SHA256 Hash over the block checksum region.

View file

@ -111,9 +111,12 @@ namespace PKHeX.Core
return temp;
}
private const int BlockDataRatioEstimate1 = 777; // bytes per block, on average (generous)
private const int BlockDataRatioEstimate2 = 555; // bytes per block, on average (stingy)
private static IReadOnlyList<SCBlock> ReadBlocks(byte[] data)
{
var result = new List<SCBlock>();
var result = new List<SCBlock>(data.Length / BlockDataRatioEstimate2);
int offset = 0;
while (offset < data.Length - SIZE_HASH)
{
@ -144,9 +147,9 @@ namespace PKHeX.Core
/// Tries to encrypt the save data.
/// </summary>
/// <returns>Raw save data without the final xorpad layer.</returns>
public static byte[] GetDecryptedRawData(IEnumerable<SCBlock> blocks)
public static byte[] GetDecryptedRawData(IReadOnlyList<SCBlock> blocks)
{
using var ms = new MemoryStream();
using var ms = new MemoryStream(blocks.Count * BlockDataRatioEstimate1);
using var bw = new BinaryWriter(ms);
foreach (var block in blocks)
block.WriteBlock(bw);