From 9c59d7e255c0a8b3e2adbd098ef4a05b93de757e Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Wed, 11 Jan 2017 21:46:20 +0000 Subject: [PATCH] Avoid delegate allocation in AutoTarget. Extract a common method for deciding if an attack should happen, and avoid LINQ inside this method. --- OpenRA.Mods.Common/Traits/AutoTarget.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/AutoTarget.cs b/OpenRA.Mods.Common/Traits/AutoTarget.cs index c96c6c7757..a9a66f0ccf 100644 --- a/OpenRA.Mods.Common/Traits/AutoTarget.cs +++ b/OpenRA.Mods.Common/Traits/AutoTarget.cs @@ -119,8 +119,9 @@ namespace OpenRA.Mods.Common.Traits return; Aggressor = attacker; - var allowMove = Info.AllowMovement && Stance != UnitStance.Defend; - if (attackFollows.All(a => a.IsTraitDisabled || !a.IsReachableTarget(a.Target, allowMove))) + + bool allowMove; + if (ShouldAttack(out allowMove)) Attack(self, Aggressor, allowMove); } @@ -132,11 +133,23 @@ namespace OpenRA.Mods.Common.Traits if (Stance < UnitStance.Defend || !Info.TargetWhenIdle) return; - var allowMove = Info.AllowMovement && Stance != UnitStance.Defend; - if (attackFollows.All(a => a.IsTraitDisabled || !a.IsReachableTarget(a.Target, allowMove))) + bool allowMove; + if (ShouldAttack(out allowMove)) ScanAndAttack(self, allowMove); } + bool ShouldAttack(out bool allowMove) + { + allowMove = Info.AllowMovement && Stance != UnitStance.Defend; + + // PERF: Avoid LINQ. + foreach (var attackFollow in attackFollows) + if (!attackFollow.IsTraitDisabled && attackFollow.IsReachableTarget(attackFollow.Target, allowMove)) + return false; + + return true; + } + public void Tick(Actor self) { if (IsTraitDisabled)