Skip to content

Commit 82fffa1

Browse files
committed
Cached count for better performance
1 parent b3b0286 commit 82fffa1

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/DotNext.Threading/Threading/CancellationTokenMultiplexer.CTS.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private ref CancellationTokenRegistration FirstInlinedRegistration
3939

4040
public int Count => inlinedTokenCount + (extraTokens?.Count ?? 0);
4141

42-
public ref CancellationTokenRegistration this[int index]
42+
public ref readonly CancellationTokenRegistration this[int index]
4343
{
4444
get
4545
{

src/DotNext.Threading/Threading/CancellationTokenMultiplexer.Scope.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ private static CancellationToken GetToken(ValueTuple<object> value)
7979
/// <inheritdoc/>
8080
public void Dispose()
8181
{
82-
if (source is not null)
82+
if (source?.Count is { } count)
8383
{
8484
Debug.Assert(multiplexerOrToken.Item1 is CancellationTokenMultiplexer);
8585

86-
for (var i = 0; i < source.Count; i++)
86+
for (var i = 0; i < count; i++)
8787
{
8888
source[i].Dispose();
8989
}
@@ -101,7 +101,7 @@ public ValueTask DisposeAsync()
101101

102102
private static async ValueTask ReturnAsync(CancellationTokenMultiplexer multiplexer, PooledCancellationTokenSource source)
103103
{
104-
for (var i = 0; i < source.Count; i++)
104+
for (int i = 0, count = source.Count; i < count; i++)
105105
{
106106
await source[i].DisposeAsync().ConfigureAwait(false);
107107
}

0 commit comments

Comments
 (0)