RecyclableList<T>.Helpers renamed to RecyclableListHelpers and made top-level class for better performance.

This commit is contained in:
mlemanczyk committed 2023-07-01 12:53:37 +02:00
1 parent db8b0602c0
commit 11fd6e3dd0
6 files changed
+26 -29

No files matched your search

@@ -3,9 +3,7 @@ using System.Runtime.CompilerServices;
namespace Recyclable.Collections namespace Recyclable.Collections
{ {
public partial class RecyclableList<T> internal static class RecyclableListHelpers<T>
{
internal static class Helpers
{ {
[Obsolete("This method MAY be removed in the near future.")] [Obsolete("This method MAY be removed in the near future.")]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -77,6 +75,7 @@ namespace Recyclable.Collections
return newCapacity; return newCapacity;
} }
[Obsolete("This method WILL be removed in the near future. Please use the existing enumerators instead of it.")] [Obsolete("This method WILL be removed in the near future. Please use the existing enumerators instead of it.")]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<T> Enumerate(RecyclableList<T[]> arrays, int chunkSize, long totalCount) public static IEnumerable<T> Enumerate(RecyclableList<T[]> arrays, int chunkSize, long totalCount)
@@ -270,4 +269,3 @@ namespace Recyclable.Collections
=> (values[yIndex], values[xIndex]) = (values[xIndex], values[yIndex]); => (values[yIndex], values[xIndex]) = (values[xIndex], values[yIndex]);
} }
} }
}
+14 -15
View File
@@ -36,7 +36,7 @@ namespace Recyclable.Collections
{ {
if (targetItemIdx + growByCount > capacity) if (targetItemIdx + growByCount > capacity)
{ {
capacity = Helpers.EnsureCapacity(this, capacity + growByCount); capacity = RecyclableListHelpers<T>.EnsureCapacity(this, capacity + growByCount);
memorySpan = new(_memoryBlock); memorySpan = new(_memoryBlock);
available = capacity - targetItemIdx; available = capacity - targetItemIdx;
} }
@@ -59,7 +59,7 @@ namespace Recyclable.Collections
private void AddRangeWithKnownCount(IEnumerable<T> source, int targetItemIdx, int requiredAdditionalCapacity) private void AddRangeWithKnownCount(IEnumerable<T> source, int targetItemIdx, int requiredAdditionalCapacity)
{ {
_ = Helpers.EnsureCapacity(this, targetItemIdx + requiredAdditionalCapacity); _ = RecyclableListHelpers<T>.EnsureCapacity(this, targetItemIdx + requiredAdditionalCapacity);
Span<T> memorySpan = new(_memoryBlock); Span<T> memorySpan = new(_memoryBlock);
foreach (var item in source) foreach (var item in source)
@@ -82,7 +82,7 @@ namespace Recyclable.Collections
{ {
if (initialCapacity > 0) if (initialCapacity > 0)
{ {
_memoryBlock = Helpers.Resize(_memoryBlock, initialCapacity); _memoryBlock = RecyclableListHelpers<T>.Resize(_memoryBlock, initialCapacity);
_capacity = _memoryBlock.Length; _capacity = _memoryBlock.Length;
} }
} }
@@ -131,7 +131,7 @@ namespace Recyclable.Collections
public RecyclableList(IEnumerable<T> source, int initialCapacity = RecyclableDefaults.Capacity) public RecyclableList(IEnumerable<T> source, int initialCapacity = RecyclableDefaults.Capacity)
{ {
_memoryBlock = Helpers.Resize(_memoryBlock, initialCapacity); _memoryBlock = RecyclableListHelpers<T>.Resize(_memoryBlock, initialCapacity);
_capacity = _memoryBlock.Length; _capacity = _memoryBlock.Length;
AddRange(source); AddRange(source);
} }
@@ -149,7 +149,7 @@ namespace Recyclable.Collections
} }
} }
private int _capacity; internal int _capacity;
public int Capacity public int Capacity
{ {
get => _capacity; get => _capacity;
@@ -157,7 +157,7 @@ namespace Recyclable.Collections
{ {
if (_capacity != value) if (_capacity != value)
{ {
_memoryBlock = Helpers.Resize(_memoryBlock, value); _memoryBlock = RecyclableListHelpers<T>.Resize(_memoryBlock, value);
_capacity = _memoryBlock.Length; _capacity = _memoryBlock.Length;
_version++; _version++;
} }
@@ -175,7 +175,7 @@ namespace Recyclable.Collections
int requiredCapacity = _count + 1; int requiredCapacity = _count + 1;
if (_capacity < requiredCapacity) if (_capacity < requiredCapacity)
{ {
_ = Helpers.EnsureCapacity(this, requiredCapacity); _ = RecyclableListHelpers<T>.EnsureCapacity(this, requiredCapacity);
} }
_memoryBlock[_count++] = item; _memoryBlock[_count++] = item;
@@ -194,14 +194,13 @@ namespace Recyclable.Collections
int targetCapacity = _count + sourceItemsCount; int targetCapacity = _count + sourceItemsCount;
if (_capacity < targetCapacity) if (_capacity < targetCapacity)
{ {
_ = Helpers.EnsureCapacity(this, targetCapacity); _ = RecyclableListHelpers<T>.EnsureCapacity(this, targetCapacity);
} }
var targetSpan = new Span<T>(_memoryBlock, _count, sourceItemsCount); var targetSpan = new Span<T>(_memoryBlock, _count, sourceItemsCount);
Span<T> itemsSpan = new(items); Span<T> itemsSpan = new(items);
itemsSpan.CopyTo(targetSpan); itemsSpan.CopyTo(targetSpan);
_count = targetCapacity; _count = targetCapacity;
_version++; _version++;
} }
@@ -217,7 +216,7 @@ namespace Recyclable.Collections
int targetCapacity = _count + sourceItemsCount; int targetCapacity = _count + sourceItemsCount;
if (_capacity < targetCapacity) if (_capacity < targetCapacity)
{ {
_ = Helpers.EnsureCapacity(this, targetCapacity); _ = RecyclableListHelpers<T>.EnsureCapacity(this, targetCapacity);
} }
var targetSpan = new Span<T>(_memoryBlock, _count, sourceItemsCount); var targetSpan = new Span<T>(_memoryBlock, _count, sourceItemsCount);
@@ -239,7 +238,7 @@ namespace Recyclable.Collections
var targetCapacity = _count + sourceItemsCount; var targetCapacity = _count + sourceItemsCount;
if (_capacity < targetCapacity) if (_capacity < targetCapacity)
{ {
_ = Helpers.EnsureCapacity(this, targetCapacity); _ = RecyclableListHelpers<T>.EnsureCapacity(this, targetCapacity);
} }
items.CopyTo(_memoryBlock, _count); items.CopyTo(_memoryBlock, _count);
@@ -260,7 +259,7 @@ namespace Recyclable.Collections
var targetCapacity = _count + sourceItemsCount; var targetCapacity = _count + sourceItemsCount;
if (_capacity < targetCapacity) if (_capacity < targetCapacity)
{ {
_ = Helpers.EnsureCapacity(this, targetCapacity); _ = RecyclableListHelpers<T>.EnsureCapacity(this, targetCapacity);
} }
items.CopyTo(_memoryBlock, _count); items.CopyTo(_memoryBlock, _count);
@@ -281,7 +280,7 @@ namespace Recyclable.Collections
var targetCapacity = _count + sourceItemsCount; var targetCapacity = _count + sourceItemsCount;
if (_capacity < targetCapacity) if (_capacity < targetCapacity)
{ {
_ = Helpers.EnsureCapacity(this, targetCapacity); _ = RecyclableListHelpers<T>.EnsureCapacity(this, targetCapacity);
} }
var targetSpan = new Span<T>(_memoryBlock, _count, sourceItemsCount); var targetSpan = new Span<T>(_memoryBlock, _count, sourceItemsCount);
@@ -314,7 +313,7 @@ namespace Recyclable.Collections
int targetCapacity = _count + (int)sourceItemsCount; int targetCapacity = _count + (int)sourceItemsCount;
if (_capacity < targetCapacity) if (_capacity < targetCapacity)
{ {
_ = Helpers.EnsureCapacity(this, targetCapacity); _ = RecyclableListHelpers<T>.EnsureCapacity(this, targetCapacity);
} }
Span<T> targetSpan = new Span<T>(_memoryBlock)[_count..], Span<T> targetSpan = new Span<T>(_memoryBlock)[_count..],
@@ -415,7 +414,7 @@ namespace Recyclable.Collections
int requestedCapacity = oldCount + 1; int requestedCapacity = oldCount + 1;
if (_capacity < requestedCapacity) if (_capacity < requestedCapacity)
{ {
_ = Helpers.EnsureCapacity(this, requestedCapacity); _ = RecyclableListHelpers<T>.EnsureCapacity(this, requestedCapacity);
} }
if (oldCount > 0) if (oldCount > 0)
+7 -7
View File
@@ -171,13 +171,13 @@ namespace Recyclable.Collections
} }
} }
public bool Contains(T item) => RecyclableList<T>.Helpers.Contains(Memory, item); public bool Contains(T item) => RecyclableListHelpers<T>.Contains(Memory, item);
public void CopyTo(T[] array, int arrayIndex) => RecyclableList<T>.Helpers.CopyTo(Memory, RemovedCount, BlockSize, (int)(LongCount % BlockSize), array, arrayIndex); public void CopyTo(T[] array, int arrayIndex) => RecyclableListHelpers<T>.CopyTo(Memory, RemovedCount, BlockSize, (int)(LongCount % BlockSize), array, arrayIndex);
public IEnumerable<T> Enumerate() => RecyclableList<T>.Helpers.Enumerate(Memory, BlockSize, LongCount); public IEnumerable<T> Enumerate() => RecyclableListHelpers<T>.Enumerate(Memory, BlockSize, LongCount);
public IEnumerator<T> GetEnumerator() => RecyclableList<T>.Helpers.Enumerate(Memory, BlockSize, LongCount).GetEnumerator(); public IEnumerator<T> GetEnumerator() => RecyclableListHelpers<T>.Enumerate(Memory, BlockSize, LongCount).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => RecyclableList<T>.Helpers.Enumerate(Memory, BlockSize, LongCount).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => RecyclableListHelpers<T>.Enumerate(Memory, BlockSize, LongCount).GetEnumerator();
public int IndexOf(T item) => (int)GetRelativeIndex(RecyclableList<T>.Helpers.LongIndexOf(Memory, BlockSize, item, _equalityComparer)); public int IndexOf(T item) => (int)GetRelativeIndex(RecyclableListHelpers<T>.LongIndexOf(Memory, BlockSize, item, _equalityComparer));
public long LongIndexOf(T item) => GetRelativeIndex(RecyclableList<T>.Helpers.LongIndexOf(Memory, BlockSize, item, _equalityComparer)); public long LongIndexOf(T item) => GetRelativeIndex(RecyclableListHelpers<T>.LongIndexOf(Memory, BlockSize, item, _equalityComparer));
public void Insert(int index, T item) => throw new NotSupportedException(); public void Insert(int index, T item) => throw new NotSupportedException();
public void RemoveAt(int index) => throw new NotSupportedException(); public void RemoveAt(int index) => throw new NotSupportedException();
public bool TryDequeue(out T? item) => TryDequeue(this, out item); public bool TryDequeue(out T? item) => TryDequeue(this, out item);
@@ -68,7 +68,7 @@
BeginUpdate(); BeginUpdate();
try try
{ {
RecyclableList<(TKey Key, TValue Value)>.Helpers.QuickSort(this, _comparer, _randomNumberGenerator); RecyclableListHelpers<(TKey, TValue)>.QuickSort(this, _comparer, _randomNumberGenerator);
} }
finally finally
{ {
@@ -422,7 +422,7 @@ namespace Recyclable.CollectionsTests
// Act // Act
using RecyclableList<long> list = new(testData); using RecyclableList<long> list = new(testData);
RecyclableList<long>.Helpers.QuickSort(list); RecyclableListHelpers<long>.QuickSort(list);
// Validate // Validate
_ = list.Count.Should().Be((int)itemsCount); _ = list.Count.Should().Be((int)itemsCount);
@@ -479,7 +479,7 @@ namespace Recyclable.CollectionsTests
// Act // Act
using RecyclableLongList<long> list = new(testData, minBlockSize: targetBlockSize); using RecyclableLongList<long> list = new(testData, minBlockSize: targetBlockSize);
RecyclableList<long>.Helpers.QuickSort(list); RecyclableListHelpers<long>.QuickSort(list);
// Validate // Validate
_ = list.LongCount.Should().Be(itemsCount); _ = list.LongCount.Should().Be(itemsCount);