OccupiedCells and TargetableCells must return arrays, not just enumerables.

This allows callers to efficiently enumerate these returned collections without the allocation and overhead imposed by the IEnumerable interface. All implementations were already returning arrays, so this only required a signature change.
This commit is contained in:
RoosterDragon
2017-11-17 21:23:17 +00:00
committed by Pavel Penev
parent da036c5728
commit 62ab6ae6f1
10 changed files with 13 additions and 15 deletions

View File

@@ -203,7 +203,7 @@ namespace OpenRA.Traits
{ {
WPos CenterPosition { get; } WPos CenterPosition { get; }
CPos TopLeft { get; } CPos TopLeft { get; }
IEnumerable<Pair<CPos, SubCell>> OccupiedCells(); Pair<CPos, SubCell>[] OccupiedCells();
} }
public static class IOccupySpaceExts public static class IOccupySpaceExts

View File

@@ -136,7 +136,7 @@ namespace OpenRA.Mods.Cnc.Traits
get { return Util.ApplyPercentageModifiers(Info.Speed, speedModifiers); } get { return Util.ApplyPercentageModifiers(Info.Speed, speedModifiers); }
} }
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return new[] { Pair.New(TopLeft, SubCell.FullCell) }; } public Pair<CPos, SubCell>[] OccupiedCells() { return new[] { Pair.New(TopLeft, SubCell.FullCell) }; }
WVec MoveStep(int facing) WVec MoveStep(int facing)
{ {

View File

@@ -15,11 +15,9 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common namespace OpenRA.Mods.Common
{ {
using OccupiedCells = IEnumerable<Pair<CPos, SubCell>>;
public static class ShroudExts public static class ShroudExts
{ {
public static bool AnyExplored(this Shroud shroud, OccupiedCells cells) public static bool AnyExplored(this Shroud shroud, Pair<CPos, SubCell>[] cells)
{ {
// PERF: Avoid LINQ. // PERF: Avoid LINQ.
foreach (var cell in cells) foreach (var cell in cells)
@@ -29,7 +27,7 @@ namespace OpenRA.Mods.Common
return false; return false;
} }
public static bool AnyVisible(this Shroud shroud, OccupiedCells cells) public static bool AnyVisible(this Shroud shroud, Pair<CPos, SubCell>[] cells)
{ {
// PERF: Avoid LINQ. // PERF: Avoid LINQ.
foreach (var cell in cells) foreach (var cell in cells)

View File

@@ -460,7 +460,7 @@ namespace OpenRA.Mods.Common.Traits
get { return Util.ApplyPercentageModifiers(Info.Speed, speedModifiers); } get { return Util.ApplyPercentageModifiers(Info.Speed, speedModifiers); }
} }
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return NoCells; } public Pair<CPos, SubCell>[] OccupiedCells() { return NoCells; }
public WVec FlyStep(int facing) public WVec FlyStep(int facing)
{ {

View File

@@ -291,9 +291,9 @@ namespace OpenRA.Mods.Common.Traits
SkipMakeAnimation = init.Contains<SkipMakeAnimsInit>(); SkipMakeAnimation = init.Contains<SkipMakeAnimsInit>();
} }
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return occupiedCells; } public Pair<CPos, SubCell>[] OccupiedCells() { return occupiedCells; }
IEnumerable<Pair<CPos, SubCell>> ITargetableCells.TargetableCells() { return targetableCells; } Pair<CPos, SubCell>[] ITargetableCells.TargetableCells() { return targetableCells; }
void INotifyCreated.Created(Actor self) void INotifyCreated.Created(Actor self)
{ {

View File

@@ -160,7 +160,7 @@ namespace OpenRA.Mods.Common.Traits
} }
public CPos TopLeft { get { return Location; } } public CPos TopLeft { get { return Location; } }
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return new[] { Pair.New(Location, SubCell.FullCell) }; } public Pair<CPos, SubCell>[] OccupiedCells() { return new[] { Pair.New(Location, SubCell.FullCell) }; }
public WPos CenterPosition { get; private set; } public WPos CenterPosition { get; private set; }

View File

@@ -77,7 +77,7 @@ namespace OpenRA.Mods.Common.Traits
self.QueueActivity(new Drag(self, CenterPosition, finalPosition, distance / dragSpeed)); self.QueueActivity(new Drag(self, CenterPosition, finalPosition, distance / dragSpeed));
} }
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return new[] { Pair.New(TopLeft, SubCell.FullCell) }; } public Pair<CPos, SubCell>[] OccupiedCells() { return new[] { Pair.New(TopLeft, SubCell.FullCell) }; }
public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return false; } public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return false; }
public SubCell GetValidSubCell(SubCell preferred = SubCell.Any) { return SubCell.FullCell; } public SubCell GetValidSubCell(SubCell preferred = SubCell.Any) { return SubCell.FullCell; }
public SubCell GetAvailableSubCell(CPos cell, SubCell preferredSubCell = SubCell.Any, Actor ignoreActor = null, bool checkTransientActors = true) public SubCell GetAvailableSubCell(CPos cell, SubCell preferredSubCell = SubCell.Any, Actor ignoreActor = null, bool checkTransientActors = true)

View File

@@ -35,7 +35,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
[Sync] readonly CPos location; [Sync] readonly CPos location;
[Sync] readonly WPos position; [Sync] readonly WPos position;
readonly IEnumerable<Pair<CPos, SubCell>> occupied; readonly Pair<CPos, SubCell>[] occupied;
public Immobile(ActorInitializer init, ImmobileInfo info) public Immobile(ActorInitializer init, ImmobileInfo info)
{ {
@@ -50,7 +50,7 @@ namespace OpenRA.Mods.Common.Traits
public CPos TopLeft { get { return location; } } public CPos TopLeft { get { return location; } }
public WPos CenterPosition { get { return position; } } public WPos CenterPosition { get { return position; } }
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return occupied; } public Pair<CPos, SubCell>[] OccupiedCells() { return occupied; }
void INotifyAddedToWorld.AddedToWorld(Actor self) void INotifyAddedToWorld.AddedToWorld(Actor self)
{ {

View File

@@ -665,7 +665,7 @@ namespace OpenRA.Mods.Common.Traits
public CPos TopLeft { get { return ToCell; } } public CPos TopLeft { get { return ToCell; } }
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() public Pair<CPos, SubCell>[] OccupiedCells()
{ {
if (FromCell == ToCell) if (FromCell == ToCell)
return new[] { Pair.New(FromCell, FromSubCell) }; return new[] { Pair.New(FromCell, FromSubCell) };

View File

@@ -383,7 +383,7 @@ namespace OpenRA.Mods.Common.Traits
[RequireExplicitImplementation] [RequireExplicitImplementation]
public interface ITargetableCells public interface ITargetableCells
{ {
IEnumerable<Pair<CPos, SubCell>> TargetableCells(); Pair<CPos, SubCell>[] TargetableCells();
} }
[RequireExplicitImplementation] [RequireExplicitImplementation]