5 ms·
It's not really fair to compare async/await systems with Go and Java. Go and Java with lightweight threads provide a full-blown system, without any limitations
by cyberax 3y ago
It's not really fair to compare async/await systems with Go and Java.
Go and Java with lightweight threads provide a full-blown system, without any limitations. You don't have to "color" your code into async and blocking functions, everything "just works". Go can even interrupt tight inner loops with async pre-emption.
The downside is that Go needs around 2k of stack minimum for each goroutine. Java is similar, but has a lower per-thread constant.
The upside is that Go can be _much_ faster due to these contiguous stacks. With async/await each yield point is basically isomorphic to a segmented stack segment. I did a quick benchmark to show this: https://blog.alex.net/benchmarking-go-and-c-async-await https://blog.alex.net/benchmarking-go-and-c-async-await
- jimsimmons 3y agoNone of what you say is a gotcha. An ignorant programmer stands only to benefit from Go
- djsavvy 3y agoIn your Go benchmark code, how is the calculation actually being done across threads? I don't see the `go` keyword anywhere in the source code.
- h4x0rr 3y agoI'm wondering that as well. The c# code seems really unrealistic though, using a lot of tasks for CPU bound work. A fair comparison would at least need to involve some degree of IO. Does go maybe automatically parallelize everythinh it can? That would be one potential answer to the initial question
- SmooL 3y agoYeah this isn't using goroutines at all? I don't see how this is a good comparison of goroutines vs coroutines.
- cyberax 3y agoBut that's the point I'm trying to show. In Go code all calls are the same. There is no sync/async distinction. In contrast, in C# (or any other similar system) async calls are _expensive_ compared to regular function calls.
- mftb 3y agoYou've misunderstood how go routines work. You need to put the "go" keyword before the function call in order for it to be run concurrently.
- cyberax 3y agoI know perfectly well how goroutines work. I'm NOT trying to show that Go is faster than async/await or anything similar. I'm showing that nested async/await calls are incredibly expensive compared to regular nested function calls.
- foton2097 3y agoYou need to add to go keyword to change a normal function to a goroutine. If you would remove async/await and Task/Return from the C# code example, it would perform pretty much the same as Go. If you want to show that async/await calls are expensive, than you should have shown two code samples of C#, one with async/await, and one without. Or could have done the same for Go, show one example with goroutines, and one without. But I think everyone already know that async/await and goroutines has it's costs. The problem is more that you are comparing Go without goroutines (without it's allocation costs) to a C# example with a poor implementation of async/await.
- deleted 3y ago[deleted]
- yencabulator 3y agoJust like there's no `spawn` or such in the async code. It's trying to show the overhead of the async machinery, compared to "uncolored" unfunctions.
- kevincox 3y agoOn the contrary I think it is critical to compare them. You can then decide if the tradeoff is worth it for your use case. Even at a million tasks Go is still under 3 GiB of memory. So that is roughly 3KiB of memory overhead per task. That is likely negligible if your tasks are doing anything significant and most people don't need to worry about this number of tasks. So this comparison shows that in many cases it is worth paying that price to avoid function colouring. But of course there are still some use cases where the price isn't worth it.
- tester756 3y ago>num += Calculate(20).Result; You're sure you should use .Result here instead of awaiting it?
- uncheckederror 3y agoTo expand upon this thought, here is the AsyncGuidance doc[1] on why not to use .Result to get the return value of a completed Task in C#. To make this simple they introduced async Main[2] a few years ago. [1]: https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md#asynchrony-is-viral https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/b... [2]: https://github.com/dotnet/csharplang/blob/main/proposals/csharp-7.1/async-main.md https://github.com/dotnet/csharplang/blob/main/proposals/csh...
- pkolaczk 3y agoI don't know how C#, but Rust async/await doesn't allocate anything on the heap by itself. So it is not a universal property of all async/await implementations contrary to what you imply in your blog post.
- evntdrvn 3y agoFor C#, you might want to try a different version with ValueTask instead of Task. It’s more memory-friendly. It would also be interesting to try Native AOT compiling both versions…
- noveltyaccount 3y ago+1 to Native AOT, I'd love to see the data. Pretty easy to do, add a line of XML to the csproj and a modified `dotnet publish` command. https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/?tabs=net7#publish-native-aot-using-the-cli https://learn.microsoft.com/en-us/dotnet/core/deploying/nati...
- neonsunset 3y agoNativeAOT will have negligible impact on memory usage or performance here. JIT performance on average is higher than NAOT.
- noveltyaccount 3y agoYeah and I think NAOT still embeds the same(?) runtime GC into the native binary. So, for memory usage, I would expect it to be nearly/exactly the same.
- cyberax 3y agoJust tried it. No difference whatsoever.
- ygra 3y agoI may remember wrongly here, but wasn't ValueTask merely the go-to option when it's expected that the task would finish synchronously most of the time? I think for the really async case with a state machine you just end up boxing the ValueTask and ending up with pretty much the same allocations as Task, just in a slightly different way.
- za3faran 3y agoOne thing I wanted to add is that in golang, you end up passing context.Context to all asynchronous functions to handle cancellations and timeouts, so you “color” them regardless. Java folks with structured concurrency have the right idea here.
- metaltyphoon 3y agoI don’t get how people that mentions “no color” don't get this: context.Context IS color.
- SmooL 3y agoIt's not _quite_ the same: you can't call async code from a sync context (hence the color issue), but I can always pass a "context.Background()" or such as a context value if I don't already have one.
- TheDong 3y ago> you can't call async code from a sync context (hence the color issue), but I can always pass a "context.Background()" or such as a context value if I don't already have one. You can always pass context.Background, in this metaphor creating a new tree of color. You can always call "runtime.block_on(async_handle)", in this metaphor also creating a new tree of color.
- throwy2342 3y agoGo doesn't have async/sync distinction, no keywords so the metaphor doesn't hold.
- pkolaczk 3y agoYou can always pass the async executor to the sync code and spawn async coroutines into it. And you can keep it in a global context as well to avoid parameters. E.g. there is `Handle::current()` for exactly this purpose in Tokio. Function coloring is just a theoretical disadvantage - people like to bring it up in discussions, but it almost never matters in practice, and it is even not universally considered a bad thing. I actually like to see in the signature if a function I'm calling into can suspend for an arbitrary amount of time.
- mav88 3y agoYou haven't written a concurrent Go program. Not sure what you're trying to demonstrate.
- cyberax 3y agoYes. Because that's the point. I'm comparing overhead of async calls vs. normal calls.
- schemescape 3y agoNote that you’re changing multiple variables, namely the entire compiler and runtime.
- cyberax 3y agoIf you know a system that both has async/await _and_ full-blown green threads, then I can try to test it.
- michaelsbradley 3y agoRacket has full-blown green threads (virtual threads)[1] that serve as an alternative to the async/await paradigm. It also supports use of native (OS) threads[2]. [1] https://docs.racket-lang.org/reference/eval-model.html#%28part._thread-model%29 https://docs.racket-lang.org/reference/eval-model.html#%28pa... [2] https://docs.racket-lang.org/reference/places.html https://docs.racket-lang.org/reference/places.html [&] https://docs.racket-lang.org/reference/futures.html https://docs.racket-lang.org/reference/futures.html
- gpderetta 3y agoC++ has now both async (built-in) and multiple flavors of stackful coroutines (as external libraries). You can run both on top of ASIO so that you can measure purely difference of the coroutine implementation as opposed to the runtime.
- ilyt 3y agoYour benchmark for Go runs in single goroutine
- cyberax 3y agoYes. On purpose.
- schemescape 3y agoIsn’t the first example completely synchronous? If that’s what’s being compared, why not use C# for both programs?
- EGreg 3y agoWith async/await (which I think C# also uses) you actually unwind the entire stack and start a new one every time you do async callbacks. With fibers / green threads / whatever you instead store stacks in memory. It took me a while to figure this out, thanks to articles I came across (such as “what color is your function?”. There are memory / speed trade-offs, as per usual. If you have a lot of memory, and can keep all the stacks in memory, then go ahead and do that. It will save on all the frivolous construction / destruction of objects. Having said that, my own experience suggests that when you have a startup, you should just build single-threaded applications that clean up after every request (such as with PHP) and spawn many of them. They will share database pool connections etc. but for the most part it will keep your app safer than if they all shared the same objects and process. The benchmarks say that PHP-FPM is only 50% slower than Swoole for instance. So why bother Starting safe beats a 2x speed boost. And by the way, you should be building distributed systems. There is no reason why some client would have 1 trillion rows in a database, unless the client themselves is a giant centralized platform. Each client should be isolated and have their own database, etc. You can have messaging between clients. If you think this is too hard, just use https://github.com/Qbix https://github.com/Qbix it does it for you out of the box. Even the AI is being run locally this way too.
- paulddraper 3y agoIt's absolutely fair to compare them. Just like comparing C vs Python performance. "B...b...but Python is easier to write." Sure. That is a trade off. And whether the trade off makes sense depends on the situation.
- kccqzy 3y ago> You don't have to "color" your code into async and blocking functions Unpopular opinion: this is a good idea. It encourages you to structure your code in a way such that computation and I/O are separated completely. The slight tedium of refactoring your functions into a different color encourages good upfront design to achieve this separation.
- armitron 3y agoGood upfront design? This stinks of implementation detail leakage affecting high-level design, which should be a cardinal sin. One should design based on constraints that best match the problem at hand, not some ossified principle turned "universal" that only really exists to mask lower-level deficiencies.
- slt2021 3y agoconcurrent algorithm is a design choice and is an architecture. Not an implementation detail
- kccqzy 3y agoWhen the implementation detail involves whether or not the function will perform I/O, it is better to let that leak. Excessive hiding of implementation detail is what leads to things like fetching a collection of user IDs from a database and then fetching each user from an ID separately (the 1+N problem). Excessive hiding of implementation detail is what leads to accidental O(N^2) algorithms. Excessive hiding of implementation detail is what leads to most performance problems.
- pkolaczk 3y ago> When the implementation detail involves whether or not the function will perform I/O, it is better to let that leak. I guess Haskell is fully based around that idea :)
- gpderetta 3y agoBut async does not help with that. As shown elsethread, in most languages you can block from async functions just fine.
- whateveracct 3y agoThe author doesn't have the understanding to have even gotten close to the capability for nuance you're asking for. They just copied code from ChatGPT and ran it and timed it and made graphs and it somehow got on HN.
- brightball 3y agoErlang / Elixir processes use about 0.5kb per process, a preemptive scheduler on the BEAM to make sure work is consistent and isolated heap space for each one to avoid stop the world garbage collection. It’s as good as it gets for this type of thing as it was designed for it from the ground up.
- tonyarkles 3y agoAnd it looks like the SystemLimitError issue they encountered is one flag away: https://elixirforum.com/t/too-many-processes-error/22224/2 https://elixirforum.com/t/too-many-processes-error/22224/2
- anonymousDan 3y agoThis was the first thing that occurred to me when I saw the error - should have been pretty straightforward to tweak the settings to give a clearer picture.
- pkolaczk 3y agoFixed already.
- tonyarkles 3y agoThanks! Looked back at the results and that's about what I'd expect. Erlang/Elixir isn't a silver bullet, but amazing in its own way for other reasons. Ultimate max performance and memory efficiency definitely isn't one of them :).
- masklinn 3y ago> Erlang / Elixir processes use about 0.5kb per process Last I checked it used about 2.5K. Beam reserves around 300 words per process (depends on exact options), but a word is 8 bytes not 8. You can get it lower (obviously at a cost as soon as you start using the process and need space) but nowhere near 512 bytes, just the process overhead is around 800.
- substation13 3y agoFunction coloring is orthogonal to the underlying mechanism. You can think of the Java / Go approach as adding the bind points automatically.
- austinshea 3y agoCyberax, I missed my opportunity to reply to your comment, but I really appreciate it, and I wanted to find a way to get this back to you. The comment in question: "Well, I was one of the engineers that made the change :) I'm not sure how much I can tell, but the public reason was: "to make pricing more predictable". Basically, one of the problems was customers who just set the spot price to 10x of the nominal price and leave the bids unattended. This was usually fine, when the price was 0.2x of the nominal price. But sometimes EC2 instance capacity crunches happened, and these high bids actually started competing with each other. As a result, customers could easily get 100 _times_ higher bill than they expected." There was more to it than that, but I figure that's a good enough reference point. Thank you for these improvements. It doesn't change anything, in terms of how much savings I can get by following the latest generations and exotic instance-types, but it does help with the reliability of my workloads. It's been a huge benefit to me, personally, that I can provide some code that enables the potentiality of servers dying, with the benefit of 80% cost savings without using RIs.
- cyberax 3y agoThank you! It means a lot! You can also try another product of my former team: https://aws.amazon.com/savingsplans/ https://aws.amazon.com/savingsplans/ - it's similar to RI, but cheaper because it doesn't provide an ironclad guarantee that the instance will be available at all times. It's still a bit more expensive than spot, but not by much.