Avoid LINQ in some Tick methods.

As Tick is called often, avoiding allocation overhead in these methods is useful.
This commit is contained in:
RoosterDragon
2017-12-15 19:26:09 +00:00
committed by reaperrr
parent 86f9b8807e
commit dd2ae9fe5e
3 changed files with 36 additions and 11 deletions

View File

@@ -172,9 +172,11 @@ namespace OpenRA.Traits
void ITick.Tick(Actor self)
{
ResourceCapacity = self.World.ActorsWithTrait<IStoreResources>()
.Where(a => a.Actor.Owner == owner)
.Sum(a => a.Trait.Capacity);
// PERF: Avoid LINQ.
ResourceCapacity = 0;
foreach (var tp in self.World.ActorsWithTrait<IStoreResources>())
if (tp.Actor.Owner == owner)
ResourceCapacity += tp.Trait.Capacity;
if (Resources > ResourceCapacity)
Resources = ResourceCapacity;

View File

@@ -33,6 +33,7 @@ namespace OpenRA.Mods.Common.Traits
{
readonly ConquestVictoryConditionsInfo info;
readonly MissionObjectives mo;
Player[] otherPlayers;
int objectiveID = -1;
public ConquestVictoryConditions(Actor self, ConquestVictoryConditionsInfo cvcInfo)
@@ -51,12 +52,17 @@ namespace OpenRA.Mods.Common.Traits
if (!self.Owner.NonCombatant && self.Owner.HasNoRequiredUnits())
mo.MarkFailed(self.Owner, objectiveID);
var others = self.World.Players.Where(p => !p.NonCombatant
&& !p.IsAlliedWith(self.Owner));
// Players, NonCombatants, and IsAlliedWith are all fixed once the game starts, so we can cache the result.
if (otherPlayers == null)
otherPlayers = self.World.Players.Where(p => !p.NonCombatant && !p.IsAlliedWith(self.Owner)).ToArray();
if (!others.Any()) return;
if (otherPlayers.Length == 0) return;
// PERF: Avoid LINQ.
foreach (var otherPlayer in otherPlayers)
if (otherPlayer.WinState != WinState.Lost)
return;
if (others.All(p => p.WinState == WinState.Lost))
mo.MarkCompleted(self.Owner, objectiveID);
}

View File

@@ -130,13 +130,30 @@ namespace OpenRA.Mods.Common.Traits
bool HasPrerequisites(Cache<string, List<Actor>> ownedPrerequisites)
{
return prerequisites.All(p => !(p.Replace("~", "").StartsWith("!") ^ !ownedPrerequisites.ContainsKey(p.Replace("!", "").Replace("~", ""))));
// PERF: Avoid LINQ.
foreach (var prereq in prerequisites)
{
var withoutTilde = prereq.Replace("~", "");
if (withoutTilde.StartsWith("!") ^ !ownedPrerequisites.ContainsKey(withoutTilde.Replace("!", "")))
return false;
}
return true;
}
bool IsHidden(Cache<string, List<Actor>> ownedPrerequisites)
{
return prerequisites.Any(prereq => prereq.StartsWith("~") &&
(prereq.Replace("~", "").StartsWith("!") ^ !ownedPrerequisites.ContainsKey(prereq.Replace("~", "").Replace("!", ""))));
// PERF: Avoid LINQ.
foreach (var prereq in prerequisites)
{
if (!prereq.StartsWith("~"))
continue;
var withoutTilde = prereq.Replace("~", "");
if (withoutTilde.StartsWith("!") ^ !ownedPrerequisites.ContainsKey(withoutTilde.Replace("!", "")))
return true;
}
return false;
}
public void Update(Cache<string, List<Actor>> ownedPrerequisites)