News for hackers. Stuff that matters.
Re:Async-await on stable Rust by mamcx on Thursday November 07, 2019 @10:45AM (#21474169)
I also think async (the paradigm) is kind of weird in rust world. I agree with https://journal.stuffwithstuff.com/2015/02/01/what-color-is-....
Re:Async-await on stable Rust by pcwalton on Thursday November 07, 2019 @11:31AM (#21474654)
The solution suggested by that article is to use M:N threading, which was tried in Rust and turned out to be slower than plain old 1:1 threading.

If you don't want to deal with async functions, then you can use threads! That's what they're there for. On Linux they're quite fast. Async is for when you need more performance than what 1:1 or M:N threading can provide.

Re:Async-await on stable Rust by mamcx on Thursday November 07, 2019 @11:51AM (#21474841)
> If you don't want to deal with async functions, then you can use threads!

Truly? If some very popular lib become async (like actix, request, that I use), I can TRULY ignore it and not split my world in async/sync?

Re:Async-await on stable Rust by pcwalton on Thursday November 07, 2019 @12:05PM (#21474968)
You can easily convert async to sync by just blocking on the result. The other way (sync to async) is more difficult and requires proxying out to a thread pool, but it's also doable.
Re:Async-await on stable Rust by BatmanAoD on Monday November 11, 2019 @02:00PM (#21507718)
An `async` function _can_ call blocking functions, of course, it just blocks the entire thread of execution which could otherwise continue making progress by polling another future.
Re:Async-await on stable Rust by nemothekid on Thursday November 07, 2019 @12:25PM (#21475148)
Both actix and reqwest are already async, the fact that you haven't noticed yet just shows that you are already ignoring it.
Re:Async-await on stable Rust by mamcx on Thursday November 07, 2019 @12:56PM (#21475491)
But the signatures are not. If them mark the types/functions async, what happened?

I must rewrite all the calls?

Re:Async-await on stable Rust by pas on Thursday November 07, 2019 @01:19PM (#21475747)
Async is just a syntactic sugar for Future<...>, you can poll() it manually.

You can take the event loop, run it on one thread and do whatever you wish on other threads.

Fearless concurrency, after all.

Re:Async-await on stable Rust by bluejekyll on Thursday November 07, 2019 @01:53PM (#21476107)
you can just `.await` on any Future returned directly by APIs, if it's not marked as an async fn.
Re:Async-await on stable Rust by tomp on Friday November 08, 2019 @04:10AM (#21480863)
How hard was it tried?

I imagine there's a reason that languages like Go adopted M:N threading... obviously part of the reason is that it's way more scaleable, but userspace threading is also supposed to be faster, as context switches don't need to switch to kernel space... were the problems tight loops (which AFAIK is also the problem in Go)? or maybe it's just so much easier / more efficient if you also have GC...

Re:Async-await on stable Rust by dunkelheit on Friday November 08, 2019 @07:36AM (#21481663)
In my opinion they tried hard enough. Here are the links to the discussions of that time if you are into that kind of thing:

https://mail.mozilla.org/pipermail/rust-dev/2013-November/00...

https://mail.mozilla.org/pipermail/rust-dev/2013-November/00...

https://github.com/rust-lang/rfcs/blob/master/text/0230-remo...

It is clear that they really wanted to make green threading work, but in the end it proved incompatible with other goals of the language.

The main problem as I understand it is with the stack: you can't make the stack big from the beginning (or your threads wouldn't be lightweight anymore) so you need to grow it dynamically. If you grow it by adding new segments to the linked list, you get the "stack thrashing"/"hot split" problem: if you are unlucky and have to allocate/deallocate a segment in a tight loop the performance suffers horribly. Go solved the problem by switching to contiguous relocatable stacks: if there is not enough space in the stack, a bigger contiguous chunk of memory is allocated and old contents of the stack are copied there (a-la C++ vector). Now there is a problem with references to the stack-allocated variables - they become invalid. In go this problem is solvable because it is a managed garbage-collected language so they can simply rewrite all the references to point to the new locations but in rust it is infeasible.

Re:Async-await on stable Rust by the_why_of_y on Friday November 08, 2019 @05:53AM (#21481210)
The reason is that M:N threading inherently requires a language runtime, and the advantage of increased scalability (GHC threads use less than 1KB of memory) comes with the disadvantage that FFI calls are really quite expensive to do, because you need to move to a separate native thread in case the FFI call blocks.

These languages (Erlang, Haskell, Go, ...) have no ambition to be useful for system programming; they're not intended as a replacement for C/C++ in that domain, unlike Rust.

Re:Async-await on stable Rust by danShumway on Thursday November 07, 2019 @01:03PM (#21475573)
Author's solution is threads:

> But if you have threads (green- or OS-level), you don’t need to do that. You can just suspend the entire thread and hop straight back to the OS or event loop without having to return from all of those functions.

Correct me if I'm wrong, but wasn't the lack of threads one of the biggest reasons why NodeJS originally outperformed most of its competitors?

Spinning up threads for each concurrent request was expensive, and (nonblocking) async code was by comparison ridiculously cheap, so the lack of overhead meant Node could just make everything async, instead of trying to decide up-front which tasks deserved which resources.

Granted, it's been over a decade since Node came out. Maybe thread overhead has gotten a lot better? But barring faulty memory, I definitely remember a number of people explaining to me back then that being single-threaded was the point.

Re:Async-await on stable Rust by IfOnlyYouKnew on Friday November 08, 2019 @07:22AM (#21481587)
I'm pretty sure that was the official talking point at the time, and some people may have even been motivated enough to actually belief it.

Of course that all changed once Node got threads.

Re:Async-await on stable Rust by BatmanAoD on Monday November 11, 2019 @02:07PM (#21507795)
Did it, though? AFAIK Node still doesn't have OS threads, which are the expensive version. The "About" page still says that Node is "designed without threads": https://nodejs.org/en/about/
Re:Async-await on stable Rust by bryanlarsen on Thursday November 07, 2019 @11:20AM (#21474540)
All 5 of his points seem to be 2015 Javascript only. Some of them don't even apply to modern Javascript; I don't see any that apply to rust.
Re:Async-await on stable Rust by vanderZwan on Friday November 08, 2019 @07:05AM (#21481519)
While I'm happy to believe you, with a short comment like that I just have to take your word for it. Could give a short explanation of how Rust already addresses each of these points for those of us who don't program in it?
Re:Async-await on stable Rust by littlestymaar on Friday November 08, 2019 @09:28AM (#21482426)
This article is 90% a rant against “callback hell” that js was facing before async/await was introduced. The remaining 10% stays valid even in the presence of async/await, but the trade-off (ignored by the article) of the alternative is having to manually deal with synchronization primitives (at least channels), which would make zero sense given that JavaScript is a single-threaded environment.

Rust is a different beast, you can have whichever model you like best (OS threads, M:N threads (with third party libs), async/await) but async/await is by far the most powerful, that's why it's such a big deal that it lands on Rust stable.