Merge pull request #10208 from RoosterDragon/perf-comments
Added some performance comments
This commit is contained in:
@@ -61,7 +61,7 @@ namespace OpenRA.Mods.Common.Effects
|
||||
|
||||
public IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
|
||||
{
|
||||
yield return trail;
|
||||
return new IRenderable[] { trail };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
{
|
||||
public sealed class PathSearch : BasePathSearch
|
||||
{
|
||||
// PERF: Maintain a pool of layers used for paths searches for each world. These searches are performed often
|
||||
// so we wish to avoid the high cost of initializing a new search space every time by reusing the old ones.
|
||||
static readonly ConditionalWeakTable<World, CellInfoLayerPool> LayerPoolTable = new ConditionalWeakTable<World, CellInfoLayerPool>();
|
||||
static readonly ConditionalWeakTable<World, CellInfoLayerPool>.CreateValueCallback CreateLayerPool = world => new CellInfoLayerPool(world.Map);
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace OpenRA.Mods.Common.Scripting
|
||||
if (!Context.World.Map.Rules.Actors.TryGetValue(type, out ai))
|
||||
throw new LuaException("Unknown actor type '{0}'".F(type));
|
||||
|
||||
result.AddRange(Player.World.ActorMap.ActorsInWorld()
|
||||
result.AddRange(Player.World.Actors
|
||||
.Where(actor => actor.Owner == Player && !actor.IsDead && actor.IsInWorld && actor.Info.Name == ai.Name));
|
||||
|
||||
return result.ToArray();
|
||||
|
||||
@@ -20,6 +20,7 @@ namespace OpenRA.Mods.Common
|
||||
{
|
||||
public static bool AnyExplored(this Shroud shroud, OccupiedCells cells)
|
||||
{
|
||||
// PERF: Avoid LINQ.
|
||||
foreach (var cell in cells)
|
||||
if (shroud.IsExplored(cell.First))
|
||||
return true;
|
||||
@@ -29,6 +30,7 @@ namespace OpenRA.Mods.Common
|
||||
|
||||
public static bool AnyVisible(this Shroud shroud, OccupiedCells cells)
|
||||
{
|
||||
// PERF: Avoid LINQ.
|
||||
foreach (var cell in cells)
|
||||
if (shroud.IsVisible(cell.First))
|
||||
return true;
|
||||
|
||||
@@ -33,6 +33,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public static bool HasCellCondition(this CellConditions c, CellConditions cellCondition)
|
||||
{
|
||||
// PERF: Enum.HasFlag is slower and requires allocations.
|
||||
return (c & cellCondition) == cellCondition;
|
||||
}
|
||||
}
|
||||
@@ -132,6 +133,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
internal readonly TerrainInfo[] TerrainInfos;
|
||||
internal WorldMovementInfo(World world, MobileInfo info)
|
||||
{
|
||||
// PERF: This struct allows us to cache the terrain info for the tileset used by the world.
|
||||
// This allows us to speed up some performance-sensitive pathfinding calculations.
|
||||
World = world;
|
||||
TerrainInfos = info.TilesetTerrainInfo[world.TileSet];
|
||||
}
|
||||
@@ -217,6 +220,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if (SharesCell && world.ActorMap.HasFreeSubCell(cell))
|
||||
return true;
|
||||
|
||||
// PERF: Avoid LINQ.
|
||||
foreach (var otherActor in world.ActorMap.GetActorsAt(cell))
|
||||
if (IsBlockedBy(self, otherActor, ignoreActor, check))
|
||||
return false;
|
||||
@@ -242,6 +246,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return true;
|
||||
|
||||
// If the other actor in our way cannot be crushed, we are blocked.
|
||||
// PERF: Avoid LINQ.
|
||||
var crushables = otherActor.TraitsImplementing<ICrushable>();
|
||||
var lacksCrushability = true;
|
||||
foreach (var crushable in crushables)
|
||||
|
||||
@@ -115,6 +115,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
}
|
||||
else
|
||||
{
|
||||
// PERF: Minimize lookup cost by combining all state into one, and using an array rather than a dictionary.
|
||||
var state = stateByPlayerIndex[i];
|
||||
frozenActor = state.FrozenActor;
|
||||
isVisible = !frozenActor.Visible;
|
||||
|
||||
@@ -48,6 +48,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
public override void Tick(Actor self)
|
||||
{
|
||||
// PERF: Avoid LINQ.
|
||||
isActive = false;
|
||||
foreach (var x in self.World.ActorsWithTrait<Production>())
|
||||
{
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
var spaceBuffer = (int)(10 / wr.Viewport.Zoom);
|
||||
var effectPos = wr.ProjectedPosition(new int2(pos.X, bounds.Y - spaceBuffer));
|
||||
|
||||
yield return new TextRenderable(font, effectPos, 0, color, name);
|
||||
return new IRenderable[] { new TextRenderable(font, effectPos, 0, color, name) };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,10 +103,10 @@ namespace OpenRA.Mods.Common.Traits
|
||||
initializePalettes = false;
|
||||
}
|
||||
|
||||
yield return new VoxelRenderable(
|
||||
return new IRenderable[] { new VoxelRenderable(
|
||||
components, self.CenterPosition, 0, camera, info.Scale,
|
||||
lightSource, info.LightAmbientColor, info.LightDiffuseColor,
|
||||
colorPalette, normalsPalette, shadowPalette);
|
||||
colorPalette, normalsPalette, shadowPalette) };
|
||||
}
|
||||
|
||||
public string Image { get { return info.Image ?? self.Info.Name; } }
|
||||
|
||||
@@ -134,20 +134,20 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
|
||||
{
|
||||
if (info.ShadowImage == null)
|
||||
yield break;
|
||||
return Enumerable.Empty<IRenderable>();
|
||||
|
||||
if (IsTraitDisabled)
|
||||
yield break;
|
||||
return Enumerable.Empty<IRenderable>();
|
||||
|
||||
if (self.IsDead || !self.IsInWorld)
|
||||
yield break;
|
||||
return Enumerable.Empty<IRenderable>();
|
||||
|
||||
if (self.World.FogObscures(self))
|
||||
yield break;
|
||||
return Enumerable.Empty<IRenderable>();
|
||||
|
||||
shadow.Tick();
|
||||
var pos = self.CenterPosition - new WVec(0, 0, self.CenterPosition.Z);
|
||||
yield return new SpriteRenderable(shadow.Image, pos, info.ShadowOffset, info.ShadowZOffset, wr.Palette(info.ShadowPalette), 1, true);
|
||||
return new IRenderable[] { new SpriteRenderable(shadow.Image, pos, info.ShadowOffset, info.ShadowZOffset, wr.Palette(info.ShadowPalette), 1, true) };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,6 +226,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
void DirtyCells(IEnumerable<PPos> cells)
|
||||
{
|
||||
// PERF: Many cells in the shroud change every tick. We only track the changes here and defer the real work
|
||||
// we need to do until we render. This allows us to avoid wasted work.
|
||||
cellsDirty.UnionWith(cells);
|
||||
}
|
||||
|
||||
|
||||
@@ -208,6 +208,8 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
|
||||
void MarkShroudDirty(IEnumerable<PPos> projectedCellsChanged)
|
||||
{
|
||||
// PERF: Many cells in the shroud change every tick. We only track the changes here and defer the real work
|
||||
// we need to do until we render. This allows us to avoid wasted work.
|
||||
dirtyShroudCells.UnionWith(projectedCellsChanged);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user