diff --git a/Recyclable.Collections/RecyclableArrayList.cs b/Recyclable.Collections/RecyclableArrayList.cs index 6779dc8..794d55b 100644 --- a/Recyclable.Collections/RecyclableArrayList.cs +++ b/Recyclable.Collections/RecyclableArrayList.cs @@ -21,25 +21,36 @@ namespace Recyclable.Collections { ArrayPool arrayPool = _arrayPool; var sourceLength = source?.Length ?? 0; - T[] newMemoryBlock = newSize.RentArrayFromPool(arrayPool); + Span sourceSpan, newArraySpan; + T[] newMemoryBlock = newSize >= RecyclableDefaults.MinPooledArrayLength + ? arrayPool.Rent(newSize) + : new T[newSize]; + switch (newSize >= sourceLength) { case true: if (sourceLength > 0) { - Memory sourceMemory = new(source); - Memory newArrayMemory = new(newMemoryBlock); - sourceMemory.CopyTo(newArrayMemory); - source!.ReturnToPool(arrayPool); + sourceSpan = new Span(source); + newArraySpan = new Span(newMemoryBlock); + sourceSpan.CopyTo(newArraySpan); + if (sourceLength >= RecyclableDefaults.MinPooledArrayLength) + { + arrayPool.Return(source!); + } } return newMemoryBlock; case false: - var sourceSpan = new Span(source)[..newSize]; - Span newArraySpan = new(newMemoryBlock); + sourceSpan = new Span(source)[..newSize]; + newArraySpan = new(newMemoryBlock); sourceSpan.CopyTo(newArraySpan); - source!.ReturnToPool(arrayPool); + if (sourceLength >= RecyclableDefaults.MinPooledArrayLength) + { + arrayPool.Return(source!); + } + return newMemoryBlock; } }