working on dynamic wrapping

This commit is contained in:
Youmu Liu
2025-11-06 18:12:04 +08:00
parent ea0e6f3ff6
commit ec1b8dbd51
9 changed files with 145 additions and 612 deletions
@@ -47,7 +47,7 @@ namespace StgSharp.HighPerformance.Memory
return new BufferStack<T>(4); // Default small capacity
}
BufferStack<T> stack = new BufferStack<T>(span.Length);
BufferStack<T> stack = new(span.Length);
// Use internal method for efficient bulk initialization
@@ -82,7 +82,7 @@ namespace StgSharp.HighPerformance.Memory
private byte* _buffer;
private readonly BufferExpansionLock _lock = new();
private ulong _count;
private long _count;
public ConcurrentBufferStack()
{
@@ -101,7 +101,7 @@ namespace StgSharp.HighPerformance.Memory
public ulong Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Volatile.Read(ref _count);
get => (ulong)Volatile.Read(ref _count);
}
public void Clear()
@@ -141,7 +141,7 @@ namespace StgSharp.HighPerformance.Memory
try
{
Header* header = (Header*)_buffer;
nuint oldTop = Volatile.Read(ref header->Top);
int oldTop = Volatile.Read(ref header->Top);
if (oldTop <= 0) {
throw new InvalidOperationException("Stack underflow");
}
@@ -170,8 +170,8 @@ namespace StgSharp.HighPerformance.Memory
try
{
Header* header = (Header*)_buffer;
nuint topSnapshot = Volatile.Read(ref header->Top);
nuint cap = Volatile.Read(ref header->Capacity);
int topSnapshot = Volatile.Read(ref header->Top);
int cap = Volatile.Read(ref header->Capacity);
if (topSnapshot >= cap)
{
// Need expansion
@@ -182,8 +182,8 @@ namespace StgSharp.HighPerformance.Memory
header = (Header*)_buffer;
if (header->Top >= header->Capacity)
{
nuint newCap = header->Capacity * 2;
nuint newSize = (nuint)sizeof(Header) + (newCap * (nuint)sizeof(T));
int newCap = header->Capacity * 2;
nuint newSize = (nuint)sizeof(Header) + ((nuint)newCap * (nuint)sizeof(T));
_lock.EnterBufferCopy();
try
@@ -216,7 +216,7 @@ namespace StgSharp.HighPerformance.Memory
try
{
Header* header = (Header*)_buffer;
nuint idx = header->Top; // reserve next slot
int idx = header->Top; // reserve next slot
if (idx >= header->Capacity)
{
// lost race against expander or other producer; retry
@@ -244,7 +244,7 @@ namespace StgSharp.HighPerformance.Memory
try
{
Header* header = (Header*)_buffer;
nuint oldTop = Volatile.Read(ref header->Top);
int oldTop = Volatile.Read(ref header->Top);
if (oldTop <= 0)
{
value = default;
@@ -263,10 +263,8 @@ namespace StgSharp.HighPerformance.Memory
{
_lock.ExitBufferRead();
}
value = default;
return true;
Thread.Sleep(0);
}
Thread.Sleep(0); // Yield to avoid busy waiting
}
// Internal method for efficient bulk initialization
@@ -282,28 +280,28 @@ namespace StgSharp.HighPerformance.Memory
fixed (T* sourcePtr = span) {
Buffer.MemoryCopy(sourcePtr, dataStart,
header->Capacity * (nuint)sizeof(T),
(nuint)header->Capacity * (nuint)sizeof(T),
(nuint)span.Length * (nuint)sizeof(T));
}
header->Top = (nuint)span.Length;
Volatile.Write(ref _count, (ulong)span.Length);
header->Top = span.Length;
Volatile.Write(ref _count, span.Length);
}
private void AllocateBuffer(int capacity)
{
nuint size = (nuint)sizeof(Header) + ((nuint)capacity * (nuint)sizeof(T));
nuint size = (nuint)sizeof(Header) + (nuint)capacity * (nuint)sizeof(T);
_buffer = (byte*)NativeMemory.Alloc(size);
Header* header = (Header*)_buffer;
header->Top = 0;
header->Capacity = (nuint)capacity;
header->Capacity = capacity;
}
private struct Header
{
public nuint Capacity; // max capacity of the stack (number of elements)
public nuint Top; // next available index in the stack
public int Capacity; // max capacity of the stack (number of elements)
public int Top; // next available index in the stack
}