RecyclableArrayList.Remove & .RemoveAt performance improvements.

This commit is contained in:
mlemanczyk committed 2023-04-08 01:13:37 +02:00
1 parent fd9c2dab82
commit dead323502
1 file changed
+32 -22
+32 -22
View File
@@ -411,31 +411,14 @@ namespace Recyclable.Collections
_count++; _count++;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public bool Remove(T itemToRemove) public bool Remove(T itemToRemove)
{ {
var itemIdx = IndexOf(itemToRemove); var index = IndexOf(itemToRemove);
if (itemIdx >= 0) if (index >= 0)
{
RemoveAt(itemIdx);
return true;
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveAt(int index)
{
if (index == _count - 1)
{ {
_count--; _count--;
} if (index < _count)
else if (index > _count - 1)
{
ThrowArgumentOutOfRangeException($"Argument \"{nameof(index)}\" = {index} is out of range");
}
else if (index < --_count)
{ {
Array.Copy(_memoryBlock, index + 1, _memoryBlock, index, _count - index); Array.Copy(_memoryBlock, index + 1, _memoryBlock, index, _count - index);
} }
@@ -443,7 +426,34 @@ namespace Recyclable.Collections
if (NeedsClearing) if (NeedsClearing)
{ {
#pragma warning disable CS8601 // In real use cases we'll never access it #pragma warning disable CS8601 // In real use cases we'll never access it
new Span<T>(_memoryBlock, _count, 1)[0] = default; _memoryBlock[_count] = default;
#pragma warning restore CS8601
}
return true;
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public void RemoveAt(int index)
{
if (index > _count - 1)
{
ThrowArgumentOutOfRangeException($"Argument \"{nameof(index)}\" = {index} is out of range");
}
_count--;
if (index < _count)
{
Array.Copy(_memoryBlock, index + 1, _memoryBlock, index, _count - index);
}
if (NeedsClearing)
{
#pragma warning disable CS8601 // In real use cases we'll never access it
_memoryBlock[_count] = default;
#pragma warning restore CS8601 #pragma warning restore CS8601
} }
} }