4 ms·
The most common issue I see with async programming is that the naive style seen in most samples/docs is strictly slower than standard imperative programming for
by jiggawatts 5y ago
The most common issue I see with async programming is that the naive style seen in most samples/docs is strictly slower than standard imperative programming for one user. In other words, it's pure overhead with no benefit at all unless you're at a very large scale and approaching 100% capacity on your hosts.
Most documentation -- and most code I've seen in the wild -- reads like this:
var foo = await GetFooAsync(...);
var bar = await GetBarAsync(...);
var baz = await GetBazAsync(...);
The timeline of that code is exactly same as the standard synchronous version, just with extra steps and pauses.
The following version is more verbose -- which makes it feel slower -- but can provide dramatic speed ups even for a single user by overlapping requests so that they run concurrently:
var fooTask = GetFooAsync(...);
var barTask = GetBarAsync(...);
var bazTask = GetBazAsync(...);
var foo = await fooTask;
var bar = await barTask;
var baz = await bazTask;
Unfortunately, I've literally never seen this design pattern in the field...
- itslennysfault 5y agoDoes C# not have an equivalent to JavaScript's Promise.all?? In JS this could be... const [ fooTask, barTask, bazTask, ] = await Promise.all([ GetFooAsync(...), GetBarAsync(...), GetBazAsync(...) ]); ... PS in your code above you assign GetBazAsync to bar and baz. :-)
- jiggawatts 5y agoFixed! I believe this kind of copy-paste "last line effect" one of the most common errors in programming: https://hownot2code.com/2016/08/15/the-last-line-effect-typos-and-fun/ https://hownot2code.com/2016/08/15/the-last-line-effect-typo... Task.WaitAll is the C# equivalent: https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.waitall https://docs.microsoft.com/en-us/dotnet/api/system.threading... But it's not necessarily faster, there are corner cases where waiting for all tasks prevents some concurrent computations (e.g.: JSON parsing) from occurring.
- contextfree 5y agoThere is Task.WhenAll which works similarly, but the problem is that it requires either all of the tasks to have the same return type, or else treat all the tasks in the array as untyped and extract the return values in a separate step. i.e. you have to write var (fooTask, barTask, bazTask) = (GetFooAsync(), GetBarAsync(), GetBazAsync()); await Task.WhenAll(fooTask, barTask, bazTask); var (foo, bar, baz) = (fooTask.Result, barTask.Result, bazTask.Result); It's possible to write a custom awaiter extension method that allows awaiting tuples of tasks, so once that's in place you can just write var (foo, bar, baz) = await (GetFooAsync(), GetBarAsync(), GetBazAsync()); There are third-party packages that do this for you and it's reasonably easy to write yourself if you understand the inner workings of async/await, but it's not part of the standard library.
- stillblue 5y agovar foo = await GetFooAsync(...); var bar = await GetBarAsync(...); var baz = await GetBazAsync(...); Unfortunately this is the kind of example that is being used in the MS docs and elsewhere to explain async. I spent a long time figuring out what the difference was when they ran the exact same way as the synchronous version.
- omegalulw 5y agoThis is indeed bad for usual cases but in general you can't say it's always bad. For example, if you need to call 1 million asnyc methods, you will benefit from batching them and running a small number at a time.
- Matthias247 5y ago"slower" is just one piece of the calculation. On the server end one goal of async/await is that you can run 10k instances of your 3 lines of code concurrently - inside a single thread. And while this might not use parallelism to make an individual operation faster, it might use less resources overall. The other use-case was to run multi-step operations which involve waiting on UI threads of appplications, which wouldn't have worked with blocking waits (would prevent redraw). For that use-case "speed" also isn't the highest priority.
- jiggawatts 5y agoLike I said, this is a theoretical benefit that is realised only if the load is sufficiently high for the reduced overhead of async programming to provide a noticeable benefit. For naive async code, there is a surprisingly narrow range of loads where this is true: only something like 80-99% load. Any higher and latencies start to go towards the stratosphere, or memory usage grows exponentially. Of course, this is fixable with the appropriate use of backpressure and timeout cancellations, but I've never seen this implemented correctly and consistently anywhere. Almost all web apps in the wild fall over when load goes from 100% to 101%. They don't become 1% slower! Instead they take 30s to return a page or just start spewing 5xx errors. For a point of comparison, Java is abandoning the complex and fragile async approach in favour of user-mode scheduled lightweight threads, which are vaguely similar in terms of efficiency, but are much easier for programmers to understand. They're also compatible with traditional threaded code.