Add FirstNonEmptyBounds method for IDecorationBounds interface.

This provides a more efficient way of determining the bounds by avoiding LINQ. A helper that works directly on arrays prevents allocation of an enumerator when the collection is know to be an array.
This commit is contained in:
RoosterDragon
2017-12-22 12:55:05 +00:00
committed by reaperrr
parent 3b642b1a79
commit d2f2029caf
10 changed files with 38 additions and 21 deletions

View File

@@ -110,6 +110,34 @@ namespace OpenRA.Traits
// HACK: This provides a shim for legacy code until it can be rewritten
public interface IDecorationBounds { Rectangle DecorationBounds(Actor self, WorldRenderer wr); }
public interface IDecorationBoundsInfo : ITraitInfoInterface { }
public static class DecorationBoundsExtensions
{
public static Rectangle FirstNonEmptyBounds(this IEnumerable<IDecorationBounds> decorationBounds, Actor self, WorldRenderer wr)
{
// PERF: Avoid LINQ.
foreach (var decoration in decorationBounds)
{
var bounds = decoration.DecorationBounds(self, wr);
if (!bounds.IsEmpty)
return bounds;
}
return Rectangle.Empty;
}
public static Rectangle FirstNonEmptyBounds(this IDecorationBounds[] decorationBounds, Actor self, WorldRenderer wr)
{
// PERF: Avoid LINQ.
foreach (var decoration in decorationBounds)
{
var bounds = decoration.DecorationBounds(self, wr);
if (!bounds.IsEmpty)
return bounds;
}
return Rectangle.Empty;
}
}
public interface IIssueOrder
{