dotnet / dotnet/winforms

Avoid state machine in InvokeAsync

Open
#12,697 3 comments 1 reaction 1 assignee Claimed by @KlausLoeffelmann View on GitHub
:construction: work in progress
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

In `Control.InvokeAsync` in https://github.com/dotnet/winforms/blob/17db32a122632f4e78b46d0a5b4aca4d1a60c0f7/src/System.Windows.Forms/src/System/Windows/Forms/Control_InvokeAsync.cs#L238 the `async/await` might be elided, at least it could be considered. This applies to both overloads accepting a `Func`.

According to [Stephen Cleary](https://blog.stephencleary.com/2016/12/eliding-async-await.html) this is a simple passthrough and does not harm to elide the keywords, but it's more efficient.

> By not including these keywords, the compiler can skip generating the async state machine. This means that there are fewer compiler-generated types in your assembly, less pressure on the garbage collector, and fewer CPU instructions to execute.
>
> However, it’s important to point out that each of these gains are absolutely minimal. There’s one fewer type, a handful of small objects saved from GC, and only a few CPU instructions skipped. The vast majority of the time, async is dealing with I/O, which completely dwarfs any performance gains. In almost every scenario, eliding async and await doesn’t make any difference to the running time of your application.

> Recommended Guidelines
> I suggest following these guidelines:
>
> [...]
> 2. Do consider eliding when the method is just a passthrough or overload.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.