Skip to content

Commit

Permalink
Misc changes to reduce allocation:
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
RoosterDragon authored and abcdefg30 committed Oct 17, 2020
1 parent c23efea commit e11c843
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
8 changes: 6 additions & 2 deletions OpenRA.Game/Graphics/SpriteRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +43,7 @@ public void Flush()
{
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;
}

Expand Down
5 changes: 4 additions & 1 deletion OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,10 @@ protected virtual void TickInner(Actor self, bool allProductionPaused)

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
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Platforms.Default/OpenGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ public static void WriteGraphicsLog(string message)
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();
Expand Down

0 comments on commit e11c843

Please sign in to comment.