TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How Rust optimizes async/await

351 pointsby tmandryalmost 6 years ago

11 comments

weimingalmost 6 years ago
As a newcomer to Rust, wishing that this post was one of the <i>first</i> ones I&#x27;ve read about this topic. It took scouring through many many posts, some of them here on HN, to be able to grasp some of the same idea. (I may not be alone, judging from the very long discussion the other day: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20719095" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20719095</a>)
评论 #20740082 未加载
评论 #20742429 未加载
评论 #20743695 未加载
zackmorrisalmost 6 years ago
This is one of the most concise tutorials on how generators, coroutines and futures&#x2F;promises are related (from first principles) that I&#x27;ve seen.<p>I&#x27;m hopeful that eventually promises and async&#x2F;await fade into history as a fad that turned out to be too unwieldy. I think that lightweight processes with no shared memory, connected by streams (the Erlang&#x2F;Elixer, Go and Actor model) are the way to go. The advantage to using async&#x2F;await seems to be to avoid the dynamic stack allocation of coroutines, which can probably be optimized away anyway. So I don&#x27;t see a strong enough advantage in moving from blocking to nonblocking code. Or to rephrase, I don&#x27;t see the advantage in moving from deterministic to nondeterministic code. I know that all of the edge cases in a promise chain can be handled, but I have yet to see it done well in deployed code. Which makes me think that it&#x27;s probably untenable for the mainstream.<p>So I&#x27;d vote to add generators to the Rust spec in order to make coroutines possible, before I&#x27;d add futures&#x2F;promises and async&#x2F;await. But maybe they are all equivalent, so if we have one, we can make all of them, not sure.
评论 #20741209 未加载
评论 #20741091 未加载
评论 #20745886 未加载
评论 #20742749 未加载
pcwaltonalmost 6 years ago
As a reminder, you don&#x27;t <i>need</i> to use async&#x2F;await to implement socket servers in Rust. You can use threads, and they scale quite well. M:N scheduling was found to be slower than 1:1 scheduling on Linux, which is why Rust doesn&#x27;t use that solution.<p>Async&#x2F;await is a great feature for those who require performance beyond what threads (backed by either 1:1 or M:N implementations) can provide. One of the major reasons behind the superior performance of async&#x2F;await futures relative to threads&#x2F;goroutines&#x2F;etc. is that async&#x2F;await compiles to a state machine in the manner described in this post, so a stack is not needed.
评论 #20742831 未加载
评论 #20742858 未加载
评论 #20743206 未加载
评论 #20739615 未加载
评论 #20742747 未加载
评论 #20744213 未加载
评论 #20739789 未加载
saurikalmost 6 years ago
Does anyone know how Rust&#x27;s implementation compares to C++2a&#x27;s? The C++ people seem to have spent a lot of time creating an extremely generic framework for async&#x2F;await wherein it is easy to change out how the scheduler works (I currently have a trivial work stack, but am going to be moving to something more akin to a deadline scheduler in the near future for a large codebase I am working with, which needs to be able to associate extra prioritization data into the task object, something that is reasonably simple to do with await_transform). I am also under the impression that existing implementation in LLVM already does some of these optimizations that Rust says they will get around to doing (as the C++ people also care a lot about zero-cost).
评论 #20741848 未加载
评论 #20742948 未加载
评论 #20743737 未加载
continuationalalmost 6 years ago
I don&#x27;t quite follow. What exactly is the overhead that other languages have for futures that is eliminated here?
评论 #20739317 未加载
评论 #20739303 未加载
评论 #20740588 未加载
评论 #20740466 未加载
emmanueloga_almost 6 years ago
Related questions: does anybody know:<p>1) which was the first language that introduced the async&#x2F;await keywords? I want to say C#, but I’m not sure.<p>2) are there any papers that describe the code transformation to state machine that is commonly performed to implement these keywords?
评论 #20743393 未加载
评论 #20743644 未加载
fnord77almost 6 years ago
I&#x27;m a big user of Rust, but I&#x27;m kinda dismayed that async IO wasn&#x27;t part of the original design.<p>It&#x27;s nice they&#x27;re making Futures a zero-cost abstraction, but it feels like it is at the expense of ergonomics.
评论 #20741967 未加载
评论 #20741855 未加载
Paul-ishalmost 6 years ago
I think the year is off by 1 in the blog post, or the title needs a 2018 tag.
评论 #20739020 未加载
评论 #20739032 未加载
MuffinFlavoredalmost 6 years ago
How does `yield` work under the hood? Does it add a reference to some array, with code to loop over all the references with some small timeout until the reference status changes from &quot;pending&quot; to &quot;completed&quot;?
评论 #20740140 未加载
评论 #20743563 未加载
strictfpalmost 6 years ago
We&#x27;re getting generators as well? Awesome.
评论 #20739255 未加载
评论 #20739219 未加载
Waltheralmost 6 years ago
Thank you for the great writeup &lt;3 Eagerly waiting for the upcoming parts - please keep it up!