From e11c8436bd96732ce0ea3338ff255c9b16ad903c Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sun, 11 Oct 2020 12:02:19 +0100 Subject: [PATCH] Misc changes to reduce allocation: - Avoid creating new strings in SpriteRenderer.Flush. - ProductionQueue.CancelUnbuildableItems can exit early if the queue is empty. It can also use a set of names for quicker lookups. - OpenGL.CheckGLError avoids a Enum.HasFlag call. --- OpenRA.Game/Graphics/SpriteRenderer.cs | 8 ++++++-- OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs | 5 ++++- OpenRA.Platforms.Default/OpenGL.cs | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/OpenRA.Game/Graphics/SpriteRenderer.cs b/OpenRA.Game/Graphics/SpriteRenderer.cs index 659df9244a..9ba6812f6e 100644 --- a/OpenRA.Game/Graphics/SpriteRenderer.cs +++ b/OpenRA.Game/Graphics/SpriteRenderer.cs @@ -10,17 +10,21 @@ #endregion using System; +using System.Linq; using OpenRA.Primitives; namespace OpenRA.Graphics { public class SpriteRenderer : Renderer.IBatchRenderer { + const int SheetCount = 7; + static readonly string[] SheetIndexToTextureName = Exts.MakeArray(SheetCount, i => "Texture{0}".F(i)); + readonly Renderer renderer; readonly IShader shader; readonly Vertex[] vertices; - readonly Sheet[] sheets = new Sheet[7]; + readonly Sheet[] sheets = new Sheet[SheetCount]; BlendMode currentBlend = BlendMode.Alpha; int nv = 0; @@ -39,7 +43,7 @@ namespace OpenRA.Graphics { for (var i = 0; i < ns; i++) { - shader.SetTexture("Texture{0}".F(i), sheets[i].GetTexture()); + shader.SetTexture(SheetIndexToTextureName[i], sheets[i].GetTexture()); sheets[i] = null; } diff --git a/OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs b/OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs index 2f0766f941..ca4642bec4 100644 --- a/OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs +++ b/OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs @@ -309,7 +309,10 @@ namespace OpenRA.Mods.Common.Traits protected void CancelUnbuildableItems() { - var buildableNames = BuildableItems().Select(b => b.Name).ToList(); + if (Queue.Count == 0) + return; + + var buildableNames = BuildableItems().Select(b => b.Name).ToHashSet(); // EndProduction removes the item from the queue, so we enumerate // by index in reverse to avoid issues with index reassignment diff --git a/OpenRA.Platforms.Default/OpenGL.cs b/OpenRA.Platforms.Default/OpenGL.cs index f8b339dda1..6a1dfb0e6d 100644 --- a/OpenRA.Platforms.Default/OpenGL.cs +++ b/OpenRA.Platforms.Default/OpenGL.cs @@ -740,7 +740,7 @@ namespace OpenRA.Platforms.Default public static void CheckGLError() { // Let the debug message handler log the errors instead. - if (Features.HasFlag(GLFeatures.DebugMessagesCallback)) + if ((Features & GLFeatures.DebugMessagesCallback) == GLFeatures.DebugMessagesCallback) return; var type = glGetError();