@@ -17,25 +17,36 @@ private sealed class PooledCancellationTokenSource : LinkedCancellationTokenSour
1717 private List < CancellationTokenRegistration > ? extraTokens ;
1818 internal PooledCancellationTokenSource ? Next ;
1919
20- public void Add ( CancellationToken token )
21- => Add ( Attach ( token ) ) ;
22-
23- private void Add ( CancellationTokenRegistration registration )
20+ public void AddRange ( ReadOnlySpan < CancellationToken > tokens )
2421 {
25- if ( inlinedTokenCount < InlinedListCapacity )
22+ // register inlined tokens
23+ var inlinedRegistrations = inlinedList . AsSpan ( ) ;
24+ inlinedTokenCount = Math . Min ( inlinedRegistrations . Length , tokens . Length ) ;
25+
26+ for ( var i = 0 ; i < inlinedTokenCount ; i ++ )
27+ {
28+ inlinedRegistrations [ i ] = Attach ( tokens [ i ] ) ;
29+ }
30+
31+ // register extra tokens
32+ tokens = tokens . Slice ( inlinedTokenCount ) ;
33+ if ( tokens . IsEmpty )
34+ return ;
35+
36+ if ( extraTokens is null )
2637 {
27- Unsafe . Add ( ref FirstInlinedRegistration , inlinedTokenCount ++ ) = registration ;
38+ extraTokens = new ( tokens . Length ) ;
2839 }
2940 else
3041 {
31- extraTokens ??= new ( ) ;
32- extraTokens . Add ( registration ) ;
42+ extraTokens . EnsureCapacity ( tokens . Length ) ;
3343 }
34- }
3544
36- [ DebuggerBrowsable ( DebuggerBrowsableState . Never ) ]
37- private ref CancellationTokenRegistration FirstInlinedRegistration
38- => ref Unsafe . As < InlinedTokenList , CancellationTokenRegistration > ( ref inlinedList ) ;
45+ foreach ( var token in tokens )
46+ {
47+ extraTokens . Add ( Attach ( token ) ) ;
48+ }
49+ }
3950
4051 public int Count => inlinedTokenCount + ( extraTokens ? . Count ?? 0 ) ;
4152
0 commit comments