RecyclableArrayList performance improvements
This commit is contained in:
1 parent
eded9ac372
commit
164c435d98
2 files changed
+45
-15
No files matched your search
@@ -0,0 +1,8 @@
|
|||||||
|
// This file is used by Code Analysis to maintain SuppressMessage
|
||||||
|
// attributes that are applied to this project.
|
||||||
|
// Project-level suppressions either have no target or are given
|
||||||
|
// a specific target and scoped to a namespace, type, member, etc.
|
||||||
|
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
[assembly: SuppressMessage("Style", "IDE0032:Use auto property", Justification = "<Pending>", Scope = "member", Target = "~F:Recyclable.Collections.RecyclableArrayList`1._count")]
|
||||||
@@ -16,7 +16,7 @@ namespace Recyclable.Collections
|
|||||||
throw new ArgumentOutOfRangeException("index");
|
throw new ArgumentOutOfRangeException("index");
|
||||||
}
|
}
|
||||||
|
|
||||||
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
protected static T[] SetNewLength(in T[]? source, in int newSize)
|
protected static T[] SetNewLength(in T[]? source, in int newSize)
|
||||||
{
|
{
|
||||||
ArrayPool<T> arrayPool = _arrayPool;
|
ArrayPool<T> arrayPool = _arrayPool;
|
||||||
@@ -36,9 +36,8 @@ namespace Recyclable.Collections
|
|||||||
return newArray;
|
return newArray;
|
||||||
|
|
||||||
case false:
|
case false:
|
||||||
Span<T> sourceSpan = new(source);
|
var sourceSpan = new Span<T>(source)[..newSize];
|
||||||
Span<T> newArraySpan = new(newArray);
|
Span<T> newArraySpan = new(newArray);
|
||||||
sourceSpan = sourceSpan[..newSize];
|
|
||||||
sourceSpan.CopyTo(newArraySpan);
|
sourceSpan.CopyTo(newArraySpan);
|
||||||
source!.ReturnToPool(arrayPool);
|
source!.ReturnToPool(arrayPool);
|
||||||
return newArray;
|
return newArray;
|
||||||
@@ -118,7 +117,7 @@ namespace Recyclable.Collections
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int _count;
|
protected int _count;
|
||||||
public int Count
|
public int Count
|
||||||
{
|
{
|
||||||
get => _count;
|
get => _count;
|
||||||
@@ -147,12 +146,14 @@ namespace Recyclable.Collections
|
|||||||
_ = EnsureCapacity(targetCapacity);
|
_ = EnsureCapacity(targetCapacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var targetSpan = new Span<T>(_memory)[_count..];
|
||||||
Span<T> itemsSpan = new(items);
|
Span<T> itemsSpan = new(items);
|
||||||
Span<T> targetSpan = new(_memory);
|
itemsSpan.CopyTo(targetSpan);
|
||||||
for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;)
|
|
||||||
{
|
//for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;)
|
||||||
targetSpan[targetItemIdx++] = itemsSpan[sourceItemIdx++];
|
//{
|
||||||
}
|
// targetSpan[targetItemIdx++] = itemsSpan[sourceItemIdx++];
|
||||||
|
//}
|
||||||
|
|
||||||
_count = targetCapacity;
|
_count = targetCapacity;
|
||||||
}
|
}
|
||||||
@@ -166,12 +167,33 @@ namespace Recyclable.Collections
|
|||||||
_ = EnsureCapacity(targetCapacity);
|
_ = EnsureCapacity(targetCapacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
Span<T> targetSpan = new(_memory);
|
items.CopyTo(_memory, _count);
|
||||||
for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;)
|
//Span<T> targetSpan = new(_memory);
|
||||||
{
|
//for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;)
|
||||||
targetSpan[targetItemIdx++] = items[sourceItemIdx++];
|
//{
|
||||||
|
// targetSpan[targetItemIdx++] = items[sourceItemIdx++];
|
||||||
|
//}
|
||||||
|
|
||||||
|
_count = targetCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void AddRange(in RecyclableArrayList<T> items)
|
||||||
|
{
|
||||||
|
var sourceItemsCount = items._count;
|
||||||
|
var targetCapacity = _count + sourceItemsCount;
|
||||||
|
if (_capacity < targetCapacity)
|
||||||
|
{
|
||||||
|
_ = EnsureCapacity(targetCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
var targetSpan = new Span<T>(_memory)[_count..];
|
||||||
|
Span<T> itemsSpan = new(items._memory);
|
||||||
|
itemsSpan.CopyTo(targetSpan);
|
||||||
|
//for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;)
|
||||||
|
//{
|
||||||
|
// targetSpan[targetItemIdx++] = itemsSpan[sourceItemIdx++];
|
||||||
|
//}
|
||||||
|
//
|
||||||
_count = targetCapacity;
|
_count = targetCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,10 +295,10 @@ namespace Recyclable.Collections
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (Capacity > 0)
|
if (_capacity > 0)
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
Capacity = 0;
|
_capacity = 0;
|
||||||
_memory.ReturnToPool(_arrayPool);
|
_memory.ReturnToPool(_arrayPool);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user