@@ -83,20 +83,20 @@ public void Add( T item )
83
83
spin . SpinOnce ( ) ;
84
84
}
85
85
86
- public Task < T > TakeAsync ( CancellationToken cancellationToken )
86
+ public ValueTask < T > TakeAsync ( CancellationToken cancellationToken )
87
87
=> cancellationToken . IsCancellationRequested
88
- ? CanceledTask < T > . Value
88
+ ? CanceledValueTask < T > . Value
89
89
: TakeWithoutValidationAsync ( cancellationToken ) ;
90
90
91
- private Task < T > TakeWithoutValidationAsync ( CancellationToken cancellationToken )
91
+ private ValueTask < T > TakeWithoutValidationAsync ( CancellationToken cancellationToken )
92
92
{
93
93
SpinWait spin = new SpinWait ( ) ;
94
94
95
95
while ( true )
96
96
{
97
- Task < T > result = Volatile . Read ( ref _awaiterTail ) . TryTakeAsync ( cancellationToken ) ;
97
+ ValueTask < T > ? result = Volatile . Read ( ref _awaiterTail ) . TryTakeAsync ( cancellationToken ) ;
98
98
if ( result != null )
99
- return result ;
99
+ return result . Value ;
100
100
101
101
spin . SpinOnce ( ) ;
102
102
}
@@ -260,17 +260,17 @@ private IAwaiter<T> SpinUntilAwaiterIsReady( int slot )
260
260
}
261
261
}
262
262
263
- public Task < T > TryTakeAsync ( CancellationToken cancellationToken )
263
+ public ValueTask < T > ? TryTakeAsync ( CancellationToken cancellationToken )
264
264
=> TryTakeAsync ( cancellationToken , Interlocked . Increment ( ref _awaiterIndex ) ) ;
265
265
266
- private Task < T > TryTakeAsync ( CancellationToken cancellationToken , int slot )
266
+ private ValueTask < T > ? TryTakeAsync ( CancellationToken cancellationToken , int slot )
267
267
=> slot < SegmentSize
268
268
? TryTakeWithoutValidationAsync ( cancellationToken , slot )
269
- : null ;
269
+ : ( ValueTask < T > ? ) null ;
270
270
271
- private Task < T > TryTakeWithoutValidationAsync ( CancellationToken cancellationToken , int slot )
271
+ private ValueTask < T > TryTakeWithoutValidationAsync ( CancellationToken cancellationToken , int slot )
272
272
{
273
- Task < T > result ;
273
+ ValueTask < T > result ;
274
274
275
275
/// The order here differs from what <see cref="TryAdd(T)"/> does: we capture the slot *before* inserting an awaiter.
276
276
/// We do it to avoid allocating an awaiter / registering the cancellation that we're not gonna need in case we lose.
@@ -284,7 +284,7 @@ private Task<T> TryTakeWithoutValidationAsync( CancellationToken cancellationTok
284
284
}
285
285
else
286
286
{
287
- result = Task . FromResult ( _items [ slot ] ) ;
287
+ result = new ValueTask < T > ( _items [ slot ] ) ;
288
288
ClearSlot ( slot ) ;
289
289
}
290
290
0 commit comments