Added comments in performance sensitive code.

This commit is contained in:
RoosterDragon
2015-12-04 19:38:20 +00:00
parent aaa82339d1
commit b0619a3e25
22 changed files with 76 additions and 11 deletions

View File

@@ -86,10 +86,9 @@ namespace OpenRA.Traits
{
var wasVisible = Visible;
Shrouded = true;
// We are doing the following LINQ manually for performance since this is a hot path.
// Visible = !Footprint.Any(shroud.IsVisible);
Visible = true;
// PERF: Avoid LINQ.
foreach (var puv in Footprint)
{
if (shroud.IsVisible(puv))
@@ -142,7 +141,7 @@ namespace OpenRA.Traits
public bool ShouldBeRemoved(Player owner)
{
// We use a loop here for performance reasons
// PERF: Avoid LINQ.
foreach (var rfa in removeFrozenActors)
if (rfa.RemoveActor(actor, owner))
return true;

View File

@@ -50,6 +50,7 @@ namespace OpenRA.Traits
{
public static bool HasStance(this Stance s, Stance stance)
{
// PERF: Enum.HasFlag is slower and requires allocations.
return (s & stance) == stance;
}
}
@@ -92,7 +93,11 @@ namespace OpenRA.Traits
public static class TargetModifiersExts
{
public static bool HasModifier(this TargetModifiers self, TargetModifiers m) { return (self & m) == m; }
public static bool HasModifier(this TargetModifiers self, TargetModifiers m)
{
// PERF: Enum.HasFlag is slower and requires allocations.
return (self & m) == m;
}
}
public interface IOrderTargeter

View File

@@ -74,10 +74,15 @@ namespace OpenRA.Traits
public static Activity RunActivity(Actor self, Activity act)
{
// PERF: If there are no activities we can bail straight away and save ourselves a call to
// Stopwatch.GetTimestamp.
if (act == null)
return act;
// Note - manual iteration here for performance due to high call volume.
// PERF: This is a hot path and must run with minimal added overhead.
// Calling Stopwatch.GetTimestamp is a bit expensive, so we enumerate manually to allow us to call it only
// once per iteration in the normal case.
// See also: DoTimed
var longTickThresholdInStopwatchTicks = PerfTimer.LongTickThresholdInStopwatchTicks;
var start = Stopwatch.GetTimestamp();
while (act != null)

View File

@@ -183,7 +183,7 @@ namespace OpenRA.Traits
for (var col = 0; col < cols; col++)
bins[row * cols + col] = new Bin();
// Cache this delegate so it does not have to be allocated repeatedly.
// PERF: Cache this delegate so it does not have to be allocated repeatedly.
actorShouldBeRemoved = removeActorPosition.Contains;
}
@@ -219,6 +219,7 @@ namespace OpenRA.Traits
public IEnumerable<Actor> GetActorsAt(CPos a)
{
// PERF: Custom enumerator for efficiency - using `yield` is slower.
var uv = a.ToMPos(map);
if (!influence.Contains(uv))
return Enumerable.Empty<Actor>();
@@ -534,6 +535,7 @@ namespace OpenRA.Traits
public IEnumerable<Actor> ActorsInBox(WPos a, WPos b)
{
// PERF: Inline BinsInBox here to avoid allocations as this method is called often.
var left = Math.Min(a.X, b.X);
var top = Math.Min(a.Y, b.Y);
var right = Math.Max(a.X, b.X);