Bug in amount of vertex memory allocated when outlining?

Hi - I have been trying to dig into why the outline generation on text is so slow but in the process i think i have found a bug where it reserves much more space for the vertices than is necesary:

protected void ApplyShadowZeroAlloc(List<UIVertex> verts, Color32 color, int start, int end, float x, float y)
        {
            UIVertex vt;

            var neededCpacity = verts.Count * 2;
            if (verts.Capacity < neededCpacity)
                verts.Capacity = neededCpacity;

The problem is that verts.Count is increasing with each shadow drawn - I beleive the correct thing to do is add (end - start) rather than multiply by 2 (but could be wrong on that).

As far as I can tell this memory will be free’d up again once the verts have been added to the mesh so this problem only results in a spike in memory use but it would still be cool to get a fix as for a paragraph of outlined text the allocation can be quite large

What code was this taken from? I’ve seen a similar pattern in a custom user script called NicerOutline, which does the same (lots of allocations)

https://bitbucket.org/Unity-Technologies/ui/src/cc791b3335d2d46b70d932fc70e6ec6c4ea6d561/UnityEngine.UI/UI/Core/VertexModifiers/Shadow.cs

I believe the NicerOutline does the same a Outline but creates more shadows as the standard up, down, left, right approach doesn’t work so well for thicker outlines.

To be clear there will still be a fair bit of allocation going on in either case but I think this maybe be making it close to double what is necessary

It is also worth noting that outline performance was bad every frame prior to 5.2 patch 3. In the patch the per frame perf is better but the initialisation is still not so good…

I actually found this issue exactly today in NicerOutline and plan on submitting a PR for it.

Do you have any suggestion on how to optimize this ?

Have a look at the Pull Request i have submitted: https://bitbucket.org/ddreaper/unity-ui-extensions/pull-requests/12/improve-memory-behaviour-by-pre-allocating/diff

Actually my PR is not 100% right, but you are indeed right that it does allocate (Outline) too much memory.