RecyclableList.ResizeAndCopy moved back to RecyclableListHelpers.

This commit is contained in:
mlemanczyk committed 2023-07-03 01:57:12 +02:00
1 parent b1bcd8ea03
commit fe77863bba
2 files changed
+30 -32

No files matched your search

@@ -227,6 +227,35 @@ namespace Recyclable.Collections
private static readonly bool NeedsClearing = !typeof(T).IsValueType;
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
public static void ResizeAndCopy(RecyclableList<T> list)
{
// TODO: Measure performance
// T[] newMemoryBlock = RecyclableListHelpers<T>._arrayPool.Rent(_capacity <<= 1);
T[] oldMemoryBlock = list._memoryBlock;
list._memoryBlock = (list._capacity <<= 1) < RecyclableDefaults.MinPooledArrayLength
? new T[list._capacity]
: _arrayPool.Rent(list._capacity);
// & WAS SLOWER WITHOUT
new Span<T>(oldMemoryBlock).CopyTo(list._memoryBlock);
if (oldMemoryBlock.Length >= RecyclableDefaults.MinPooledArrayLength)
{
// TODO: Measure gain vs relying on arrayPool to clear
//if (NeedsClearing)
//{
// Array.Clear(source);
//}
// // If anything, it has been already cleared above, so we don't need to repeat it.
_arrayPool.Return(oldMemoryBlock, NeedsClearing);
}
// list._memoryBlock = newMemoryBlock;
list._capacity = list._memoryBlock.Length;
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
public static void ResizeAndCopy(RecyclableList<T> sourceList, int newSize)
{
+1 -32
View File
@@ -183,43 +183,12 @@ namespace Recyclable.Collections
public bool IsReadOnly => false;
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private void ResizeAndCopy()
{
// TODO: Measure performance
// ArrayPool<T> arrayPool = _arrayPool;
// TODO: Measure performance after RecyclableArrayPool is reworked
// T[] newMemoryBlock = RecyclableListHelpers<T>._arrayPool.Rent(_capacity <<= 1);
T[] newMemoryBlock = (_capacity <<= 1) >= RecyclableDefaults.MinPooledArrayLength
? RecyclableListHelpers<T>._arrayPool.Rent(_capacity)
: new T[_capacity];
// & WAS SLOWER WITHOUT
T[] oldMemoryBlock = _memoryBlock;
new Span<T>(oldMemoryBlock).CopyTo(newMemoryBlock);
if (oldMemoryBlock.Length >= RecyclableDefaults.MinPooledArrayLength)
{
// TODO: Measure gain vs relying on arrayPool to clear
//if (NeedsClearing)
//{
// Array.Clear(source);
//}
// // If anything, it has been already cleared above, so we don't need to repeat it.
RecyclableListHelpers<T>._arrayPool.Return(oldMemoryBlock, NeedsClearing);
}
_memoryBlock = newMemoryBlock;
_capacity = newMemoryBlock.Length;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(T item)
{
if (_count == _capacity)
{
ResizeAndCopy();
RecyclableListHelpers<T>.ResizeAndCopy(this);
}
_memoryBlock[_count++] = item;