Skip to content
Merged
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
14 changes: 12 additions & 2 deletions src/Nethermind/Nethermind.Core/Collections/ArrayListCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public static void ClearToCount(T[] array, int count)
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ClearTail(T[] array, int newCount, int oldCount)
{
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>() && newCount < oldCount)
{
Array.Clear(array, newCount, oldCount - newCount);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add(
ArrayPool<T> pool,
Expand Down Expand Up @@ -108,9 +117,9 @@ public static void ReduceCount(
ClearToCount(oldArray, oldCount);
pool.Return(oldArray);
}
else if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
else
{
Array.Clear(array, newCount, oldCount - newCount);
ClearTail(array, newCount, oldCount);
}

[DoesNotReturn]
Expand Down Expand Up @@ -228,6 +237,7 @@ public static void Insert(
public static void Truncate(int newLength, T[] array, ref int count)
{
GuardIndex(newLength, count, shouldThrow: true, allowEqualToCount: true);
ClearTail(array, newLength, count);
count = newLength;
}

Expand Down