From 9f20aa2f4fd65c6eb756cb1656425a22a2e4debe Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 19 Jul 2020 16:35:31 -0500 Subject: [PATCH] Remove unnecessary comparison in Array.Resize, an array is only created if the size is not equivalent we're just repeating the same logic; let the jit optimize out the null check --- PKHeX.Core/PKM/PKM.cs | 15 ++++----------- PKHeX.Core/Util/ArrayUtil.cs | 6 ++++++ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/PKHeX.Core/PKM/PKM.cs b/PKHeX.Core/PKM/PKM.cs index 7b1637501..7ba029cc2 100644 --- a/PKHeX.Core/PKM/PKM.cs +++ b/PKHeX.Core/PKM/PKM.cs @@ -22,17 +22,10 @@ public abstract class PKM : ITrainerID, ILangNick, IGameValueLimit, INature public int Box { get; set; } = -1; // Batch Editor public int Slot { get; set; } = -1; // Batch Editor - public virtual byte[] EncryptedPartyData => Truncate(Encrypt(), SIZE_PARTY); - public virtual byte[] EncryptedBoxData => Truncate(Encrypt(), SIZE_STORED); - public virtual byte[] DecryptedPartyData => Truncate(Write(), SIZE_PARTY); - public virtual byte[] DecryptedBoxData => Truncate(Write(), SIZE_STORED); - - private static byte[] Truncate(byte[] data, int newSize) - { - if (data.Length != newSize) - Array.Resize(ref data, newSize); - return data; - } + public virtual byte[] EncryptedPartyData => ArrayUtil.Truncate(Encrypt(), SIZE_PARTY); + public virtual byte[] EncryptedBoxData => ArrayUtil.Truncate(Encrypt(), SIZE_STORED); + public virtual byte[] DecryptedPartyData => ArrayUtil.Truncate(Write(), SIZE_PARTY); + public virtual byte[] DecryptedBoxData => ArrayUtil.Truncate(Write(), SIZE_STORED); public virtual bool Valid { get => ChecksumValid && Sanity == 0; set { if (!value) return; Sanity = 0; RefreshChecksum(); } } diff --git a/PKHeX.Core/Util/ArrayUtil.cs b/PKHeX.Core/Util/ArrayUtil.cs index ace99e458..0276aca1a 100644 --- a/PKHeX.Core/Util/ArrayUtil.cs +++ b/PKHeX.Core/Util/ArrayUtil.cs @@ -21,6 +21,12 @@ public static class ArrayUtil return true; } + public static byte[] Truncate(byte[] data, int newSize) + { + Array.Resize(ref data, newSize); + return data; + } + public static byte[] Slice(this byte[] src, int offset, int length) { byte[] data = new byte[length];