noexcept or NoExceptionAttribute for async methods #6079
Replies: 5 comments 14 replies
-
What happens when an exception is thrown then? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
To my knowledge, the exception wouldn't even be swallowed - it would bubble up that worker thread's call stack and crash your application. |
Beta Was this translation helpful? Give feedback.
-
I don't think you can really guarantee that exceptions won't be thrown in any meaningful piece of code. The runtime can throw exceptions almost everywhere. |
Beta Was this translation helpful? Give feedback.
-
This is less an argument that the C# language needs to change and more one that there's room for improvement in the JIT compiler - if it could be enhanced to inline methods as you suggest, the benefit would apply across a far wider range of methods and runtimes than a narrowly scoped niche language change.
Have you benchmarked the overhead of the async state machine? I haven't done so myself, but my intuition is that the overhead would be on the close order of 10s or 100s of nanoseconds, which pales into insignificance against the thread scheduling overhead of the OS (usually milliseconds) or round-trip networking (typically milliseconds, but sometimes microseconds in specialist cases). |
Beta Was this translation helpful? Give feedback.
-
Background
In the current version of C#, an async method will yield a state machine with
try-catch
blocks in theMoveNext
method. But this would prevent the JIT optimizing (especially, inlining) theMoveNext
, which will greatly harm the performance of async methods.Proposal
Introduce a keyword
noexcept
or an attributeNoExceptionAttribute
for async methods, then compiler won't generate try-catch blocks for methods annotated withnoexcept
or[NoException]
.This could also be extended to other scenarios to suppress compilers emitting
try-catch
in auto-generated code.Usage
or
and the compiler will lower it to
This is especially useful for
ValueTask<T>
scenarios:Now we will have the potential to inline the async method call.
Beta Was this translation helpful? Give feedback.
All reactions