Skip to content

Commit

Permalink
initial FreeList size is 1
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Dec 29, 2023
1 parent b7ef16f commit 82e6f93
Showing 1 changed file with 4 additions and 40 deletions.
44 changes: 4 additions & 40 deletions src/R3/Internal/FreeListCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace R3.Internal;
internal struct FreeListCore<T>
where T : class
{
const int InitialArraySize = 4;

readonly object gate;
T?[]? values = null;
int lastIndex;
Expand All @@ -19,40 +17,6 @@ public FreeListCore(object gate)
this.gate = gate;
}

public FreeListCore(object gate, int capacity)
{
this.gate = gate;
this.values = new T[capacity];
}

public FreeListCore(object gate, T[] items)
{
this.gate = gate;
this.values = new T[items.Length];
for (int i = 0; i < items.Length; i++)
{
this.values[i] = items[i];
}
}

public FreeListCore(object gate, IEnumerable<T> items)
{
this.gate = gate;
if (items.TryGetNonEnumeratedCount(out var count))
{
this.values = new T[count];
}
else
{
this.values = new T[InitialArraySize];
}
var i = 0;
foreach (var item in items)
{
this.values[i++] = item;
}
}

public bool IsDisposed => lastIndex == -2;

public ReadOnlySpan<T?> AsSpan()
Expand All @@ -71,16 +35,16 @@ public void Add(T item, out int removeKey)

if (values == null)
{
values = new T[InitialArraySize];
values = new T[1]; // initial size is 1.
}

// try find blank
var index = FindNullIndex(values);
if (index == -1)
{
// full, resize(x1.5)
// full, 1, 4, 6,...resize(x1.5)
var len = values.Length;
var newValues = new T[len + (len / 2)];
var newValues = len == 1 ? new T[4] : new T[len + (len / 2)];
Array.Copy(values, newValues, len);
Volatile.Write(ref values, newValues);
index = len;
Expand All @@ -92,7 +56,7 @@ public void Add(T item, out int removeKey)
Volatile.Write(ref lastIndex, index);
}

removeKey = index; // index is remove key.
removeKey = index; // index is remove key.
}
}

Expand Down

0 comments on commit 82e6f93

Please sign in to comment.