Use Task.WaitAsync in SemaphoreSlim.WaitAsync#55262
Merged
stephentoub merged 2 commits intodotnet:mainfrom Jul 8, 2021
Merged
Conversation
Member
stephentoub
commented
Jul 7, 2021
| Method | Toolchain | Mean | Ratio | Allocated |
|---|---|---|---|---|
| WithCT | \main\CoreRun.exe | 1.103 us | 1.00 | 496 B |
| WithCT | \pr\CoreRun.exe | 1.032 us | 0.94 | 440 B |
| WithTimeout | \main\CoreRun.exe | 1.492 us | 1.00 | 888 B |
| WithTimeout | \pr\CoreRun.exe | 1.103 us | 0.74 | 536 B |
| WithCTandTimeout | \main\CoreRun.exe | 1.589 us | 1.00 | 904 B |
| WithCTandTimeout | \pr\CoreRun.exe | 1.200 us | 0.75 | 536 B |
|
Tagging subscribers to this area: @mangod9 Issue Details
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using System;
using System.Threading.Tasks;
using System.Threading;
[MemoryDiagnoser]
public class Program
{
public static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
private SemaphoreSlim _sem = new SemaphoreSlim(0, 1);
private CancellationTokenSource _cts = new CancellationTokenSource();
[Benchmark]
public Task WithCT()
{
Task t = _sem.WaitAsync(_cts.Token);
_sem.Release();
return t;
}
[Benchmark]
public Task WithTimeout()
{
Task t = _sem.WaitAsync(TimeSpan.FromMinutes(1));
_sem.Release();
return t;
}
[Benchmark]
public Task WithCTandTimeout()
{
Task t = _sem.WaitAsync(TimeSpan.FromMinutes(1), _cts.Token);
_sem.Release();
return t;
}
}
|
mangod9
approved these changes
Jul 7, 2021
Member
|
Appears that there are some test failures though. |
Member
Author
|
Yes, there's one test deterministically failing. I know why. What I need to figure out is why it ever passed prior to this :-) |
The Cancel_WaitAsync_ContinuationInvokedAsynchronously test was failing, highlighting that we were no longer invoking the continuation asynchronously from the Cancel call. But in fact we were incompletely doing so in the past, such that we'd only force that asynchrony if no timeout was provided... if both a timeout and a token were provided, then we wouldn't. I've enhanced the test to validate both cases, and made sure we now pass.
Member
|
Why not Task.Yield()? Do you care about avoiding the sync context? |
Member
Author
Always :) |
Member
|
What about the extra delegate allocation tho 🙃 |
Member
Author
That occurs only if cancellation is requested? I'm not worried about it :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.