From f4bfdb8311ef6636ec6bb488a60b2185bafb6f55 Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 1 Aug 2018 18:30:51 -0700 Subject: [PATCH] Remove unnecessary empty array allocations yay net 4.6 read more: http://justinvp.com/2015/07/20/array-empty/ --- PKHeX.Core/Game/GameStrings/GameStrings.cs | 2 +- PKHeX.Core/Legality/Core.cs | 4 ++-- PKHeX.Core/Legality/Encounters/EncounterLink.cs | 2 +- .../Encounters/Information/ValidEncounterMoves.cs | 5 +++-- .../Encounters/Verifiers/VerifyCurrentMoves.cs | 14 +++++++------- PKHeX.Core/Legality/Learnset/Learnset.cs | 10 ++++++---- PKHeX.Core/Legality/Learnset/Learnset6.cs | 6 +++++- PKHeX.Core/Legality/Moves/MoveEgg.cs | 9 +++++---- PKHeX.Core/Legality/Moves/MoveParseSource.cs | 6 ++++-- PKHeX.Core/Legality/Structures/EggMoves.cs | 7 ++++--- PKHeX.Core/MysteryGifts/WC3.cs | 2 +- PKHeX.Core/PersonalInfo/PersonalInfo.cs | 6 ++++-- PKHeX.Core/PersonalInfo/PersonalInfoG4.cs | 2 +- PKHeX.Core/Saves/SAV7.cs | 4 ++-- PKHeX.Core/Saves/SaveFile.cs | 6 +++--- PKHeX.Core/Util/DataUtil.cs | 2 +- PKHeX.WinForms/Subforms/PKM Editors/Text.cs | 2 +- 17 files changed, 51 insertions(+), 38 deletions(-) diff --git a/PKHeX.Core/Game/GameStrings/GameStrings.cs b/PKHeX.Core/Game/GameStrings/GameStrings.cs index 9ba6a857d..b0302fd36 100644 --- a/PKHeX.Core/Game/GameStrings/GameStrings.cs +++ b/PKHeX.Core/Game/GameStrings/GameStrings.cs @@ -244,7 +244,7 @@ public IReadOnlyList GetItemStrings(int generation, GameVersion game = G { switch (generation) { - case 0: return new string[0]; + case 0: return Array.Empty(); case 1: return g1items; case 2: return g2items; case 3: return GetItemStrings3(game); diff --git a/PKHeX.Core/Legality/Core.cs b/PKHeX.Core/Legality/Core.cs index e2f3276ba..c5d273186 100644 --- a/PKHeX.Core/Legality/Core.cs +++ b/PKHeX.Core/Legality/Core.cs @@ -316,7 +316,7 @@ internal static int[] GetBaseEggMoves(PKM pkm, int species, GameVersion gameSour } break; } - return new int[0]; + return Array.Empty(); } internal static List GetValidPostEvolutionMoves(PKM pkm, int Species, IReadOnlyList[] evoChains, GameVersion Version) @@ -843,7 +843,7 @@ private static bool[] GetReleasedHeldItems(int generation) case 5: return ReleasedHeldItems_5; case 6: return ReleasedHeldItems_6; case 7: return ReleasedHeldItems_7; - default: return new bool[0]; + default: return Array.Empty(); } } diff --git a/PKHeX.Core/Legality/Encounters/EncounterLink.cs b/PKHeX.Core/Legality/Encounters/EncounterLink.cs index 031e84f0b..101268cf9 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterLink.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterLink.cs @@ -21,7 +21,7 @@ public class EncounterLink : IEncounterable, IRibbonSetEvent4, IMoveset, ILocati public int EggLocation { get => 0; set { } } public GameVersion Version { get; set; } = GameVersion.Gen6; - public int[] Moves { get; set; } = new int[0]; + public int[] Moves { get; set; } = Array.Empty(); public string Name => "Pokémon Link Gift"; diff --git a/PKHeX.Core/Legality/Encounters/Information/ValidEncounterMoves.cs b/PKHeX.Core/Legality/Encounters/Information/ValidEncounterMoves.cs index 92e124229..47df44429 100644 --- a/PKHeX.Core/Legality/Encounters/Information/ValidEncounterMoves.cs +++ b/PKHeX.Core/Legality/Encounters/Information/ValidEncounterMoves.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; namespace PKHeX.Core @@ -13,7 +14,7 @@ public class ValidEncounterMoves public List[] LevelUpMoves { get; } = Empty; public List[] TMHMMoves { get; } = Empty; public List[] TutorMoves { get; } = Empty; - public int[] Relearn = new int[0]; + public int[] Relearn = Array.Empty(); public int MinimumLevelGen1 { get; } public int MinimumLevelGen2 { get; } diff --git a/PKHeX.Core/Legality/Encounters/Verifiers/VerifyCurrentMoves.cs b/PKHeX.Core/Legality/Encounters/Verifiers/VerifyCurrentMoves.cs index bbaa1b5be..c3217e446 100644 --- a/PKHeX.Core/Legality/Encounters/Verifiers/VerifyCurrentMoves.cs +++ b/PKHeX.Core/Legality/Encounters/Verifiers/VerifyCurrentMoves.cs @@ -95,14 +95,14 @@ private static CheckMoveResult[] ParseMovesWasEggPreRelearn(PKM pkm, int[] Moves var TradebackPreevo = pkm.Format == 2 && info.EncounterMatch.Species > 151; var NonTradebackLvlMoves = TradebackPreevo ? Legal.GetExclusivePreEvolutionMoves(pkm, info.EncounterMatch.Species, info.EvoChainsAllGens[2], 2, e.Version).Where(m => m > Legal.MaxMoveID_1).ToArray() - : new int[0]; + : Array.Empty(); var Egg = MoveEgg.GetEggMoves(pkm, e.Species, pkm.AltForm, e.Version); if (info.Generation < 3 && pkm.Format >= 7 && pkm.VC1) Egg = Egg.Where(m => m <= Legal.MaxMoveID_1).ToArray(); bool volt = (info.Generation > 3 || e.Version == GameVersion.E) && Legal.LightBall.Contains(pkm.Species); - var Special = volt && EventEggMoves.Length == 0 ? new[] { 344 } : new int[0]; // Volt Tackle for bred Pichu line + var Special = volt && EventEggMoves.Length == 0 ? new[] { 344 } : Array.Empty(); // Volt Tackle for bred Pichu line var source = new MoveParseSource { @@ -127,7 +127,7 @@ private static CheckMoveResult[] ParseMovesSketch(PKM pkm, int[] Moves) } private static CheckMoveResult[] ParseMoves3DS(PKM pkm, int[] Moves, LegalInfo info) { - info.EncounterMoves.Relearn = info.Generation >= 6 ? pkm.RelearnMoves : new int[0]; + info.EncounterMoves.Relearn = info.Generation >= 6 ? pkm.RelearnMoves : Array.Empty(); if (info.EncounterMatch is IMoveset) return ParseMovesSpecialMoveset(pkm, Moves, info); @@ -155,7 +155,7 @@ private static CheckMoveResult[] ParseMovesGenGB(PKM pkm, int[] Moves, LegalInfo var G1Encounter = info.EncounterMatch; if (G1Encounter == null) return ParseMovesSpecialMoveset(pkm, Moves, info); - var InitialMoves = new int[0]; + var InitialMoves = Array.Empty(); int[] SpecialMoves = GetSpecialMoves(info.EncounterMatch); var games = info.EncounterMatch is IGeneration g && g.Generation == 1 ? Legal.GetGen1Versions(info) : Legal.GetGen2Versions(info); foreach (var ver in games) @@ -189,8 +189,8 @@ private static CheckMoveResult[] ParseMovesSpecialMoveset(PKM pkm, int[] Moves, private static int[] GetSpecialMoves(IEncounterable EncounterMatch) { if (EncounterMatch is IMoveset mg) - return mg.Moves ?? new int[0]; - return new int[0]; + return mg.Moves ?? Array.Empty(); + return Array.Empty(); } private static CheckMoveResult[] ParseMovesRelearn(PKM pkm, int[] Moves, LegalInfo info) { @@ -848,7 +848,7 @@ private static int[] GetGenMovesCheckOrderGB(PKM pkm, int originalGeneration) private static int[] GetGenMovesOrder(int start, int end) { if (end < 0) - return new int[0]; + return Array.Empty(); if (start <= end) return new[] {start}; var order = new int[start - end + 1]; diff --git a/PKHeX.Core/Legality/Learnset/Learnset.cs b/PKHeX.Core/Legality/Learnset/Learnset.cs index d5d706fb6..ff1dc0787 100644 --- a/PKHeX.Core/Legality/Learnset/Learnset.cs +++ b/PKHeX.Core/Legality/Learnset/Learnset.cs @@ -34,13 +34,13 @@ public int[] GetMoves(int maxLevel, int minLevel = 0) if (minLevel <= 1 && maxLevel >= 100) return Moves; if (minLevel > maxLevel) - return new int[0]; + return Array.Empty(); int start = Array.FindIndex(Levels, z => z >= minLevel); if (start < 0) - return new int[0]; + return Array.Empty(); int end = Array.FindLastIndex(Levels, z => z <= maxLevel); if (end < 0) - return new int[0]; + return Array.Empty(); int[] result = new int[end - start + 1]; Array.Copy(Moves, start, result, 0, result.Length); return result; @@ -147,14 +147,16 @@ public int GetMinMoveLevel(int level) return Math.Max(end - 4, 1); } - private Dictionary Learn; + private Dictionary GetDictionary() { var dict = new Dictionary(); for (int i = 0; i < Moves.Length; i++) + { if (!dict.ContainsKey(Moves[i])) dict.Add(Moves[i], Levels[i]); + } return dict; } diff --git a/PKHeX.Core/Legality/Learnset/Learnset6.cs b/PKHeX.Core/Legality/Learnset/Learnset6.cs index df4d56d55..cc1da6dad 100644 --- a/PKHeX.Core/Legality/Learnset/Learnset6.cs +++ b/PKHeX.Core/Legality/Learnset/Learnset6.cs @@ -1,3 +1,4 @@ +using System; using System.IO; namespace PKHeX.Core @@ -10,18 +11,21 @@ public sealed class Learnset6 : Learnset private Learnset6(byte[] data) { if (data.Length < 4 || data.Length % 4 != 0) - { Count = 0; Levels = new int[0]; Moves = new int[0]; return; } + { Count = 0; Levels = Moves = Array.Empty(); return; } Count = (data.Length / 4) - 1; Moves = new int[Count]; Levels = new int[Count]; using (var ms = new MemoryStream(data)) using (var br = new BinaryReader(ms)) + { for (int i = 0; i < Count; i++) { Moves[i] = br.ReadInt16(); Levels[i] = br.ReadInt16(); } + } } + public static Learnset[] GetArray(byte[][] entries) { Learnset[] data = new Learnset[entries.Length]; diff --git a/PKHeX.Core/Legality/Moves/MoveEgg.cs b/PKHeX.Core/Legality/Moves/MoveEgg.cs index 1a49d5c88..b8677958a 100644 --- a/PKHeX.Core/Legality/Moves/MoveEgg.cs +++ b/PKHeX.Core/Legality/Moves/MoveEgg.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using static PKHeX.Core.Legal; namespace PKHeX.Core @@ -9,7 +10,7 @@ internal static int[] GetEggMoves(PKM pkm, int species, int formnum, GameVersion { int gen = pkm.Format <= 2 || pkm.VC ? 2 : pkm.GenNumber; if (!pkm.InhabitedGeneration(gen, species) || pkm.PersonalInfo.Gender == 255 && !FixedGenderFromBiGender.Contains(species)) - return new int[0]; + return Array.Empty(); if (version == GameVersion.Any) version = (GameVersion)pkm.Version; return GetEggMoves(gen, species, formnum, version); @@ -55,7 +56,7 @@ private static int[] GetEggMoves(int gen, int species, int formnum, GameVersion } default: - return new int[0]; + return Array.Empty(); } } @@ -91,7 +92,7 @@ internal static int[] GetRelearnLVLMoves(PKM pkm, int species, int lvl, int form case GameVersion.UM: return getMoves(LevelUpUSUM, PersonalTable.USUM); } - return new int[0]; + return Array.Empty(); int[] getMoves(IReadOnlyList moves, PersonalTable table) => moves[table.GetFormeIndex(species, formnum)].GetMoves(lvl); } diff --git a/PKHeX.Core/Legality/Moves/MoveParseSource.cs b/PKHeX.Core/Legality/Moves/MoveParseSource.cs index 715172110..d87bc7b4d 100644 --- a/PKHeX.Core/Legality/Moves/MoveParseSource.cs +++ b/PKHeX.Core/Legality/Moves/MoveParseSource.cs @@ -1,8 +1,10 @@ -namespace PKHeX.Core +using System; + +namespace PKHeX.Core { internal class MoveParseSource { - private static readonly int[] Empty = new int[0]; + private static readonly int[] Empty = Array.Empty(); public int[] CurrentMoves { get; set; } = Empty; public int[] SpecialSource { get; set; } = Empty; public int[] NonTradeBackLevelUpMoves { get; set; } = Empty; diff --git a/PKHeX.Core/Legality/Structures/EggMoves.cs b/PKHeX.Core/Legality/Structures/EggMoves.cs index db9608bb1..375c5018e 100644 --- a/PKHeX.Core/Legality/Structures/EggMoves.cs +++ b/PKHeX.Core/Legality/Structures/EggMoves.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Linq; namespace PKHeX.Core @@ -39,7 +40,7 @@ public class EggMoves6 : EggMoves private EggMoves6(byte[] data) { if (data.Length < 2 || data.Length % 2 != 0) - { Count = 0; Moves = new int[0]; return; } + { Count = 0; Moves = Array.Empty(); return; } using (BinaryReader br = new BinaryReader(new MemoryStream(data))) { Moves = new int[Count = br.ReadUInt16()]; @@ -60,7 +61,7 @@ public class EggMoves7 : EggMoves private EggMoves7(byte[] data) { if (data.Length < 2 || data.Length % 2 != 0) - { Count = 0; Moves = new int[0]; return; } + { Count = 0; Moves = Array.Empty(); return; } using (BinaryReader br = new BinaryReader(new MemoryStream(data))) { FormTableIndex = br.ReadUInt16(); diff --git a/PKHeX.Core/MysteryGifts/WC3.cs b/PKHeX.Core/MysteryGifts/WC3.cs index 4b6934104..6d577d84f 100644 --- a/PKHeX.Core/MysteryGifts/WC3.cs +++ b/PKHeX.Core/MysteryGifts/WC3.cs @@ -28,7 +28,7 @@ public class WC3 : MysteryGift, IRibbonSetEvent3, IVersion public int Language { get; set; } = -1; public override int Species { get; set; } public override bool IsEgg { get; set; } - public override int[] Moves { get; set; } = new int[0]; + public override int[] Moves { get; set; } = Array.Empty(); public bool NotDistributed { get; set; } public Shiny Shiny { get; set; } = Shiny.Random; public bool Fateful { get; set; } // Obedience Flag diff --git a/PKHeX.Core/PersonalInfo/PersonalInfo.cs b/PKHeX.Core/PersonalInfo/PersonalInfo.cs index fdd641385..40668f8a2 100644 --- a/PKHeX.Core/PersonalInfo/PersonalInfo.cs +++ b/PKHeX.Core/PersonalInfo/PersonalInfo.cs @@ -1,4 +1,6 @@ -namespace PKHeX.Core +using System; + +namespace PKHeX.Core { /// /// Stat/misc data for individual species or their associated alternate forme data. @@ -234,7 +236,7 @@ public int[] EggGroups /// /// Special tutor learn compatibility flags for individual moves. /// - public bool[][] SpecialTutors { get; protected set; } = new bool[0][]; + public bool[][] SpecialTutors { get; protected set; } = Array.Empty(); protected static bool[] GetBits(byte[] data, int start = 0, int length = -1) { diff --git a/PKHeX.Core/PersonalInfo/PersonalInfoG4.cs b/PKHeX.Core/PersonalInfo/PersonalInfoG4.cs index 16f43cc66..0fbae100b 100644 --- a/PKHeX.Core/PersonalInfo/PersonalInfoG4.cs +++ b/PKHeX.Core/PersonalInfo/PersonalInfoG4.cs @@ -17,7 +17,7 @@ public PersonalInfoG4(byte[] data) // Unpack TMHM & Tutors TMHM = GetBits(Data, 0x1C, 0x0D); - TypeTutors = new bool[0]; // not stored in personal + TypeTutors = Array.Empty(); // not stored in personal } public override byte[] Write() diff --git a/PKHeX.Core/Saves/SAV7.cs b/PKHeX.Core/Saves/SAV7.cs index d945dcc4f..ccbcd34db 100644 --- a/PKHeX.Core/Saves/SAV7.cs +++ b/PKHeX.Core/Saves/SAV7.cs @@ -42,8 +42,8 @@ public SAV7(byte[] data = null) if (demo || !Exportable) { PokeDex = -1; // Disabled - LockedSlots = new int[0]; - TeamSlots = new int[0]; + LockedSlots = Array.Empty(); + TeamSlots = Array.Empty(); } else // Valid slot locking info present { diff --git a/PKHeX.Core/Saves/SaveFile.cs b/PKHeX.Core/Saves/SaveFile.cs index 86349a05e..5ab3e8022 100644 --- a/PKHeX.Core/Saves/SaveFile.cs +++ b/PKHeX.Core/Saves/SaveFile.cs @@ -237,7 +237,7 @@ public IList BattleBoxData get { if (!HasBattleBox) - return new PKM[0]; + return Array.Empty(); PKM[] data = new PKM[6]; for (int i = 0; i < data.Length; i++) @@ -444,8 +444,8 @@ public bool IsPartyAllEggs(params int[] except) public virtual int BoxesUnlocked { get => -1; set { } } public virtual byte[] BoxFlags { get => null; set { } } public virtual int CurrentBox { get => 0; set { } } - protected int[] LockedSlots = new int[0]; - protected int[] TeamSlots = new int[0]; + protected int[] LockedSlots = Array.Empty(); + protected int[] TeamSlots = Array.Empty(); public bool MoveBox(int box, int insertBeforeBox) { if (box == insertBeforeBox) // no movement required diff --git a/PKHeX.Core/Util/DataUtil.cs b/PKHeX.Core/Util/DataUtil.cs index e9ed459b7..d5bc693b0 100644 --- a/PKHeX.Core/Util/DataUtil.cs +++ b/PKHeX.Core/Util/DataUtil.cs @@ -81,7 +81,7 @@ public static string[] GetStringList(string f) return (string[])stringListCache[f].Clone(); var txt = GetStringResource(f); // Fetch File, \n to list. - if (txt == null) return new string[0]; + if (txt == null) return Array.Empty(); string[] rawlist = txt.Split('\n'); for (int i = 0; i < rawlist.Length; i++) rawlist[i] = rawlist[i].TrimEnd('\r'); diff --git a/PKHeX.WinForms/Subforms/PKM Editors/Text.cs b/PKHeX.WinForms/Subforms/PKM Editors/Text.cs index 47d282408..89fb415e4 100644 --- a/PKHeX.WinForms/Subforms/PKM Editors/Text.cs +++ b/PKHeX.WinForms/Subforms/PKM Editors/Text.cs @@ -200,7 +200,7 @@ private static ushort[] GetChars(int generation) case 6: case 7: return chars67; - default: return new ushort[0]; + default: return Array.Empty(); } } private static readonly ushort[] chars67 =