optimize para performance of buffered stack

This commit is contained in:
Youmu Liu
2025-11-05 20:33:58 +08:00
parent 9c4c71dc5b
commit ea0e6f3ff6
10 changed files with 516 additions and 91 deletions
@@ -42,7 +42,7 @@ namespace StgSharp.Threading
public class BufferExpansionLock : IDisposable
{
private static readonly ThreadLocal<int> Count = new();
private static readonly ThreadLocal<int> Count = new(() => 0);
private readonly ReaderWriterLockSlim _copyLock;
private readonly ReaderWriterLockSlim _expansionLock;
@@ -74,13 +74,11 @@ namespace StgSharp.Threading
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EnterBufferRead()
{
if (++Count.Value == 1)
{
int c = Count.Value;
if (c == 0) {
_copyLock.EnterReadLock();
} else
{
Count.Value++;
}
Count.Value = c + 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -104,12 +102,10 @@ namespace StgSharp.Threading
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ExitBufferRead()
{
if (--Count.Value == 0)
{
int c = Count.Value - 1;
Count.Value = c;
if (c == 0) {
_copyLock.ExitReadLock();
} else
{
Count.Value--;
}
}