Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upOnly register cancellation token callbacks if needed #2758
Conversation
|
Interesting... Could you post the generated assembly? |
|
Can I use SynchronizationContext.SetSynchronizationContext to avoid using ConfigureAwait(false)?, it's near the end of article. |
|
Yeah. BTW the way things are going with the multiplexing work, we will probably be able to get rid of SingleThreadSynchronizationContext altogether and just do ConfigureAwait(false) as normal. SingleThreadSynchronizationContext was done to avoid deadlocking when executing batches synchronously, and so the same codepaths need to sometimes run with ConfigureAwait(false) (normal async operations) and sometimes on special threads outside the TP (to avoid the deadlock). Am not sure yet, we'll see. |
|
BTW are you OK with merging this? If you want to investigate the assembly go ahead :) |
|
Not quite sure which preview this is but: BenchmarkDotNet=v0.12.0, OS=ubuntu 20.04
Intel Xeon W-2133 CPU 3.60GHz, 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=5.0.100-preview.3.20216.6
[Host] : .NET Core 5.0.0 (CoreCLR 5.0.20.21406, CoreFX 5.0.20.21406), X64 RyuJIT
DefaultJob : .NET Core 5.0.0 (CoreCLR 5.0.20.21406, CoreFX 5.0.20.21406), X64 RyuJIT
SkipWithNullable performs better |
|
Yep, nullable got quite some tweaks, cool to see the culmination. I assume register is still not inlined though. As far as I know one of the latest previews should contain rudimentary auto throw helper jitting, that would then free Register itself for inlining. |
|
Cool, thanks for the info. We can wait with this PR and revisit in another preview or two for sure. |
|
(your responsibility to ping me for more benchmarking |
|
Okay, let's make it a bit prettier and merge. |
| CancellationTokenRegistration registration = default; | ||
| if (cancellationToken.CanBeCanceled) | ||
| registration = cancellationToken.Register(s => ((TaskCompletionSource<NpgsqlConnector?>)s!).SetCanceled(), tcs); | ||
| try | ||
| { | ||
| await tcs.Task; | ||
| } | ||
| finally | ||
| { | ||
| registration.Dispose(); | ||
| } |
This comment has been minimized.
This comment has been minimized.
YohDeadfall
May 25, 2020
Member
| CancellationTokenRegistration registration = default; | |
| if (cancellationToken.CanBeCanceled) | |
| registration = cancellationToken.Register(s => ((TaskCompletionSource<NpgsqlConnector?>)s!).SetCanceled(), tcs); | |
| try | |
| { | |
| await tcs.Task; | |
| } | |
| finally | |
| { | |
| registration.Dispose(); | |
| } | |
| using (var registration = cancellationToken.CanBeCanceled | |
| ? cancellationToken.Register(s => ((TaskCompletionSource<NpgsqlConnector?>)s!).SetCanceled(), tcs) | |
| : default) | |
| { | |
| await tcs.Task; | |
| } |
| using (cancellationToken.Register(cmd => ((NpgsqlCommand)cmd!).Cancel(), this)) | ||
| CancellationTokenRegistration registration = default; | ||
| if (cancellationToken.CanBeCanceled) | ||
| registration = cancellationToken.Register(cmd => ((NpgsqlCommand)cmd!).Cancel(), this); |
This comment has been minimized.
This comment has been minimized.
|
@YohDeadfall looks good, but I propose waiting a bit - if @NinoFloris is right, changes in the runtime/BCL would make all of this superfluous anyway... If the current code as-is runs eventually runs just as fast, better to keep it that way... |
|
From https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-5/
This was the original relevant issue dotnet/runtime#34522 (comment):
It has been moved to after 5.0 |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

roji commentedDec 4, 2019
Rather than always doing registrations on cancellation tokens, do it only if the cancellation token supports it (i.e. isn't
CancellationToken.None). This is probably not very meaningful for Npgsql, but why not do it (in hot paths).Benchmark code: