Cache results of TraitsImplementing calls.

If a class is caching the TraitsImplementing enumerable, instead cache the results of enumerating it to an array. The avoids having to enumerate the sequence each time it is needed.
This commit is contained in:
RoosterDragon
2015-04-20 23:48:00 +01:00
parent 2937a31463
commit fb0cab7481
18 changed files with 45 additions and 47 deletions

View File

@@ -21,7 +21,7 @@ namespace OpenRA.Mods.Common.Activities
{
readonly Target target;
readonly AttackPlane attackPlane;
readonly IEnumerable<AmmoPool> ammoPools;
readonly AmmoPool[] ammoPools;
Activity inner;
int ticksUntilTurn;
@@ -29,7 +29,7 @@ namespace OpenRA.Mods.Common.Activities
{
this.target = target;
attackPlane = self.TraitOrDefault<AttackPlane>();
ammoPools = self.TraitsImplementing<AmmoPool>();
ammoPools = self.TraitsImplementing<AmmoPool>().ToArray();
ticksUntilTurn = attackPlane.AttackPlaneInfo.AttackTurnDelay;
}
@@ -40,7 +40,7 @@ namespace OpenRA.Mods.Common.Activities
// Move to the next activity only if all ammo pools are depleted and none reload automatically
// TODO: This should check whether there is ammo left that is actually suitable for the target
if (ammoPools != null && ammoPools.All(x => !x.Info.SelfReloads && !x.HasAmmo()))
if (ammoPools.All(x => !x.Info.SelfReloads && !x.HasAmmo()))
return NextActivity;
if (attackPlane != null)

View File

@@ -22,14 +22,14 @@ namespace OpenRA.Mods.Common.Activities
readonly Target target;
readonly Helicopter helicopter;
readonly AttackHeli attackHeli;
readonly IEnumerable<AmmoPool> ammoPools;
readonly AmmoPool[] ammoPools;
public HeliAttack(Actor self, Target target)
{
this.target = target;
helicopter = self.Trait<Helicopter>();
attackHeli = self.Trait<AttackHeli>();
ammoPools = self.TraitsImplementing<AmmoPool>();
ammoPools = self.TraitsImplementing<AmmoPool>().ToArray();
}
public override Activity Tick(Actor self)
@@ -39,7 +39,7 @@ namespace OpenRA.Mods.Common.Activities
// If all ammo pools are depleted and none reload automatically, return to helipad to reload and then move to next activity
// TODO: This should check whether there is ammo left that is actually suitable for the target
if (ammoPools != null && ammoPools.All(x => !x.Info.SelfReloads && !x.HasAmmo()))
if (ammoPools.All(x => !x.Info.SelfReloads && !x.HasAmmo()))
return Util.SequenceActivities(new HeliReturn(self), NextActivity);
var dist = target.CenterPosition - self.CenterPosition;

View File

@@ -19,7 +19,7 @@ namespace OpenRA.Mods.Common.Activities
class Demolish : Enter
{
readonly Actor target;
readonly IEnumerable<IDemolishable> demolishables;
readonly IDemolishable[] demolishables;
readonly int delay;
readonly int flashes;
readonly int flashesDelay;
@@ -32,7 +32,7 @@ namespace OpenRA.Mods.Common.Activities
: base(self, target)
{
this.target = target;
demolishables = target.TraitsImplementing<IDemolishable>();
demolishables = target.TraitsImplementing<IDemolishable>().ToArray();
this.delay = delay;
this.flashes = flashes;
this.flashesDelay = flashesDelay;

View File

@@ -19,7 +19,7 @@ namespace OpenRA.Mods.Common.Activities
{
readonly IPositionable positionable;
readonly IMove movement;
readonly IEnumerable<IDisableMove> moveDisablers;
readonly IDisableMove[] moveDisablers;
WPos start, end;
int length;
int ticks = 0;
@@ -28,7 +28,7 @@ namespace OpenRA.Mods.Common.Activities
{
positionable = self.Trait<IPositionable>();
movement = self.TraitOrDefault<IMove>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
moveDisablers = self.TraitsImplementing<IDisableMove>().ToArray();
this.start = start;
this.end = end;
this.length = length;

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Mods.Common.Activities
static readonly List<CPos> NoPath = new List<CPos>();
readonly Mobile mobile;
readonly IEnumerable<IDisableMove> moveDisablers;
readonly IDisableMove[] moveDisablers;
readonly WRange nearEnough;
readonly Func<List<CPos>> getPath;
readonly Actor ignoredActor;
@@ -43,7 +43,7 @@ namespace OpenRA.Mods.Common.Activities
public Move(Actor self, CPos destination)
{
mobile = self.Trait<Mobile>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
moveDisablers = self.TraitsImplementing<IDisableMove>().ToArray();
getPath = () =>
self.World.WorldActor.Trait<IPathFinder>().FindPath(
@@ -60,7 +60,7 @@ namespace OpenRA.Mods.Common.Activities
public Move(Actor self, CPos destination, WRange nearEnough)
{
mobile = self.Trait<Mobile>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
moveDisablers = self.TraitsImplementing<IDisableMove>().ToArray();
getPath = () => self.World.WorldActor.Trait<IPathFinder>()
.FindUnitPath(mobile.ToCell, destination, self);
@@ -71,7 +71,7 @@ namespace OpenRA.Mods.Common.Activities
public Move(Actor self, CPos destination, SubCell subCell, WRange nearEnough)
{
mobile = self.Trait<Mobile>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
moveDisablers = self.TraitsImplementing<IDisableMove>().ToArray();
getPath = () => self.World.WorldActor.Trait<IPathFinder>()
.FindUnitPathToRange(mobile.FromCell, subCell, self.World.Map.CenterOfSubCell(destination, subCell), nearEnough, self);
@@ -82,7 +82,7 @@ namespace OpenRA.Mods.Common.Activities
public Move(Actor self, CPos destination, Actor ignoredActor)
{
mobile = self.Trait<Mobile>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
moveDisablers = self.TraitsImplementing<IDisableMove>().ToArray();
getPath = () =>
self.World.WorldActor.Trait<IPathFinder>().FindPath(
@@ -97,7 +97,7 @@ namespace OpenRA.Mods.Common.Activities
public Move(Actor self, Target target, WRange range)
{
mobile = self.Trait<Mobile>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
moveDisablers = self.TraitsImplementing<IDisableMove>().ToArray();
getPath = () =>
{
@@ -115,7 +115,7 @@ namespace OpenRA.Mods.Common.Activities
public Move(Actor self, Func<List<CPos>> getPath)
{
mobile = self.Trait<Mobile>();
moveDisablers = self.TraitsImplementing<IDisableMove>();
moveDisablers = self.TraitsImplementing<IDisableMove>().ToArray();
this.getPath = getPath;

View File

@@ -17,12 +17,12 @@ namespace OpenRA.Mods.Common.Activities
{
public class Rearm : Activity
{
readonly IEnumerable<AmmoPool> ammoPools;
readonly AmmoPool[] ammoPools;
readonly Dictionary<AmmoPool, int> ammoPoolsReloadTimes;
public Rearm(Actor self)
{
ammoPools = self.TraitsImplementing<AmmoPool>().Where(p => !p.Info.SelfReloads);
ammoPools = self.TraitsImplementing<AmmoPool>().Where(p => !p.Info.SelfReloads).ToArray();
if (ammoPools == null)
return;

View File

@@ -17,12 +17,12 @@ namespace OpenRA.Mods.Common.Activities
{
public class Turn : Activity
{
readonly IEnumerable<IDisableMove> moveDisablers;
readonly IDisableMove[] moveDisablers;
readonly int desiredFacing;
public Turn(Actor self, int desiredFacing)
{
moveDisablers = self.TraitsImplementing<IDisableMove>();
moveDisablers = self.TraitsImplementing<IDisableMove>().ToArray();
this.desiredFacing = desiredFacing;
}