Fixed unnecessary zero-length array allocations

Changed all currently present zero-length array allocations in the codebase to use `Array.Empty` instead.
This commit is contained in:
penev92
2022-01-09 21:32:25 +02:00
committed by abcdefg30
parent 1312c1aa72
commit 0d24ccc47a
28 changed files with 45 additions and 34 deletions

View File

@@ -24,8 +24,8 @@ namespace OpenRA.Graphics
readonly Dictionary<string, ImmutablePalette> palettes = new Dictionary<string, ImmutablePalette>(); readonly Dictionary<string, ImmutablePalette> palettes = new Dictionary<string, ImmutablePalette>();
readonly Dictionary<string, MutablePalette> mutablePalettes = new Dictionary<string, MutablePalette>(); readonly Dictionary<string, MutablePalette> mutablePalettes = new Dictionary<string, MutablePalette>();
readonly Dictionary<string, int> indices = new Dictionary<string, int>(); readonly Dictionary<string, int> indices = new Dictionary<string, int>();
byte[] buffer = new byte[0]; byte[] buffer = Array.Empty<byte>();
float[] colorShiftBuffer = new float[0]; float[] colorShiftBuffer = Array.Empty<float>();
public HardwarePalette() public HardwarePalette()
{ {

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.Primitives; using OpenRA.Primitives;
@@ -16,7 +17,7 @@ namespace OpenRA.Graphics
{ {
public class SpriteRenderable : IPalettedRenderable, IModifyableRenderable, IFinalizedRenderable public class SpriteRenderable : IPalettedRenderable, IModifyableRenderable, IFinalizedRenderable
{ {
public static readonly IEnumerable<IRenderable> None = new IRenderable[0]; public static readonly IEnumerable<IRenderable> None = Array.Empty<IRenderable>();
readonly Sprite sprite; readonly Sprite sprite;
readonly WPos pos; readonly WPos pos;

View File

@@ -370,11 +370,11 @@ namespace OpenRA
newData.SpawnPoints = spawns.ToArray(); newData.SpawnPoints = spawns.ToArray();
} }
else else
newData.SpawnPoints = new CPos[0]; newData.SpawnPoints = Array.Empty<CPos>();
} }
catch (Exception) catch (Exception)
{ {
newData.SpawnPoints = new CPos[0]; newData.SpawnPoints = Array.Empty<CPos>();
newData.Status = MapStatus.Unavailable; newData.Status = MapStatus.Unavailable;
} }

View File

@@ -77,7 +77,7 @@ namespace OpenRA.Primitives
{ {
if (data.TryGetValue(typeof(T), out var objs)) if (data.TryGetValue(typeof(T), out var objs))
return objs.Cast<T>(); return objs.Cast<T>();
return new T[0]; return Array.Empty<T>();
} }
public void Remove<T>(T val) public void Remove<T>(T val)

View File

@@ -279,7 +279,7 @@ namespace OpenRA.Scripting
return outer.SelectMany(i => i.GetGenericArguments()); return outer.SelectMany(i => i.GetGenericArguments());
} }
static readonly object[] NoArguments = new object[0]; static readonly object[] NoArguments = Array.Empty<object>();
Type[] FilterActorCommands(ActorInfo ai) Type[] FilterActorCommands(ActorInfo ai)
{ {
return FilterCommands(ai, knownActorCommands); return FilterCommands(ai, knownActorCommands);

View File

@@ -229,7 +229,7 @@ namespace OpenRA
if (!string.IsNullOrEmpty(text)) if (!string.IsNullOrEmpty(text))
bytes = encoding.GetBytes(text); bytes = encoding.GetBytes(text);
else else
bytes = new byte[0]; bytes = Array.Empty<byte>();
s.Write(bytes.Length); s.Write(bytes.Length);
s.WriteArray(bytes); s.WriteArray(bytes);

View File

@@ -72,8 +72,8 @@ namespace OpenRA.Traits
public Polygon MouseBounds = Polygon.Empty; public Polygon MouseBounds = Polygon.Empty;
static readonly IRenderable[] NoRenderables = new IRenderable[0]; static readonly IRenderable[] NoRenderables = Array.Empty<IRenderable>();
static readonly Rectangle[] NoBounds = new Rectangle[0]; static readonly Rectangle[] NoBounds = Array.Empty<Rectangle>();
int flashTicks; int flashTicks;
TintModifiers flashModifiers; TintModifiers flashModifiers;

View File

@@ -41,7 +41,7 @@ namespace OpenRA.Traits
public class ScreenMap : IWorldLoaded public class ScreenMap : IWorldLoaded
{ {
static readonly IEnumerable<FrozenActor> NoFrozenActors = new FrozenActor[0]; static readonly IEnumerable<FrozenActor> NoFrozenActors = Array.Empty<FrozenActor>();
readonly Func<FrozenActor, bool> frozenActorIsValid = fa => fa.IsValid; readonly Func<FrozenActor, bool> frozenActorIsValid = fa => fa.IsValid;
readonly Func<Actor, bool> actorIsInWorld = a => a.IsInWorld; readonly Func<Actor, bool> actorIsInWorld = a => a.IsInWorld;
readonly Func<Actor, ActorBoundsPair> selectActorAndBounds; readonly Func<Actor, ActorBoundsPair> selectActorAndBounds;

View File

@@ -51,7 +51,7 @@ namespace OpenRA
public LongBitSet<PlayerBitMask> AllPlayersMask = default(LongBitSet<PlayerBitMask>); public LongBitSet<PlayerBitMask> AllPlayersMask = default(LongBitSet<PlayerBitMask>);
public readonly LongBitSet<PlayerBitMask> NoPlayersMask = default(LongBitSet<PlayerBitMask>); public readonly LongBitSet<PlayerBitMask> NoPlayersMask = default(LongBitSet<PlayerBitMask>);
public Player[] Players = new Player[0]; public Player[] Players = Array.Empty<Player>();
public event Action<Player> RenderPlayerChanged; public event Action<Player> RenderPlayerChanged;

View File

@@ -58,7 +58,7 @@ namespace OpenRA.Mods.Cnc.Graphics
cachedPos = WPos.Zero; cachedPos = WPos.Zero;
cachedLength = WVec.Zero; cachedLength = WVec.Zero;
cache = new IFinalizedRenderable[] { }; cache = Array.Empty<IFinalizedRenderable>();
} }
public WPos Pos => pos; public WPos Pos => pos;

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.IO; using System.IO;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Primitives; using OpenRA.Primitives;
@@ -32,7 +33,7 @@ namespace OpenRA.Mods.Cnc.SpriteLoaders
Data = data; Data = data;
if (data == null) if (data == null)
Data = new byte[0]; Data = Array.Empty<byte>();
else else
Size = size; Size = size;
} }

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.IO; using System.IO;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Primitives; using OpenRA.Primitives;
@@ -32,7 +33,7 @@ namespace OpenRA.Mods.Cnc.SpriteLoaders
Data = data; Data = data;
if (data == null) if (data == null)
Data = new byte[0]; Data = Array.Empty<byte>();
else else
Size = size; Size = size;
} }

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.IO; using System.IO;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Primitives; using OpenRA.Primitives;
@@ -109,7 +110,7 @@ namespace OpenRA.Mods.Cnc.SpriteLoaders
} }
} }
else else
Data = new byte[0]; Data = Array.Empty<byte>();
} }
} }

View File

@@ -50,7 +50,7 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
Game.ModData = destModData; Game.ModData = destModData;
var destPaletteInfo = destModData.DefaultRules.Actors[SystemActors.Player].TraitInfo<PlayerColorPaletteInfo>(); var destPaletteInfo = destModData.DefaultRules.Actors[SystemActors.Player].TraitInfo<PlayerColorPaletteInfo>();
var destRemapIndex = destPaletteInfo.RemapIndex; var destRemapIndex = destPaletteInfo.RemapIndex;
var shadowIndex = new int[] { }; var shadowIndex = Array.Empty<int>();
// the remap range is always 16 entries, but their location and order changes // the remap range is always 16 entries, but their location and order changes
for (var i = 0; i < 16; i++) for (var i = 0; i < 16; i++)

View File

@@ -54,7 +54,7 @@ namespace OpenRA.Mods.Common.Activities
aircraft = self.Trait<Aircraft>(); aircraft = self.Trait<Aircraft>();
this.target = target; this.target = target;
this.offset = offset; this.offset = offset;
this.clearCells = clearCells ?? new CPos[0]; this.clearCells = clearCells ?? Array.Empty<CPos>();
this.landRange = landRange.Length >= 0 ? landRange : aircraft.Info.LandRange; this.landRange = landRange.Length >= 0 ? landRange : aircraft.Info.LandRange;
this.targetLineColor = targetLineColor; this.targetLineColor = targetLineColor;

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.Activities; using OpenRA.Activities;
using OpenRA.Effects; using OpenRA.Effects;
@@ -26,10 +27,10 @@ namespace OpenRA.Mods.Common.Effects
int remainingDelay; int remainingDelay;
public SpawnActorEffect(Actor actor) public SpawnActorEffect(Actor actor)
: this(actor, 0, new CPos[0], null) { } : this(actor, 0, Array.Empty<CPos>(), null) { }
public SpawnActorEffect(Actor actor, int delay) public SpawnActorEffect(Actor actor, int delay)
: this(actor, delay, new CPos[0], null) { } : this(actor, delay, Array.Empty<CPos>(), null) { }
public SpawnActorEffect(Actor actor, int delay, CPos[] pathAfterSpawn, Activity activityAtDestination) public SpawnActorEffect(Actor actor, int delay, CPos[] pathAfterSpawn, Activity activityAtDestination)
{ {

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@@ -78,7 +79,7 @@ namespace OpenRA.Mods.Common.SpriteLoaders
public TgaFrame() public TgaFrame()
{ {
Data = new byte[0]; Data = Array.Empty<byte>();
} }
public TgaFrame(Stream stream) public TgaFrame(Stream stream)

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Traits; using OpenRA.Traits;
@@ -27,7 +28,7 @@ namespace OpenRA.Mods.Common.Traits
readonly Actor[] parents = null; readonly Actor[] parents = null;
public LineBuildParentInit(Actor[] value) public LineBuildParentInit(Actor[] value)
: base(new string[0]) : base(Array.Empty<string>())
{ {
parents = value; parents = value;
} }
@@ -70,7 +71,7 @@ namespace OpenRA.Mods.Common.Traits
public class LineBuild : INotifyKilled, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyLineBuildSegmentsChanged public class LineBuild : INotifyKilled, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyLineBuildSegmentsChanged
{ {
readonly LineBuildInfo info; readonly LineBuildInfo info;
readonly Actor[] parentNodes = new Actor[0]; readonly Actor[] parentNodes = Array.Empty<Actor>();
HashSet<Actor> segments; HashSet<Actor> segments;
public LineBuild(ActorInitializer init, LineBuildInfo info) public LineBuild(ActorInitializer init, LineBuildInfo info)

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.Traits; using OpenRA.Traits;
@@ -46,7 +47,7 @@ namespace OpenRA.Mods.Common.Traits
if (info.OccupiesSpace) if (info.OccupiesSpace)
occupied = new[] { (TopLeft, SubCell.FullCell) }; occupied = new[] { (TopLeft, SubCell.FullCell) };
else else
occupied = new (CPos, SubCell)[0]; occupied = Array.Empty<(CPos, SubCell)>();
} }
public CPos TopLeft => location; public CPos TopLeft => location;

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.Linq; using System.Linq;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Traits; using OpenRA.Traits;
@@ -29,7 +30,7 @@ namespace OpenRA.Mods.Common.Traits
public class Targetable : ConditionalTrait<TargetableInfo>, ITargetable public class Targetable : ConditionalTrait<TargetableInfo>, ITargetable
{ {
protected static readonly string[] None = new string[] { }; protected static readonly string[] None = Array.Empty<string>();
protected Cloak[] cloaks; protected Cloak[] cloaks;
public Targetable(Actor self, TargetableInfo info) public Targetable(Actor self, TargetableInfo info)

View File

@@ -35,7 +35,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
var modData = Game.ModData = utility.ModData; var modData = Game.ModData = utility.ModData;
var src = args[1]; var src = args[1];
var shadowIndex = new int[] { }; var shadowIndex = Array.Empty<int>();
if (args.Contains("--noshadow")) if (args.Contains("--noshadow"))
{ {
Array.Resize(ref shadowIndex, shadowIndex.Length + 3); Array.Resize(ref shadowIndex, shadowIndex.Length + 3);

View File

@@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
Console.WriteLine(".SH OPTIONS"); Console.WriteLine(".SH OPTIONS");
var sections = Game.Settings.Sections; var sections = Game.Settings.Sections;
sections.Add("Launch", new LaunchArguments(new Arguments(new string[0]))); sections.Add("Launch", new LaunchArguments(new Arguments(Array.Empty<string>())));
foreach (var section in sections.OrderBy(s => s.Key)) foreach (var section in sections.OrderBy(s => s.Key))
{ {
var fields = section.Value.GetType().GetFields(); var fields = section.Value.GetType().GetFields();

View File

@@ -32,7 +32,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
// HACK: The engine code assumes that Game.modData is set. // HACK: The engine code assumes that Game.modData is set.
var modData = Game.ModData = utility.ModData; var modData = Game.ModData = utility.ModData;
var palette = new ImmutablePalette(args[1], new[] { 0 }, new int[0]); var palette = new ImmutablePalette(args[1], new[] { 0 }, Array.Empty<int>());
SequenceProvider sequences = null; SequenceProvider sequences = null;
var mapPackage = new Folder(Platform.EngineDir).OpenPackage(args[2], modData.ModFiles); var mapPackage = new Folder(Platform.EngineDir).OpenPackage(args[2], modData.ModFiles);

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.GameRules; using OpenRA.GameRules;
@@ -80,8 +81,8 @@ namespace OpenRA.Mods.Common.Warheads
CurrentMuzzleFacing = () => (map.CenterOfCell(targetCell) - target.CenterPosition).Yaw, CurrentMuzzleFacing = () => (map.CenterOfCell(targetCell) - target.CenterPosition).Yaw,
DamageModifiers = args.DamageModifiers, DamageModifiers = args.DamageModifiers,
InaccuracyModifiers = new int[0], InaccuracyModifiers = Array.Empty<int>(),
RangeModifiers = new int[0], RangeModifiers = Array.Empty<int>(),
Source = target.CenterPosition, Source = target.CenterPosition,
CurrentSource = () => target.CenterPosition, CurrentSource = () => target.CenterPosition,

View File

@@ -26,7 +26,7 @@ namespace OpenRA.Mods.Common.Widgets
readonly WorldRenderer worldRenderer; readonly WorldRenderer worldRenderer;
IActorPreview[] preview = new IActorPreview[0]; IActorPreview[] preview = Array.Empty<IActorPreview>();
public int2 PreviewOffset { get; private set; } public int2 PreviewOffset { get; private set; }
public int2 IdealPreviewSize { get; private set; } public int2 IdealPreviewSize { get; private set; }

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.Common.Widgets
int scatterHighlighted; int scatterHighlighted;
int stopHighlighted; int stopHighlighted;
TraitPair<IIssueDeployOrder>[] selectedDeploys = { }; TraitPair<IIssueDeployOrder>[] selectedDeploys = Array.Empty<TraitPair<IIssueDeployOrder>>();
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public CommandBarLogic(Widget widget, World world, Dictionary<string, MiniYaml> logicArgs) public CommandBarLogic(Widget widget, World world, Dictionary<string, MiniYaml> logicArgs)

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.Linq; using System.Linq;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets; using OpenRA.Widgets;
@@ -20,7 +21,7 @@ namespace OpenRA.Mods.Common.Widgets
readonly World world; readonly World world;
int selectionHash; int selectionHash;
TraitPair<AutoTarget>[] actorStances = { }; TraitPair<AutoTarget>[] actorStances = Array.Empty<TraitPair<AutoTarget>>();
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public StanceSelectorLogic(Widget widget, World world) public StanceSelectorLogic(Widget widget, World world)

View File

@@ -68,7 +68,7 @@ namespace OpenRA
if (args.Length == 0) if (args.Length == 0)
{ {
PrintUsage(new InstalledMods(modSearchPaths, new string[0]), null); PrintUsage(new InstalledMods(modSearchPaths, Array.Empty<string>()), null);
return; return;
} }