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

null-out removed elements from SlimBag<T> and ActiveList<T> #149

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Jitter2/Collision/DynamicTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace Jitter2.Collision;
/// <typeparam name="T">The type of elements stored in the dynamic tree.</typeparam>
public class DynamicTree<T> where T : class, IDynamicTreeProxy, IListIndex
{
private SlimBag<T>[] lists = Array.Empty<SlimBag<T>>();
private volatile SlimBag<T>[] lists = Array.Empty<SlimBag<T>>();

private readonly ActiveList<T> activeList;

Expand Down Expand Up @@ -466,6 +466,11 @@ private void ScanForMovedProxies(Parallel.Batch batch)

// else proxy is well contained within the nodes expanded Box:
}

// Make sure we do not hold too many dangling references
// in the internal array of the SlimBag<T> data structure which might
// prevent GC. But do only free them one-by-one to prevent overhead.
list.NullOutOne();
}

private void ScanForOverlaps(int fraction, bool add)
Expand Down
16 changes: 14 additions & 2 deletions src/Jitter2/DataStructures/ActiveList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ public T this[int i]

public void Clear()
{
Array.Clear(elements, 0, Count);
for (int i = 0; i < Count; i++)
{
elements[i].ListIndex = -1;
elements[i] = null!;
}

Count = 0;
Active = 0;
}
Expand Down Expand Up @@ -188,8 +193,15 @@ public void Remove(T element)
Debug.Assert(element.ListIndex != -1);

MoveToInactive(element);

int li = element.ListIndex;

Count -= 1;
Swap(Count, element.ListIndex);

elements[li] = elements[Count];
elements[li].ListIndex = li;
elements[Count] = null!;

element.ListIndex = -1;
}

Expand Down
9 changes: 9 additions & 0 deletions src/Jitter2/DataStructures/SlimBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,13 @@ public void NullOut()
Array.Clear(array, counter, nullOut - counter);
nullOut = counter;
}

/// <summary>
/// Null out a single position in the internal array.
/// </summary>
public void NullOutOne()
{
if(nullOut <= counter) return;
array[--nullOut] = default!;
}
}
25 changes: 25 additions & 0 deletions src/JitterTests/MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,31 @@ public static void SlimBagTest()
bag.NullOut();
Assert.That(bag[0], Is.Not.EqualTo(null));
Assert.That(bag[1], Is.EqualTo(null));

bag.Clear();
bag.NullOut();

bag.Add(new object());
bag.Add(new object());
bag.Add(new object());

bag.NullOutOne();

Assert.That(bag[0], Is.Not.EqualTo(null));
Assert.That(bag[1], Is.Not.EqualTo(null));
Assert.That(bag[2], Is.Not.EqualTo(null));

bag.NullOutOne();
Assert.That(bag[2], Is.Not.EqualTo(null));

bag.NullOutOne();
Assert.That(bag[2], Is.EqualTo(null));

bag.NullOutOne();

Assert.That(bag[0], Is.Not.EqualTo(null));
Assert.That(bag[1], Is.Not.EqualTo(null));
Assert.That(bag[2], Is.EqualTo(null));
}

[TestCase]
Expand Down
Loading