Fix the previous commit for improving memory consumption.

Allocate the list with the correct capacity ahead, and avoid any needs for increasing the capacity when applying the shadow later.

--HG--
branch : liortal/improve-memory-behaviour-by-preallocatin-1444835033293
release
Lior Tal 2015-10-15 06:53:35 +00:00
parent 0845316a6c
commit 6cba848e43
1 changed files with 7 additions and 12 deletions

View File

@ -8,8 +8,7 @@ namespace UnityEngine.UI.Extensions
[AddComponentMenu("UI/Effects/Extensions/Nicer Outline")] [AddComponentMenu("UI/Effects/Extensions/Nicer Outline")]
public class NicerOutline : BaseMeshEffect public class NicerOutline : BaseMeshEffect
{ {
// A constant factor used when allocating the vertex list. private const int VERTICES_PER_QUAD = 6;
private const int GROWTH_FACTOR = 24;
[SerializeField] [SerializeField]
private Color m_EffectColor = new Color (0f, 0f, 0f, 0.5f); private Color m_EffectColor = new Color (0f, 0f, 0f, 0.5f);
@ -94,10 +93,6 @@ namespace UnityEngine.UI.Extensions
{ {
UIVertex vt; UIVertex vt;
var neededCpacity = verts.Count * 2;
if (verts.Capacity < neededCpacity)
verts.Capacity = neededCpacity;
for (int i = start; i < end; ++i) for (int i = start; i < end; ++i)
{ {
vt = verts[i]; vt = verts[i];
@ -117,10 +112,6 @@ namespace UnityEngine.UI.Extensions
protected void ApplyShadow(List<UIVertex> verts, Color32 color, int start, int end, float x, float y) protected void ApplyShadow(List<UIVertex> verts, Color32 color, int start, int end, float x, float y)
{ {
var neededCpacity = verts.Count * 2;
if (verts.Capacity < neededCpacity)
verts.Capacity = neededCpacity;
ApplyShadowZeroAlloc(verts, color, start, end, x, y); ApplyShadowZeroAlloc(verts, color, start, end, x, y);
} }
@ -131,8 +122,12 @@ namespace UnityEngine.UI.Extensions
{ {
return; return;
} }
List < UIVertex > verts = new List<UIVertex>(GROWTH_FACTOR * mesh.vertices.Length);
using (var helper = new VertexHelper(mesh)) // Initilize a list with the correct capacity, to avoid unneeded allocations later.
// The list will hold 9 copies of the vertex data (original + 8 copies).
List < UIVertex > verts = new List<UIVertex>(9 * (mesh.vertices.Length / 4 * VERTICES_PER_QUAD));
using (var helper = new VertexHelper(mesh))
{ {
helper.GetUIVertexStream(verts); helper.GetUIVertexStream(verts);
} }