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

@@ -60,7 +60,7 @@ namespace OpenRA.Mods.Common.Traits.Render
if (conditionManager != null && !string.IsNullOrEmpty(info.Condition) && token == ConditionManager.InvalidConditionToken)
token = conditionManager.GrantCondition(self, info.Condition);
var wsb = wsbs.FirstOrDefault(Exts.IsTraitEnabled);
var wsb = wsbs.FirstEnabledTraitOrDefault();
if (wsb == null)
return;
@@ -80,7 +80,7 @@ namespace OpenRA.Mods.Common.Traits.Render
if (conditionManager != null && !string.IsNullOrEmpty(info.Condition) && token == ConditionManager.InvalidConditionToken)
token = conditionManager.GrantCondition(self, info.Condition);
var wsb = wsbs.FirstOrDefault(Exts.IsTraitEnabled);
var wsb = wsbs.FirstEnabledTraitOrDefault();
if (wsb == null)
return;
@@ -99,7 +99,7 @@ namespace OpenRA.Mods.Common.Traits.Render
{
Reverse(self, () =>
{
var wsb = wsbs.FirstOrDefault(Exts.IsTraitEnabled);
var wsb = wsbs.FirstEnabledTraitOrDefault();
// HACK: The actor remains alive and active for one tick before the followup activity
// (sell/transform/etc) runs. This causes visual glitches that we attempt to minimize