Add FirstEnabledTraitOrDefault helper method.

This avoids the allocations caused by LINQ when using traits.FirstOrDefault(Exts.IsTraitEnabled). This is important in FrozenActorLayer.RefreshState which is called very often. We apply the new helper method to all areas using the old pattern. An overload that takes an array allows arrays to be enumerated without causing allocations.
This commit is contained in:
RoosterDragon
2017-11-17 19:10:03 +00:00
committed by Paul Chote
parent cb670d83b3
commit 7a7eed4fb7
8 changed files with 32 additions and 12 deletions

View File

@@ -503,6 +503,26 @@ namespace OpenRA
{
return IsTraitEnabled(t as object);
}
public static T FirstEnabledTraitOrDefault<T>(this IEnumerable<T> ts)
{
// PERF: Avoid LINQ.
foreach (var t in ts)
if (t.IsTraitEnabled())
return t;
return default(T);
}
public static T FirstEnabledTraitOrDefault<T>(this T[] ts)
{
// PERF: Avoid LINQ.
foreach (var t in ts)
if (t.IsTraitEnabled())
return t;
return default(T);
}
}
public static class Enum<T>