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, MutablePalette> mutablePalettes = new Dictionary<string, MutablePalette>();
readonly Dictionary<string, int> indices = new Dictionary<string, int>();
byte[] buffer = new byte[0];
float[] colorShiftBuffer = new float[0];
byte[] buffer = Array.Empty<byte>();
float[] colorShiftBuffer = Array.Empty<float>();
public HardwarePalette()
{

View File

@@ -9,6 +9,7 @@
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Primitives;
@@ -16,7 +17,7 @@ namespace OpenRA.Graphics
{
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 WPos pos;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -41,7 +41,7 @@ namespace OpenRA.Traits
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<Actor, bool> actorIsInWorld = a => a.IsInWorld;
readonly Func<Actor, ActorBoundsPair> selectActorAndBounds;

View File

@@ -51,7 +51,7 @@ namespace OpenRA
public LongBitSet<PlayerBitMask> AllPlayersMask = 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;