Changes to reduce allocations in the main loop.

Targeted some methods that generated allocated a lot of memory in the main game loop:
- Actor.Render - Changed LINQ calls into equivalent loops. No allocation for delegates.
- Animation.Render - Returned an array rather than a yield expression. The array uses less memory than the complier generated enumerable.
- FrozenActor and FrozenUnderFog - Materialize the footprint into an array: The enumerable is not-trivial to evaluate is and evaluated many times inside the Tick function. The memory needed is minimal. Changed LINQ into equivalent loops to prevent delegate allocation. Should result in overall much faster calls.
- Widget.GetEventBounds - Changed LINQ calls into equivalent loops.
- MobileInfo.CanEnterCell - Changed LINQ calls into equivalent loops. Don't materialize list of blocking actors every time, instead enumerate them and only when they need to be checked.
- FrozenUnderFog.TickRender - Generate the renderables lazily and also remove a no-op Select call.
This commit is contained in:
RoosterDragon
2014-05-23 14:48:11 +01:00
parent db08357e36
commit a0db80cb6a
6 changed files with 63 additions and 37 deletions

View File

@@ -22,7 +22,7 @@ namespace OpenRA.Traits
public class FrozenActor
{
public readonly IEnumerable<CPos> Footprint;
public readonly CPos[] Footprint;
public readonly WPos CenterPosition;
public readonly Rectangle Bounds;
readonly Actor actor;
@@ -41,7 +41,7 @@ namespace OpenRA.Traits
public FrozenActor(Actor self, IEnumerable<CPos> footprint)
{
actor = self;
Footprint = footprint;
Footprint = footprint.ToArray();
CenterPosition = self.CenterPosition;
Bounds = self.Bounds.Value;
}
@@ -54,7 +54,13 @@ namespace OpenRA.Traits
int flashTicks;
public void Tick(World world, Shroud shroud)
{
Visible = !Footprint.Any(c => shroud.IsVisible(c));
Visible = false;
foreach (var pos in Footprint)
if (shroud.IsVisible(pos))
{
Visible = true;
break;
}
if (flashTicks > 0)
flashTicks--;