Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposed list optimizations #2436

Open
wants to merge 1 commit into
base: 4.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 11 additions & 16 deletions spine-csharp/src/ExposedList.cs
Expand Up @@ -76,35 +76,30 @@ public class ExposedList<T> : IEnumerable<T> {
public void Add (T item) {
// If we check to see if we need to grow before trying to grow
// we can speed things up by 25%
if (Count == Items.Length)
if (Count == Capacity)
GrowIfNeeded(1);
Items[Count++] = item;
version++;
}

public void GrowIfNeeded (int addedCount) {
int minimumSize = Count + addedCount;
if (minimumSize > Items.Length)
if (minimumSize > Capacity)
Capacity = Math.Max(Math.Max(Capacity * 2, DefaultCapacity), minimumSize);
}

public ExposedList<T> Resize (int newSize) {
int itemsLength = Items.Length;
var oldItems = Items;
if (newSize > itemsLength) {
Array.Resize(ref Items, newSize);
} else if (newSize < itemsLength) {
// Allow nulling of T reference type to allow GC.
for (int i = newSize; i < itemsLength; i++)
oldItems[i] = default(T);
}
public ExposedList<T> Resize(int newSize)
{
if (newSize > Capacity)
GrowIfNeeded(newSize - Count);

Count = newSize;
return this;
}

public void EnsureCapacity (int min) {
if (Items.Length < min) {
int newCapacity = Items.Length == 0 ? DefaultCapacity : Items.Length * 2;
if (Capacity < min) {
int newCapacity = Capacity == 0 ? DefaultCapacity : Capacity * 2;
//if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
if (newCapacity < min) newCapacity = min;
Capacity = newCapacity;
Expand Down Expand Up @@ -181,7 +176,7 @@ public class ExposedList<T> : IEnumerable<T> {

public void Clear (bool clearArray = true) {
if (clearArray)
Array.Clear(Items, 0, Items.Length);
Array.Clear(Items, 0, Capacity);

Count = 0;
version++;
Expand Down Expand Up @@ -364,7 +359,7 @@ public class ExposedList<T> : IEnumerable<T> {

public void Insert (int index, T item) {
CheckIndex(index);
if (Count == Items.Length)
if (Count == Capacity)
GrowIfNeeded(1);
Shift(index, 1);
Items[index] = item;
Expand Down