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

@@ -54,7 +54,7 @@ namespace OpenRA.Mods.Common.Activities
aircraft = self.Trait<Aircraft>();
this.target = target;
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.targetLineColor = targetLineColor;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.UtilityCommands
Console.WriteLine(".SH OPTIONS");
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))
{
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.
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;
var mapPackage = new Folder(Platform.EngineDir).OpenPackage(args[2], modData.ModFiles);

View File

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

View File

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

View File

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

View File

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