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

@@ -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;

View File

@@ -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)