I highly doubt this allocation will ever make a noticeable difference. It only allocates when the animation changes AND the buffer is not large enough. We could set the initial size larger, but the first time resize happens does not impact GC because the initial list size is empty, so the first time is allocation only (which is extremely fast in a managed language).
@Pharan, peeking at ExposedList, this looks like a problem:
spine-runtimes/ExposedList.cs at 3.6
Eg, if I have 100 objects in a list, then Resize(50)
, Count
is set to 50 but the items remain in the array at indices 50 to 99. Those items can never be GCed until the list size increases and the references are overwritten, which may never happen. This is why libgdx Array works as it does, references outside of 0 to Count
must be nulled:
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/utils/Array.java#L341-L346
Note truncate is doing the nulling (C# land would use Array.Clear
):
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/utils/Array.java#L434-L439
Exposing Resize
is dangerous, as it doesn't clean up the references, and should be private/protected (or maybe just documented, since this list exposes its internals which is inherently unsafe, eg it allows Count
to be set, which has the same problem). I suggest using the same implementation and method names as libgdx, which is thoroughly battle tested and doing so makes porting more accurate.
There is a chance for a small optimization: Resize
uses Array.Resize
which creates a new array and copies the old array to the new. This is not exactly what we need
we only need to copy from 0 to Count - 1
to the new array, not 0 to Items.Length - 1
. We'd be every so slightly better off allocating a new array and copying only up to Count - 1
.
Sometimes when code calls setSize
, it doesn't care about the current contents of the array, so there is no need to copy that data to the new array. This can be achieved like this:
list.Clear();
list.SetSize(123); // Count is 0 so nothing is copied from old to new array if a resize occurs.