Additional, optimized constructors & AddRange variants added to RecyclableArrayList. Benchmarks extended.
This commit is contained in:
1 parent
878ec4924a
commit
8984a3fac0
11 files changed
+484
-75
No files matched your search
@@ -82,7 +82,6 @@ namespace Recyclable.Collections
|
||||
public RecyclableArrayList(int initialCapacity)
|
||||
#pragma warning restore CS8618
|
||||
{
|
||||
// _memory will be initialized when the 1st item is added
|
||||
if (initialCapacity > 0)
|
||||
{
|
||||
_memory = SetNewLength(_memory, initialCapacity);
|
||||
@@ -90,6 +89,34 @@ namespace Recyclable.Collections
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning disable CS8618 // _memory will be initialized when the 1st item is added
|
||||
public RecyclableArrayList(RecyclableArrayList<T> source)
|
||||
#pragma warning restore CS8618
|
||||
{
|
||||
AddRange(source);
|
||||
}
|
||||
|
||||
#pragma warning disable CS8618 // _memory will be initialized when the 1st item is added
|
||||
public RecyclableArrayList(in T[] source)
|
||||
#pragma warning restore CS8618
|
||||
{
|
||||
AddRange(source);
|
||||
}
|
||||
|
||||
#pragma warning disable CS8618 // _memory will be initialized when the 1st item is added
|
||||
public RecyclableArrayList(List<T> source)
|
||||
#pragma warning restore CS8618
|
||||
{
|
||||
AddRange(source);
|
||||
}
|
||||
|
||||
#pragma warning disable CS8618 // _memory will be initialized when the 1st item is added
|
||||
public RecyclableArrayList(IList<T> source)
|
||||
#pragma warning restore CS8618
|
||||
{
|
||||
AddRange(source);
|
||||
}
|
||||
|
||||
public RecyclableArrayList(IEnumerable<T> source, int initialCapacity = RecyclableDefaults.Capacity)
|
||||
{
|
||||
_memory = SetNewLength(_memory, initialCapacity);
|
||||
@@ -126,7 +153,7 @@ namespace Recyclable.Collections
|
||||
|
||||
public bool IsReadOnly { get; } = false;
|
||||
|
||||
public void Add(in T item)
|
||||
public void Add(T item)
|
||||
{
|
||||
int requestedCapacity = _count + 1;
|
||||
if (_capacity < requestedCapacity)
|
||||
@@ -152,7 +179,7 @@ namespace Recyclable.Collections
|
||||
_count = targetCapacity;
|
||||
}
|
||||
|
||||
public void AddRange(in List<T> items)
|
||||
public void AddRange(List<T> items)
|
||||
{
|
||||
var sourceItemsCount = items.Count;
|
||||
var targetCapacity = _count + sourceItemsCount;
|
||||
@@ -165,7 +192,20 @@ namespace Recyclable.Collections
|
||||
_count = targetCapacity;
|
||||
}
|
||||
|
||||
public void AddRange(in RecyclableArrayList<T> items)
|
||||
public void AddRange(IList<T> items)
|
||||
{
|
||||
var sourceItemsCount = items.Count;
|
||||
var targetCapacity = _count + sourceItemsCount;
|
||||
if (_capacity < targetCapacity)
|
||||
{
|
||||
_ = EnsureCapacity(targetCapacity);
|
||||
}
|
||||
|
||||
items.CopyTo(_memory, _count);
|
||||
_count = targetCapacity;
|
||||
}
|
||||
|
||||
public void AddRange(RecyclableArrayList<T> items)
|
||||
{
|
||||
var sourceItemsCount = items._count;
|
||||
var targetCapacity = _count + sourceItemsCount;
|
||||
@@ -180,10 +220,17 @@ namespace Recyclable.Collections
|
||||
_count = targetCapacity;
|
||||
}
|
||||
|
||||
public void AddRange(IEnumerable<T> source)
|
||||
public void AddRange(IEnumerable<T> source, int growByCount = 100)
|
||||
{
|
||||
ref int capacity = ref _capacity;
|
||||
ref int count = ref _count;
|
||||
foreach (var item in source)
|
||||
{
|
||||
if (capacity == count)
|
||||
{
|
||||
_ = EnsureCapacity(capacity * growByCount);
|
||||
}
|
||||
|
||||
Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 22 KiB |
@@ -3,7 +3,7 @@
|
||||
public class RecyclableDefaults
|
||||
{
|
||||
public const int BlockSize = 10_240;
|
||||
public const int Capacity = 8;
|
||||
public const int Capacity = 1;
|
||||
public const int MinPooledArrayLength = 100;
|
||||
}
|
||||
}
|
||||
@@ -121,11 +121,26 @@ namespace Recyclable.Collections
|
||||
return -1;
|
||||
}
|
||||
|
||||
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
//public static RecyclableList<T> ToRecyclableList<T>(this IList<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static RecyclableList<T> ToRecyclableList<T>(this IList<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||
|
||||
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
//public static RecyclableList<T> ToRecyclableList<T>(this IEnumerable<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static RecyclableList<T> ToRecyclableList<T>(this List<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static RecyclableArrayList<T> ToRecyclableArrayList<T>(this IEnumerable<T> values) => new(values, values.TryGetNonEnumeratedCount(out int count) ? count : RecyclableDefaults.Capacity);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static RecyclableArrayList<T> ToRecyclableArrayList<T>(this T[] values) => new(values);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static RecyclableArrayList<T> ToRecyclableArrayList<T>(this List<T> values) => new(values);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static RecyclableArrayList<T> ToRecyclableArrayList<T>(this RecyclableArrayList<T> values) => new(values);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static RecyclableList<T> ToRecyclableList<T>(this IEnumerable<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static T[] RentArrayFromPool<T>(this int minSize, ArrayPool<T> arrayPool) => (minSize >= _minPooledArraySize)
|
||||
|
||||
Reference in new issue
Block a user