11using System . Diagnostics ;
22using System . Runtime . CompilerServices ;
3- using System . Runtime . InteropServices ;
43
54namespace DotNext . Threading ;
65
@@ -13,57 +12,54 @@ private sealed class PooledCancellationTokenSource : LinkedCancellationTokenSour
1312 private static readonly int InlinedListCapacity = GetCapacity < InlinedTokenList > ( ) ;
1413
1514 private InlinedTokenList inlinedList ;
16- private int inlinedTokenCount ;
17- private List < CancellationTokenRegistration > ? extraTokens ;
15+ private int count ;
16+ private CancellationTokenRegistration [ ] ? extraTokens ;
1817 internal PooledCancellationTokenSource ? Next ;
1918
2019 public void AddRange ( ReadOnlySpan < CancellationToken > tokens )
2120 {
2221 // register inlined tokens
2322 var inlinedRegistrations = inlinedList . AsSpan ( ) ;
24- inlinedTokenCount = Math . Min ( inlinedRegistrations . Length , tokens . Length ) ;
23+ var inlinedCount = Math . Min ( inlinedRegistrations . Length , tokens . Length ) ;
2524
26- for ( var i = 0 ; i < inlinedTokenCount ; i ++ )
25+ for ( var i = 0 ; i < inlinedCount ; i ++ )
2726 {
2827 inlinedRegistrations [ i ] = Attach ( tokens [ i ] ) ;
2928 }
3029
3130 // register extra tokens
32- tokens = tokens . Slice ( inlinedTokenCount ) ;
31+ tokens = tokens . Slice ( inlinedCount ) ;
32+ count = inlinedCount + tokens . Length ;
3333 if ( tokens . IsEmpty )
3434 return ;
3535
36- if ( extraTokens is null )
36+ if ( extraTokens is null || extraTokens . Length < tokens . Length )
3737 {
38- extraTokens = new ( tokens . Length ) ;
39- }
40- else
41- {
42- extraTokens . EnsureCapacity ( tokens . Length ) ;
38+ extraTokens = new CancellationTokenRegistration [ tokens . Length ] ;
4339 }
4440
45- foreach ( var token in tokens )
41+ for ( var i = 0 ; i < tokens . Length ; i ++ )
4642 {
47- extraTokens . Add ( Attach ( token ) ) ;
43+ extraTokens [ i ] = Attach ( tokens [ i ] ) ;
4844 }
4945 }
5046
51- public int Count => inlinedTokenCount + ( extraTokens ? . Count ?? 0 ) ;
47+ public int Count => count ;
5248
5349 public ref readonly CancellationTokenRegistration this [ int index ]
5450 {
5551 get
5652 {
57- Debug . Assert ( ( uint ) index < ( uint ) Count ) ;
58-
53+ Debug . Assert ( ( uint ) index < ( uint ) count ) ;
54+
5955 Span < CancellationTokenRegistration > registrations ;
6056 if ( index < InlinedListCapacity )
6157 {
6258 registrations = inlinedList . AsSpan ( ) ;
6359 }
6460 else
6561 {
66- registrations = CollectionsMarshal . AsSpan ( extraTokens ) ;
62+ registrations = extraTokens ;
6763 index -= InlinedListCapacity ;
6864 }
6965
@@ -73,9 +69,14 @@ public ref readonly CancellationTokenRegistration this[int index]
7369
7470 public void Reset ( )
7571 {
76- inlinedTokenCount = 0 ;
7772 inlinedList = default ;
78- extraTokens ? . Clear ( ) ;
73+
74+ if ( extraTokens is not null && count > InlinedListCapacity )
75+ {
76+ Array . Clear ( extraTokens , 0 , count - InlinedListCapacity ) ;
77+ }
78+
79+ count = 0 ;
7980 }
8081
8182 private static int GetCapacity < T > ( )
0 commit comments