get the benchmark result, very POWERFULL
This commit is contained in:
@@ -39,6 +39,15 @@ using hlsfHandle = StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAlloc
|
||||
|
||||
namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
public enum FreePolicy
|
||||
{
|
||||
|
||||
FullCollect,
|
||||
LazyCollect,
|
||||
NoCollect
|
||||
|
||||
}
|
||||
|
||||
public unsafe partial class HybridLayerSegregatedFitAllocator
|
||||
{
|
||||
|
||||
@@ -79,9 +88,11 @@ namespace StgSharp.HighPerformance.Memory
|
||||
/// </summary>
|
||||
/// <param name="handle">
|
||||
/// Handle to the allocated memory block
|
||||
/// <param name="collectPolicy">
|
||||
/// </param>
|
||||
public void Free(
|
||||
hlsfHandle handle
|
||||
hlsfHandle handle,
|
||||
FreePolicy collectPolicy = FreePolicy.NoCollect
|
||||
)
|
||||
{
|
||||
Entry* e = handle.EntryHandle;
|
||||
@@ -96,7 +107,10 @@ namespace StgSharp.HighPerformance.Memory
|
||||
e->State = EntryState.Empty;
|
||||
|
||||
// Try merge with adjacent free blocks and remove them from buckets
|
||||
MergeAndRemoveFromBuckets(e);
|
||||
if (collectPolicy == FreePolicy.LazyCollect)
|
||||
{
|
||||
MergeAndRemoveFromBuckets(e);
|
||||
}
|
||||
|
||||
// Calculate final level and segment for the merged block
|
||||
long finalSize = e->Size;
|
||||
@@ -108,6 +122,10 @@ namespace StgSharp.HighPerformance.Memory
|
||||
PushBucketToLevel(finalLevel, finalSegment, (BucketNode*)e->Position);
|
||||
|
||||
// Console.WriteLine("one block freed");
|
||||
if (collectPolicy == FreePolicy.FullCollect)
|
||||
{
|
||||
Collect();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ namespace StgSharp.HighPerformance.Memory
|
||||
uint maxBlockSize
|
||||
)
|
||||
{
|
||||
_entries = SlabAllocator<Entry>.Create(64, SlabBufferLayout.Chunked);
|
||||
_entries = SlabAllocator<Entry>.Create(64, SlabBufferLayout.Chunked, false);
|
||||
m_Buffer = (byte *)NativeMemory.AlignedAlloc(byteSize, (nuint)16);
|
||||
_align = 16;
|
||||
_size = byteSize;
|
||||
@@ -111,7 +111,7 @@ namespace StgSharp.HighPerformance.Memory
|
||||
throw new ArgumentException(
|
||||
"Alignment must be a power of two and at least 16 bytes.");
|
||||
}
|
||||
_entries = SlabAllocator<Entry>.Create(64, SlabBufferLayout.Chunked);
|
||||
_entries = SlabAllocator<Entry>.Create(64, SlabBufferLayout.Chunked, false);
|
||||
m_Buffer = (byte *)NativeMemory.AlignedAlloc(byteSize, (nuint)align);
|
||||
_align = align;
|
||||
_size = byteSize;
|
||||
@@ -138,7 +138,7 @@ namespace StgSharp.HighPerformance.Memory
|
||||
throw new ArgumentException(
|
||||
"Alignment must be a power of two and at least 16 bytes.");
|
||||
}
|
||||
_entries = SlabAllocator<Entry>.Create(64, SlabBufferLayout.Chunked);
|
||||
_entries = SlabAllocator<Entry>.Create(64, SlabBufferLayout.Chunked, false);
|
||||
m_Buffer = (byte *)NativeMemory.AlignedAlloc(byteSize, (nuint)align);
|
||||
_align = align;
|
||||
_size = byteSize;
|
||||
|
||||
@@ -38,6 +38,8 @@ namespace StgSharp.HighPerformance.Memory
|
||||
internal sealed unsafe class ChunkedSlabAllocator<T> : SlabAllocator<T> where T : unmanaged
|
||||
{
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
private BufferStack<nuint> _buffers;
|
||||
private BufferStack<nuint> _recycle;
|
||||
private readonly int _elementSize = Unsafe.SizeOf<T>();
|
||||
@@ -90,6 +92,10 @@ namespace StgSharp.HighPerformance.Memory
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
if (_disposed) {
|
||||
return;
|
||||
}
|
||||
_disposed = true;
|
||||
// NativeMemory.Free((void*)_currentBuffer);
|
||||
while (_buffers.TryPop(out nuint buffer))
|
||||
{
|
||||
@@ -97,6 +103,7 @@ namespace StgSharp.HighPerformance.Memory
|
||||
}
|
||||
_buffers.Dispose();
|
||||
_recycle.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
@@ -41,6 +41,8 @@ namespace StgSharp.HighPerformance.Memory
|
||||
internal sealed unsafe class ConcurrentChunkedSlabAllocator<T> : SlabAllocator<T> where T : unmanaged
|
||||
{
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
private readonly BufferExpansionLock _lock = new();
|
||||
private ConcurrentBufferStack<nuint> _buffers;
|
||||
private ConcurrentBufferStack<nuint> _recycle;
|
||||
@@ -114,6 +116,10 @@ namespace StgSharp.HighPerformance.Memory
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
if (_disposed) {
|
||||
return;
|
||||
}
|
||||
_disposed = true;
|
||||
NativeMemory.Free((void*)_currentBuffer);
|
||||
while (_buffers.TryPop(out nuint buffer)) {
|
||||
NativeMemory.Free((void*)buffer);
|
||||
@@ -121,6 +127,7 @@ namespace StgSharp.HighPerformance.Memory
|
||||
_lock.Dispose();
|
||||
_buffers.Dispose();
|
||||
_recycle.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
@@ -37,6 +37,8 @@ namespace StgSharp.HighPerformance.Memory
|
||||
public sealed unsafe class ConcurrentSequentialSlabAllocator<T> : SlabAllocator<T> where T: unmanaged
|
||||
{
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
private T* _buffer;
|
||||
|
||||
private readonly BufferExpansionLock _lock = new();
|
||||
@@ -120,9 +122,14 @@ namespace StgSharp.HighPerformance.Memory
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
if (_disposed) {
|
||||
return;
|
||||
}
|
||||
_disposed = true;
|
||||
_stack.Dispose();
|
||||
NativeMemory.Free(_buffer);
|
||||
_lock.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
Reference in New Issue
Block a user