66using System . Runtime . CompilerServices ;
77using System . Threading ;
88using System . Runtime . InteropServices ;
9+ using System . Threading . Tasks ;
10+
911#if NET5_0_OR_GREATER
1012using System . Runtime . Intrinsics . Arm ;
1113using System . Runtime . Intrinsics . X86 ;
@@ -24,12 +26,12 @@ namespace NetworkLibrary
2426 */
2527 public class BufferPool
2628 {
29+ static ConcurrentDictionary < byte [ ] , string > AAA = new ConcurrentDictionary < byte [ ] , string > ( ) ;
2730 public static bool ForceGCOnCleanup = true ;
2831 public static int MaxMemoryBeforeForceGc = 100000000 ;
2932 public const int MaxBufferSize = 1073741824 ;
3033 public const int MinBufferSize = 256 ;
31- private static readonly ConcurrentBag < WeakReference < byte [ ] > > weakReferencePool = new ConcurrentBag < WeakReference < byte [ ] > > ( ) ;
32- private static readonly ConcurrentBag < WeakReference < byte [ ] > > [ ] bufferBuckets = new ConcurrentBag < WeakReference < byte [ ] > > [ 32 ] ;
34+ private static readonly ConcurrentBag < byte [ ] > [ ] bufferBuckets = new ConcurrentBag < byte [ ] > [ 32 ] ;
3335 private static SortedDictionary < int , int > bucketCapacityLimits = new SortedDictionary < int , int > ( )
3436 {
3537 { 256 , 10000 } ,
@@ -63,9 +65,8 @@ public class BufferPool
6365
6466 static BufferPool ( )
6567 {
66- Init ( ) ;
67- memoryMaintainer = new Thread ( MaintainMemory ) ;
68- memoryMaintainer . Priority = ThreadPriority . Lowest ;
68+ Init ( ) ;
69+ MaintainMemory ( ) ;
6970 }
7071
7172 /// <summary>
@@ -94,25 +95,24 @@ private static void Init()
9495 //bufferBuckets = new ConcurrentDictionary<int, ConcurrentBag<byte[]>>();
9596 for ( int i = 8 ; i < 31 ; i ++ )
9697 {
97- bufferBuckets [ i ] = new ConcurrentBag < WeakReference < byte [ ] > > ( ) ;
98+ bufferBuckets [ i ] = new ConcurrentBag < byte [ ] > ( ) ;
9899 }
99100 }
100101
101- private static void MaintainMemory ( )
102+ private static async void MaintainMemory ( )
102103 {
103- var lastTime = process . TotalProcessorTime ;
104104 while ( true )
105105 {
106- autoGcHandle . WaitOne ( ) ;
107- Thread . Sleep ( 10000 ) ;
108- var currentProcTime = process . TotalProcessorTime ;
109- var deltaT = ( lastTime - currentProcTime ) . TotalMilliseconds ;
110- lastTime = currentProcTime ;
111-
112- if ( deltaT < 100 && process . WorkingSet64 < MaxMemoryBeforeForceGc )
113- GC . Collect ( ) ;
106+ await Task . Delay ( 10000 ) ;
107+ for ( int i = 8 ; i < 31 ; i ++ )
108+ {
109+ while ( bufferBuckets [ i ] . Count > bucketCapacityLimits [ GetBucketSize ( i ) ] )
110+ {
111+ if ( bufferBuckets [ i ] . TryTake ( out var buffer ) )
112+ AAA . TryRemove ( buffer , out _ ) ;
113+ }
114+ }
114115 }
115-
116116 }
117117
118118
@@ -125,6 +125,7 @@ private static void MaintainMemory()
125125 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
126126 public static byte [ ] RentBuffer ( int size )
127127 {
128+ //return new byte[size];
128129 byte [ ] buffer ;
129130 if ( MaxBufferSize < size )
130131 throw new InvalidOperationException (
@@ -133,13 +134,10 @@ public static byte[] RentBuffer(int size)
133134
134135 int idx = GetBucketIndex ( size ) ;
135136
136- while ( bufferBuckets [ idx ] . TryTake ( out WeakReference < byte [ ] > bufferRef ) )
137+ if ( bufferBuckets [ idx ] . TryTake ( out buffer ) )
137138 {
138- if ( bufferRef . TryGetTarget ( out buffer ) )
139- {
140- weakReferencePool . Add ( bufferRef ) ;
141- return buffer ;
142- }
139+ AAA . TryRemove ( buffer , out _ ) ;
140+ return buffer ;
143141 }
144142
145143 buffer = ByteCopy . GetNewArray ( GetBucketSize ( idx ) ) ;
@@ -156,15 +154,14 @@ public static void ReturnBuffer(byte[] buffer)
156154 {
157155 if ( buffer . Length <= MinBufferSize ) return ;
158156
159- int idx = GetBucketIndex ( buffer . Length ) ;
160- if ( weakReferencePool . TryTake ( out var wr ) )
157+ if ( ! AAA . TryAdd ( buffer , null ) )
161158 {
162- wr . SetTarget ( buffer ) ;
163- bufferBuckets [ idx - 1 ] . Add ( wr ) ;
164-
159+ MiniLogger . Log ( MiniLogger . LogLevel . Error , "Buffer Pool Duplicated return detected" ) ;
160+ return ;
165161 }
166- else
167- bufferBuckets [ idx - 1 ] . Add ( new WeakReference < byte [ ] > ( buffer ) ) ;
162+
163+ int idx = GetBucketIndex ( buffer . Length ) ;
164+ bufferBuckets [ idx - 1 ] . Add ( buffer ) ;
168165 buffer = null ;
169166 }
170167
0 commit comments