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:
@@ -167,10 +167,10 @@ namespace OpenRA.Mods.Common.Traits
|
||||
Facing = legacyFacing,
|
||||
|
||||
DamageModifiers = self.TraitsImplementing<IFirepowerModifier>()
|
||||
.Select(a => a.GetFirepowerModifier()),
|
||||
.Select(a => a.GetFirepowerModifier()).ToArray(),
|
||||
|
||||
InaccuracyModifiers = self.TraitsImplementing<IInaccuracyModifier>()
|
||||
.Select(a => a.GetInaccuracyModifier()),
|
||||
.Select(a => a.GetInaccuracyModifier()).ToArray(),
|
||||
|
||||
Source = muzzlePosition,
|
||||
SourceActor = self,
|
||||
|
||||
@@ -53,7 +53,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
this.self = self;
|
||||
|
||||
var armaments = Exts.Lazy(() => self.TraitsImplementing<Armament>()
|
||||
.Where(a => info.Armaments.Contains(a.Info.Name)));
|
||||
.Where(a => info.Armaments.Contains(a.Info.Name)).ToArray());
|
||||
|
||||
getArmaments = () => armaments.Value;
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Traits;
|
||||
|
||||
@@ -22,12 +23,12 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
public class AttackTurreted : AttackFollow, ITick, ISync
|
||||
{
|
||||
protected IEnumerable<Turreted> turrets;
|
||||
protected Turreted[] turrets;
|
||||
|
||||
public AttackTurreted(Actor self, AttackTurretedInfo info)
|
||||
: base(self, info)
|
||||
{
|
||||
turrets = self.TraitsImplementing<Turreted>();
|
||||
turrets = self.TraitsImplementing<Turreted>().ToArray();
|
||||
}
|
||||
|
||||
protected override bool CanAttack(Actor self, Target target)
|
||||
|
||||
@@ -62,7 +62,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
IBodyOrientation body;
|
||||
AttackBase ab;
|
||||
Turreted t;
|
||||
IEnumerable<Armament> arms;
|
||||
Armament[] arms;
|
||||
Animation anim;
|
||||
|
||||
public WithTurret(Actor self, WithTurretInfo info)
|
||||
@@ -75,7 +75,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
t = self.TraitsImplementing<Turreted>()
|
||||
.First(tt => tt.Name == info.Turret);
|
||||
arms = self.TraitsImplementing<Armament>()
|
||||
.Where(w => w.Info.Turret == info.Turret);
|
||||
.Where(w => w.Info.Turret == info.Turret).ToArray();
|
||||
|
||||
anim = new Animation(self.World, rs.GetImage(self), () => t.TurretFacing);
|
||||
anim.Play(info.Sequence);
|
||||
|
||||
@@ -29,13 +29,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
readonly RepairableInfo info;
|
||||
readonly Health health;
|
||||
readonly IEnumerable<AmmoPool> ammoPools;
|
||||
readonly AmmoPool[] ammoPools;
|
||||
|
||||
public Repairable(Actor self, RepairableInfo info)
|
||||
{
|
||||
this.info = info;
|
||||
health = self.Trait<Health>();
|
||||
ammoPools = self.TraitsImplementing<AmmoPool>();
|
||||
ammoPools = self.TraitsImplementing<AmmoPool>().ToArray();
|
||||
}
|
||||
|
||||
public IEnumerable<IOrderTargeter> Orders
|
||||
@@ -72,10 +72,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
bool CanRearm()
|
||||
{
|
||||
if (ammoPools != null)
|
||||
return ammoPools.Any(x => !x.Info.SelfReloads && !x.FullAmmo());
|
||||
else
|
||||
return false;
|
||||
return ammoPools.Any(x => !x.Info.SelfReloads && !x.FullAmmo());
|
||||
}
|
||||
|
||||
public string VoicePhraseForOrder(Actor self, Order order)
|
||||
|
||||
Reference in New Issue
Block a user