@@ -7,9 +7,9 @@ partial class CancellationTokenMultiplexer
77 private sealed class PooledCancellationTokenSource : LinkedCancellationTokenSource , IResettable
88 {
99 private const int Capacity = 3 ;
10- private ( CancellationTokenRegistration , CancellationTokenRegistration , CancellationTokenRegistration ) inlineList ;
10+ private ( CancellationTokenRegistration , CancellationTokenRegistration , CancellationTokenRegistration ) inlinedList ;
11+ private byte inlinedTokenCount ;
1112 private List < CancellationTokenRegistration > ? extraTokens ;
12- private int tokenCount ;
1313 internal PooledCancellationTokenSource ? Next ;
1414
1515 public void Add ( CancellationToken token )
@@ -18,24 +18,24 @@ public void Add(CancellationToken token)
1818 private ref CancellationTokenRegistration Add ( )
1919 {
2020 Span < CancellationTokenRegistration > registrations ;
21- var index = tokenCount ;
22- if ( tokenCount < Capacity )
21+ int index ;
22+ if ( inlinedTokenCount < Capacity )
2323 {
24- registrations = inlineList . AsSpan ( ) ;
24+ index = inlinedTokenCount ++ ;
25+ registrations = inlinedList . AsSpan ( ) ;
2526 }
2627 else
2728 {
2829 extraTokens ??= new ( ) ;
30+ index = extraTokens . Count ;
2931 extraTokens . Add ( default ) ;
3032 registrations = CollectionsMarshal . AsSpan ( extraTokens ) ;
31- index -= Capacity ;
3233 }
3334
34- tokenCount ++ ;
3535 return ref registrations [ index ] ;
3636 }
3737
38- public int Count => tokenCount ;
38+ public int Count => inlinedTokenCount + extraTokens ? . Count ?? 0 ;
3939
4040 public ref CancellationTokenRegistration this [ int index ]
4141 {
@@ -44,7 +44,7 @@ public ref CancellationTokenRegistration this[int index]
4444 Span < CancellationTokenRegistration > registrations ;
4545 if ( index < Capacity )
4646 {
47- registrations = inlineList . AsSpan ( ) ;
47+ registrations = inlinedList . AsSpan ( ) ;
4848 }
4949 else
5050 {
@@ -58,8 +58,8 @@ public ref CancellationTokenRegistration this[int index]
5858
5959 public void Reset ( )
6060 {
61- tokenCount = 0 ;
62- inlineList = default ;
61+ inlinedTokenCount = 0 ;
62+ inlinedList = default ;
6363 extraTokens ? . Clear ( ) ;
6464 }
6565
0 commit comments