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

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