Reduce allocations in AutoTarget.ChooseTarget.
- Cache active target query. - Prefer .Count == 0 over Any when working with Lists. - Use helper for GetEnabledTargetTypes. - Don't declare target variable until actually needed.
This commit is contained in:
committed by
Paul Chote
parent
986025ca76
commit
0555ce9321
@@ -98,7 +98,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
|
|
||||||
UnitStance stance;
|
UnitStance stance;
|
||||||
ConditionManager conditionManager;
|
ConditionManager conditionManager;
|
||||||
AutoTargetPriority[] targetPriorities;
|
IEnumerable<AutoTargetPriorityInfo> activeTargetPriorities;
|
||||||
int conditionToken = ConditionManager.InvalidConditionToken;
|
int conditionToken = ConditionManager.InvalidConditionToken;
|
||||||
|
|
||||||
public void SetStance(Actor self, UnitStance value)
|
public void SetStance(Actor self, UnitStance value)
|
||||||
@@ -140,8 +140,14 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
|
|
||||||
void INotifyCreated.Created(Actor self)
|
void INotifyCreated.Created(Actor self)
|
||||||
{
|
{
|
||||||
|
// AutoTargetPriority and their Priorities are fixed - so we can safely cache them with ToArray.
|
||||||
|
// IsTraitEnabled can change over time, and so must appear after the ToArray so it gets re-evaluated each time.
|
||||||
|
activeTargetPriorities =
|
||||||
|
self.TraitsImplementing<AutoTargetPriority>()
|
||||||
|
.OrderByDescending(ati => ati.Info.Priority).ToArray()
|
||||||
|
.Where(Exts.IsTraitEnabled).Select(atp => atp.Info);
|
||||||
|
|
||||||
conditionManager = self.TraitOrDefault<ConditionManager>();
|
conditionManager = self.TraitOrDefault<ConditionManager>();
|
||||||
targetPriorities = self.TraitsImplementing<AutoTargetPriority>().ToArray();
|
|
||||||
ApplyStanceCondition(self);
|
ApplyStanceCondition(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,12 +269,8 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
var chosenTargetPriority = int.MinValue;
|
var chosenTargetPriority = int.MinValue;
|
||||||
int chosenTargetRange = 0;
|
int chosenTargetRange = 0;
|
||||||
|
|
||||||
var activePriorities = targetPriorities.Where(Exts.IsTraitEnabled)
|
var activePriorities = activeTargetPriorities.ToList();
|
||||||
.Select(at => at.Info)
|
if (activePriorities.Count == 0)
|
||||||
.OrderByDescending(ati => ati.Priority)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
if (!activePriorities.Any())
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var actorsInRange = self.World.FindActorsInCircle(self.CenterPosition, scanRange);
|
var actorsInRange = self.World.FindActorsInCircle(self.CenterPosition, scanRange);
|
||||||
@@ -282,11 +284,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Check whether we can auto-target this actor
|
// Check whether we can auto-target this actor
|
||||||
var targetTypes = actor.TraitsImplementing<ITargetable>()
|
var targetTypes = actor.GetEnabledTargetTypes().ToArray();
|
||||||
.Where(Exts.IsTraitEnabled).SelectMany(t => t.TargetTypes)
|
|
||||||
.ToHashSet();
|
|
||||||
|
|
||||||
var target = Target.FromActor(actor);
|
|
||||||
var validPriorities = activePriorities.Where(ati =>
|
var validPriorities = activePriorities.Where(ati =>
|
||||||
{
|
{
|
||||||
// Already have a higher priority target
|
// Already have a higher priority target
|
||||||
@@ -294,16 +292,17 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Incompatible target types
|
// Incompatible target types
|
||||||
if (!targetTypes.Overlaps(ati.ValidTargets) || targetTypes.Overlaps(ati.InvalidTargets))
|
if (!ati.ValidTargets.Overlaps(targetTypes) || ati.InvalidTargets.Overlaps(targetTypes))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}).ToList();
|
}).ToList();
|
||||||
|
|
||||||
if (!validPriorities.Any() || PreventsAutoTarget(self, actor) || !actor.CanBeViewedByPlayer(self.Owner))
|
if (validPriorities.Count == 0 || PreventsAutoTarget(self, actor) || !actor.CanBeViewedByPlayer(self.Owner))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Make sure that we can actually fire on the actor
|
// Make sure that we can actually fire on the actor
|
||||||
|
var target = Target.FromActor(actor);
|
||||||
var armaments = ab.ChooseArmamentsForTarget(target, false);
|
var armaments = ab.ChooseArmamentsForTarget(target, false);
|
||||||
if (!allowMove)
|
if (!allowMove)
|
||||||
armaments = armaments.Where(arm =>
|
armaments = armaments.Where(arm =>
|
||||||
|
|||||||
Reference in New Issue
Block a user