Constructors simpliefied. GetItem/SetItem replaced with direct field use for performance gain. SetNewLength optimized for 32-bit long arrays.
This commit is contained in:
1 parent
6246a38cbe
commit
28b52c555d
1 file changed
+52
-59
@@ -61,17 +61,52 @@ namespace Recyclable.Collections
|
|||||||
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private T[] SetNewLength(long newSize)
|
private T[] SetNewLength(in long newSize)
|
||||||
{
|
{
|
||||||
var source = _memory;
|
if (newSize == _capacity)
|
||||||
T[] newArray = newSize.RentArrayFromPool<T>();
|
|
||||||
if (newArray.LongLength >= source.LongLength)
|
|
||||||
{
|
{
|
||||||
source.CopyTo(newArray, 0);
|
return _memory;
|
||||||
|
}
|
||||||
|
|
||||||
|
var source = _memory;
|
||||||
|
var sourceLength = source.LongLength;
|
||||||
|
var is32bit = newSize <= int.MaxValue;
|
||||||
|
T[] newArray;
|
||||||
|
if (is32bit)
|
||||||
|
{
|
||||||
|
var newSize32bit = (int)newSize;
|
||||||
|
newArray = newSize32bit.RentArrayFromPool<T>();
|
||||||
|
if (newSize32bit >= sourceLength)
|
||||||
|
{
|
||||||
|
if (sourceLength > 0)
|
||||||
|
{
|
||||||
|
Memory<T> sourceMemory = new(source);
|
||||||
|
Memory<T> newArrayMemory = new(newArray);
|
||||||
|
sourceMemory.CopyTo(newArrayMemory);
|
||||||
|
source.ReturnToPool();
|
||||||
|
}
|
||||||
|
|
||||||
|
return newArray;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Span<T> sourceSpan = source.AsSpan();
|
||||||
|
Span<T> newArraySpan = newArray.AsSpan();
|
||||||
|
sourceSpan = sourceSpan[..newSize32bit];
|
||||||
|
sourceSpan.CopyTo(newArraySpan);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
CopyItems(0, newArray.LongLength, ref newArray, 0);
|
newArray = newSize.RentArrayFromPool<T>();
|
||||||
|
if (newSize >= sourceLength)
|
||||||
|
{
|
||||||
|
source.CopyTo(newArray, 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CopyItems(0, newArray.LongLength, ref newArray, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
source.ReturnToPool();
|
source.ReturnToPool();
|
||||||
@@ -82,56 +117,14 @@ namespace Recyclable.Collections
|
|||||||
|
|
||||||
public RecyclableBlockList(long initialCapacity = RecyclableDefaults.Capacity)
|
public RecyclableBlockList(long initialCapacity = RecyclableDefaults.Capacity)
|
||||||
{
|
{
|
||||||
//EnsureCapacity(capacity);
|
_memory = SetNewLength(initialCapacity);
|
||||||
if (_capacity < initialCapacity)
|
_capacity = initialCapacity;
|
||||||
{
|
|
||||||
long newCapacity;
|
|
||||||
switch (_capacity > 0)
|
|
||||||
{
|
|
||||||
case true:
|
|
||||||
newCapacity = _capacity * 2;
|
|
||||||
while (newCapacity < initialCapacity)
|
|
||||||
{
|
|
||||||
newCapacity *= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case false:
|
|
||||||
newCapacity = initialCapacity;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
_capacity = newCapacity;
|
|
||||||
_memory = SetNewLength(newCapacity);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public RecyclableBlockList(IEnumerable<T> source, int initialCapacity = RecyclableDefaults.Capacity)
|
public RecyclableBlockList(IEnumerable<T> source, int initialCapacity = RecyclableDefaults.Capacity)
|
||||||
{
|
{
|
||||||
//EnsureCapacity(capacity);
|
_memory = SetNewLength(initialCapacity);
|
||||||
if (_capacity < initialCapacity)
|
_capacity = initialCapacity;
|
||||||
{
|
|
||||||
long newCapacity;
|
|
||||||
switch (_capacity > 0)
|
|
||||||
{
|
|
||||||
case true:
|
|
||||||
newCapacity = _capacity * 2;
|
|
||||||
while (newCapacity < initialCapacity)
|
|
||||||
{
|
|
||||||
newCapacity *= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case false:
|
|
||||||
newCapacity = initialCapacity;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
_capacity = newCapacity;
|
|
||||||
_memory = SetNewLength(newCapacity);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var item in source)
|
foreach (var item in source)
|
||||||
{
|
{
|
||||||
@@ -141,14 +134,14 @@ namespace Recyclable.Collections
|
|||||||
|
|
||||||
public T this[long index]
|
public T this[long index]
|
||||||
{
|
{
|
||||||
get => GetItem(_memory, index);
|
get => _memory[index];
|
||||||
set => SetItem(_memory, index, value);
|
set => _memory[index] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T this[int index]
|
public T this[int index]
|
||||||
{
|
{
|
||||||
get => GetItem(_memory, index);
|
get => _memory[index];
|
||||||
set => SetItem(_memory, index, value);
|
set => _memory[index] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private long _capacity;
|
private long _capacity;
|
||||||
@@ -192,8 +185,8 @@ namespace Recyclable.Collections
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
_capacity = newCapacity;
|
|
||||||
_memory = SetNewLength(newCapacity);
|
_memory = SetNewLength(newCapacity);
|
||||||
|
_capacity = newCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
_memory[LongCount] = item;
|
_memory[LongCount] = item;
|
||||||
@@ -222,8 +215,8 @@ namespace Recyclable.Collections
|
|||||||
// break;
|
// break;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// _capacity = newCapacity;
|
|
||||||
// _memory = SetNewLength(newCapacity);
|
// _memory = SetNewLength(newCapacity);
|
||||||
|
// _capacity = newCapacity;
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
|
||||||
@@ -269,8 +262,8 @@ namespace Recyclable.Collections
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
_capacity = newCapacity;
|
|
||||||
_memory = SetNewLength(newCapacity);
|
_memory = SetNewLength(newCapacity);
|
||||||
|
_capacity = newCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var toMoveIdx = LongCount - 1; toMoveIdx >= index; toMoveIdx--)
|
for (var toMoveIdx = LongCount - 1; toMoveIdx >= index; toMoveIdx--)
|
||||||
|
|||||||
Reference in new issue
Block a user