3 ms·
There's a performance penalty which happens if you don't need async (eg, if the innermost thing is actually using Task.FromResult), and I've never tested but th
by gregmac 5y ago
There's a performance penalty which happens if you don't need async (eg, if the innermost thing is actually using Task.FromResult), and I've never tested but there's probably some cutoff point where if an awaited operation is faster than x, it's faster to write non-async code (though x may vary by cpu or some other factors).
Practicality speaking, this isn't much of a concern for most code, though. I've only really run into this in a couple specific scenarios: unit tests, and when using something like a message bus or cache layer that has a local/in-memory implementation. I noticed only incidentally (as opposed to because of a perf problem): on unit tests where the time got slightly longer when we changed to async, and in the message bus case when doing some performance profiling and using the "local" dev version.
- jayd16 5y agoIf there's no actual yield, async/await IS synchronous.
- int_19h 5y agoThe method still needs to allocate a Task, though. ValueTask along with more aggressive placement of structs in registers helps with that in more recent .NET versions, but it's still not entirely free.
- metaltyphoon 5y agoAsync / Await can actually happen synchronously if there is no yield.
- kqr 5y agoThis is principally a throughput-vs-latency thing, right? Async promises better throughput because concurrency, potentially at the cost of latency.