• Bugs
  • [Unity] Small bug causing garbage for meshes each frame

Related Discussions
...

Hi Nate,

Was hunting down some garbage SkeletonRenderer.cs was generating and think I have identified and fixed a small issue.

Currently at the Setup Mesh Stage (line 201) you do:

float[] tempVertices = this.tempVertices;

with this.tempVertices initially being set to a size of 8 for RegionAttachments.

However, for meshes you re-allocate as necessary to accomodate their (mostly larger) vertice length. I noticed though that you re-allocate the local copy and not the global copy.

Changing the lines 246 and 267 from

tempVertices = new float[meshVertexCount];

to

this.tempVertices = tempVertices = new float[meshVertexCount];

or something along those lines updates the global array too and solves the issue, which results in 0 bytes of garbage for me now once the skeleton has been around for a while and displayed all potential meshes/triangles etc.

I imagine for others with a lot of meshes and vertices this was probably generating quite some garbage over time. Hope you can update the source accordingly. Anyway, pretty simple bug that just slipped under the radar.

Cheers, Rob 🙂

P.s. Maybe also get rid of the global private variable mesh, as at the moment you only try and destroy then null it in Reset, but it is never assigned to, you use a local with the same name in LateUpdate instead 😉

Nice catch. I think this was an oversight.
I think the way you recoded it was the way it was intended to work. SkeletonRenderer.tempVertices is supposed to be a buffer array that increases persistent size when it needs to.

Did you see good performance improvements?

I think Nate doesn't use Unity Pro so he can't really check GC stuff like this himself.

Good find, thanks! Both are done. 🙂

Great 🙂
As you said Pharan I figured this was the desired behavior as soon as I found it, but it took a while to hunt down where those bytes were being allocated each frame. Can't say I really saw any performance improvement, but less garbage is always a good thing 😃
To be honest I have extended the functionality of SkeletonRenderer to meet my own devious requirements quite a bit now, so my benchmarks are probably off the leaner code in the repo by a bit. Hopefully soon I can showcase what I have added 😉
Take care guys