RecyclableArrayList.Remove & .RemoveAt performance improvements.
This commit is contained in:
1 parent
fd9c2dab82
commit
dead323502
1 file changed
+22
-12
@@ -411,31 +411,41 @@ 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);
|
_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
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||||
public void RemoveAt(int index)
|
public void RemoveAt(int index)
|
||||||
{
|
{
|
||||||
if (index == _count - 1)
|
if (index > _count - 1)
|
||||||
{
|
|
||||||
_count--;
|
|
||||||
}
|
|
||||||
else if (index > _count - 1)
|
|
||||||
{
|
{
|
||||||
ThrowArgumentOutOfRangeException($"Argument \"{nameof(index)}\" = {index} is out of range");
|
ThrowArgumentOutOfRangeException($"Argument \"{nameof(index)}\" = {index} is out of range");
|
||||||
}
|
}
|
||||||
else if (index < --_count)
|
|
||||||
|
_count--;
|
||||||
|
if (index < _count)
|
||||||
{
|
{
|
||||||
Array.Copy(_memoryBlock, index + 1, _memoryBlock, index, _count - index);
|
Array.Copy(_memoryBlock, index + 1, _memoryBlock, index, _count - index);
|
||||||
}
|
}
|
||||||
@@ -443,7 +453,7 @@ 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
|
#pragma warning restore CS8601
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user