Remove unnecessary empty array allocations

yay net 4.6
read more: http://justinvp.com/2015/07/20/array-empty/
This commit is contained in:
Kurt
2018-08-01 18:30:51 -07:00
parent fe6c071d72
commit f4bfdb8311
17 changed files with 51 additions and 38 deletions

View File

@@ -244,7 +244,7 @@ public IReadOnlyList<string> GetItemStrings(int generation, GameVersion game = G
{
switch (generation)
{
case 0: return new string[0];
case 0: return Array.Empty<string>();
case 1: return g1items;
case 2: return g2items;
case 3: return GetItemStrings3(game);

View File

@@ -316,7 +316,7 @@ internal static int[] GetBaseEggMoves(PKM pkm, int species, GameVersion gameSour
}
break;
}
return new int[0];
return Array.Empty<int>();
}
internal static List<int> GetValidPostEvolutionMoves(PKM pkm, int Species, IReadOnlyList<EvoCriteria>[] 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<bool>();
}
}

View File

@@ -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<int>();
public string Name => "Pokémon Link Gift";

View File

@@ -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<int>[] LevelUpMoves { get; } = Empty;
public List<int>[] TMHMMoves { get; } = Empty;
public List<int>[] TutorMoves { get; } = Empty;
public int[] Relearn = new int[0];
public int[] Relearn = Array.Empty<int>();
public int MinimumLevelGen1 { get; }
public int MinimumLevelGen2 { get; }

View File

@@ -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<int>();
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<int>(); // 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<int>();
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>();
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<int>();
return Array.Empty<int>();
}
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<int>();
if (start <= end)
return new[] {start};
var order = new int[start - end + 1];

View File

@@ -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>();
int start = Array.FindIndex(Levels, z => z >= minLevel);
if (start < 0)
return new int[0];
return Array.Empty<int>();
int end = Array.FindLastIndex(Levels, z => z <= maxLevel);
if (end < 0)
return new int[0];
return Array.Empty<int>();
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<int, int> Learn;
private Dictionary<int, int> GetDictionary()
{
var dict = new Dictionary<int, int>();
for (int i = 0; i < Moves.Length; i++)
{
if (!dict.ContainsKey(Moves[i]))
dict.Add(Moves[i], Levels[i]);
}
return dict;
}

View File

@@ -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<int>(); 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];

View File

@@ -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<int>();
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<int>();
}
}
@@ -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>();
int[] getMoves(IReadOnlyList<Learnset> moves, PersonalTable table) => moves[table.GetFormeIndex(species, formnum)].GetMoves(lvl);
}

View File

@@ -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<int>();
public int[] CurrentMoves { get; set; } = Empty;
public int[] SpecialSource { get; set; } = Empty;
public int[] NonTradeBackLevelUpMoves { get; set; } = Empty;

View File

@@ -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<int>(); 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<int>(); return; }
using (BinaryReader br = new BinaryReader(new MemoryStream(data)))
{
FormTableIndex = br.ReadUInt16();

View File

@@ -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<int>();
public bool NotDistributed { get; set; }
public Shiny Shiny { get; set; } = Shiny.Random;
public bool Fateful { get; set; } // Obedience Flag

View File

@@ -1,4 +1,6 @@
namespace PKHeX.Core
using System;
namespace PKHeX.Core
{
/// <summary>
/// Stat/misc data for individual species or their associated alternate forme data.
@@ -234,7 +236,7 @@ public int[] EggGroups
/// <summary>
/// Special tutor learn compatibility flags for individual moves.
/// </summary>
public bool[][] SpecialTutors { get; protected set; } = new bool[0][];
public bool[][] SpecialTutors { get; protected set; } = Array.Empty<bool[]>();
protected static bool[] GetBits(byte[] data, int start = 0, int length = -1)
{

View File

@@ -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<bool>(); // not stored in personal
}
public override byte[] Write()

View File

@@ -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<int>();
TeamSlots = Array.Empty<int>();
}
else // Valid slot locking info present
{

View File

@@ -237,7 +237,7 @@ public IList<PKM> BattleBoxData
get
{
if (!HasBattleBox)
return new PKM[0];
return Array.Empty<PKM>();
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<int>();
protected int[] TeamSlots = Array.Empty<int>();
public bool MoveBox(int box, int insertBeforeBox)
{
if (box == insertBeforeBox) // no movement required

View File

@@ -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>();
string[] rawlist = txt.Split('\n');
for (int i = 0; i < rawlist.Length; i++)
rawlist[i] = rawlist[i].TrimEnd('\r');

View File

@@ -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<ushort>();
}
}
private static readonly ushort[] chars67 =