RecyclableArrayList.Insert performance improvements. Tiny cleanup.

This commit is contained in:
mlemanczyk committed 2023-01-14 20:03:15 +01:00
1 parent 164c435d98
commit 7d72e1ea32
1 file changed
+6 -15
+6 -15
View File
@@ -103,7 +103,7 @@ namespace Recyclable.Collections
set => _memory[index] = value; set => _memory[index] = value;
} }
internal int _capacity; protected int _capacity;
public int Capacity public int Capacity
{ {
get => _capacity; get => _capacity;
@@ -149,12 +149,6 @@ namespace Recyclable.Collections
var targetSpan = new Span<T>(_memory)[_count..]; var targetSpan = new Span<T>(_memory)[_count..];
Span<T> itemsSpan = new(items); Span<T> itemsSpan = new(items);
itemsSpan.CopyTo(targetSpan); itemsSpan.CopyTo(targetSpan);
//for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;)
//{
// targetSpan[targetItemIdx++] = itemsSpan[sourceItemIdx++];
//}
_count = targetCapacity; _count = targetCapacity;
} }
@@ -226,7 +220,7 @@ namespace Recyclable.Collections
public int IndexOf(T itemToFind) public int IndexOf(T itemToFind)
{ {
Span<T> memorySpan = _memory; Span<T> memorySpan = new(_memory);
int itemCount = _count; int itemCount = _count;
var equalityComparer = _equalityComparer; var equalityComparer = _equalityComparer;
for (var itemIdx = 0; itemIdx < itemCount; itemIdx++) for (var itemIdx = 0; itemIdx < itemCount; itemIdx++)
@@ -249,13 +243,10 @@ namespace Recyclable.Collections
_ = EnsureCapacity(requestedCapacity); _ = EnsureCapacity(requestedCapacity);
} }
Span<T> memorySpan = new(_memory); var sourceSpan = new Span<T>(_memory)[index..];
for (var toMoveIdx = _count - 1; toMoveIdx >= index; toMoveIdx--) var targetSpan = new Span<T>(_memory)[(index + 1)..];
{ sourceSpan.CopyTo(targetSpan);
memorySpan[toMoveIdx + 1] = memorySpan[toMoveIdx]; sourceSpan[index] = item;
}
memorySpan[index] = item;
_count++; _count++;
} }