News for hackers. Stuff that matters.
Re:Rust async-await status report 2 by steveklabnik on Wednesday July 10, 2019 @10:01AM (#20402006)
Everything was considered. This has been the most discussed Rust feature to date, but a serious margin.

I'll try to give you some answers from my perspective; I am not on the language team, nor have I done any of this work myself, but this is what I remember, and see from the outside.

The short answer is: it's the only way to accomplish the task that fits into Rust's requirements. The top relevant ones are speed, lack of required runtime for non-async code, and zero-cost C interoperability. It sounds to me like you're coming at this from a "what is the simplest thing for a programmer to use" angle. That's a totally valid angle, but Rust is focused on ease of use only within the context of other requirements. Notably, Erlang's model, while it has excellent properties, falls down on all three of these other requirements. That's totally okay in the context of Erlang, but is not acceptable in the context of Rust.

Re:Rust async-await status report 2 by spinningslate on Wednesday July 10, 2019 @10:17AM (#20402139)
Thanks. I suspected run-time constraints might be a reason so that makes a lot of sense.

> Notably, Erlang's model, while it has excellent properties, falls down on all three of these other requirements.

Being nitpicky, but I'd argue Erlang only falls down on speed where it's computationally bound. Where it's IO bound (e.g. a prototypical chat server) Erlang has excellent speed characteristics - where speed is measured in concurrency and latency.

But I absolutely get that's not the interpretation of "speed" you meant.

Appreciate the explanation.

Re:Rust async-await status report 2 by steveklabnik on Wednesday July 10, 2019 @10:25AM (#20402225)
While it is true that I'm mostly talking about computation speed, that's not the only thing. It is true that Erlang is good at this, but I'd like to see some real numbers here. In benchmarks, this is almost never the case. For example, if you take a look at techempower's plaintext benchmark, which should purely be about IO: https://www.techempower.com/benchmarks/#section=data-r18&hw=...

Phoenix, which is in Elixir, does 2.7% of the requests of the Rust frameworks. Chicago Boss, in Erlang, does 0.2%. Is this a realistic benchmark? I don't know; I can't say if the code for either of these implementation is good or not. But if it's a silly error, I think it would be good for Elixir/Erlang advocates to fix this. Early Rust implementations had extremely poor showings too.

I tend to think of Erlang as being extremely reliable, with acceptable performance, rather than being the fastest IO language in the West.

Re:Rust async-await status report 2 by di4na on Wednesday July 10, 2019 @11:08AM (#20402659)
As an admin of the elixir slack, i have to handle that one a lot.

It is not really realistic at all no. But mostly because the benchmark is not realistic of a production load. That said we should do better.

The main reason it is not fixed is that the few members of the community that tried to fix it had a really bad experience interacting with the repo and getting feedback of impact and simply lost interest. And we have not have people motivated enough so far because they do not really care about the benchmark.

Ofc it matters to people that are not in the community but well...

That said, yes we would never be close to Rust anyway (probably) but we could probably have a far better standard deviation than Rust. Something that tend to matter in production a lot but people forget reading benchmark is the spread of the response time vs the average one.

We should also perform better in a noisy environment like the "cloud" one.

There are hopes that the recent EEF marketing group could help find volunteers or finance someone to fix it.

Re:Rust async-await status report 2 by steveklabnik on Wednesday July 10, 2019 @11:13AM (#20402738)
Yep, that makes perfect sense. I've had good experiences contributing, but it can be tough...

It's also 100% true that benchmarks are not representative of production loads. Sadly, it's also 100% true that programmers make decisions based on benchmarks that aren't representative of production loads. It's a real catch-22 situation.

Re:Rust async-await status report 2 by adwn on Wednesday July 10, 2019 @10:37AM (#20402376)
> Being nitpicky, but I'd argue Erlang only falls down on speed where it's computationally bound. Where it's IO bound (e.g. a prototypical chat server) Erlang has excellent speed characteristics - where speed is measured in concurrency and latency.

At the scale where asynchronous IO becomes interesting, speed is computationally bound. Think maintaining tens of thousands of TCP connections. You can either spawn a single OS thread for each connection, which gets prohibitively expensive with regards to memory consumption, or you can use asynchronous IO and have a couple of native worker threads, approx. 1 per CPU core.

If Erlang can handle, say, tens of thousands of TCP connections with one Erlang thread/actor per connection, then only because it is using an implementation of asynchronous IO in the background.

Re:Rust async-await status report 2 by spinningslate on Wednesday July 10, 2019 @10:50AM (#20402487)
> If Erlang can handle, say, tens of thousands of TCP connections with one Erlang thread/actor per connection, then only because it is using an implementation of asynchronous IO in the background.

Yes, it can. That's one of the BEAM's key strengths (the Erlang VM). It presents a simple, uniform concurrency abstraction - the process - to developers. Note these are application-level processes, not OS level. They're very lightweight and it's possible to spin up tens of thousands on a single machine. The VM multiplexes user processes over the underlying hardware (threads/cores). More here [0].

[0] https://stackoverflow.com/questions/2708033/technically-why-...

Re:Rust async-await status report 2 by Manishearth on Wednesday July 10, 2019 @11:28AM (#20402891)
We used to have M:N threading in Rust's runtime -- this is one other way to achieve what we wanted. It imposed a cost on all programs, including those which weren't using it, and iirc it didn't fit well and wasn't flexible enough (I'm not sure of this) and it got removed.

Async/await/futures with library level scheduling is basically the only way to get the benefits of application threads in rust without such downsides.

Re:Rust async-await status report 2 by dboreham on Wednesday July 10, 2019 @12:07PM (#20403289)
But with other downsides such as making application code less easy to debug and maintain.
Re:Rust async-await status report 2 by skybrian on Wednesday July 10, 2019 @02:31PM (#20404878)
Certainly, but if you're willing to commit to one runtime for your app, there are plenty of other choices: Go, Erlang, Kotlin, Swift, Typescript or Dart might be better bets. Web browsers and mobile phone apps do well with this approach.

Rust (along with C or Zig) has an interesting use case for building large libraries that can be reused across different runtimes via foreign function calls. It's paying some usability for the library maintainer for being able to work in more environments.

To speculate a bit, I expect we will eventually see some interesting new runtime environments built out of the various libraries in the Rust ecosystem. (Along with C of course, which is how runtimes are built now.)

Maybe we will see a trend away from reimplementing all the libraries in whatever the hot new language is, in favor of building on a more persistent set of stable libraries? (Already true today with C, but the Rust ecosystem could evolve into something more cohesive.)

Re:Rust async-await status report 2 by couchand on Wednesday July 10, 2019 @12:59PM (#20403858)
I'd like to hear about the distinction between promises/futures and async/await. The former is absolutely a useful abstraction, requires no special features to implement, and thus is already widespread. The latter adds syntax support for one particular monad, and in the process adds magic that obscures the fundamental operation.

My experience with JS async/await code in the wild is that developers promptly forget (or never learn) that it desugars to a promise, forget all the tools that can be used for managing complex asynchronous code with explicit promises, and then fail to properly account for the asynchronicity of their code.

I would hope that Rust, with a type system and a community focused on engineering skills, would be able to avoid some of these pitfalls, but I'm not so sure about that.

Re:Rust async-await status report 2 by steveklabnik on Wednesday July 10, 2019 @04:52PM (#20406418)
do notation in a language without a GC is an open research question. Some believe it’s impossible.

However, we have people using Rust today that need help. We could ignore these production users and say “sorry, it’s gonna be who knows when”, or we can ship something that solves their problems. Even shipping async/await itself took four years, even though it’s a technique that’s well known!

Re:Rust async-await status report 2 by couchand on Thursday July 11, 2019 @09:38AM (#20411392)
My working theory is that async/await syntax causes more problems than it solves. I've read through the RFC and many of the comments, but I don't see anywhere a cogent argument why the syntax is needed -- that seems to be taken on faith.

I've written and reviewed boatloads of promise-using JS code, a fair amount of promise-using C++ and task-using C#, and some future-using Rust. I've never seen a case where the value added by async/await exceeds the potential for issues.

The goal seems to be make asynchronous code look like synchronous code. In my experience, developers do in fact act like it's synchronous, forgetting the critical fact that it's not. The supposed feature is a massive bug in everyday usage.

EDIT: reading through the RFC and comments, I now realize that async fn is NOT actually being implemented as sugar on top of future-returning functions, but in fact is more like a Python or ES6 generator. There seems to be a disagreement as to whether an async fn is really just a future-returning function: I see the claim made both ways, and haven't found an authoritative source yet. Confusing!

EDIT2: > do notation in a language without a GC is an open research question. Some believe it’s impossible.

I don't understand how GC comes into play. Isn't the do notation simply syntactic sugar for bind sequencing?

Re:Rust async-await status report 2 by steveklabnik on Thursday July 11, 2019 @11:25AM (#20412257)
> I've never seen a case where the value added by async/await exceeds the potential for issues.

There are many cases in which you can write safe code with async/await that you cannot write safely with raw futures. See here: http://aturon.github.io/tech/2018/04/24/async-borrowing/

> I now realize that async fn is NOT actually being implemented as sugar on top of future-returning functions, but in fact is more like a Python or ES6 generator. There seems to be a disagreement as to whether an async fn is really just a future-returning function: I see the claim made both ways, and haven't found an authoritative source yet. Confusing!

The return type of an `async fn` is an `impl Future`. Additionally, you can `.await` on any `Future` inside of an `async fn`. That means it feels like sugar for Futures, which is why people say that. It's a good mental model. However, the current implementation is a generator internally.

> I don't understand how GC comes into play. Isn't the do notation simply syntactic sugar for bind sequencing?

It is in a language with garbage collection. Things get much more dicey when you don't have those things. For example, Rust has three kinds of closures, not one. You have to deal with references and lifetimes. Rust is generally imperative, and so you have to figure out how break, continue, etc, all work. Due to all of these things and others, the various monadic types Rust have don't actually have the same signatures as each other. I believe there are some more issues, but that's what I've got off the top of my head. That doesn't mean that this is actually impossible; it means that it requires more research to even figure out if it is possible.

Rust doesn't have to solve every problem perfectly. There will be other languages in the future. Maybe Rust++ will have this kind of tech.

Re:Rust async-await status report 2 by couchand on Thursday July 11, 2019 @12:31PM (#20412924)
I had not seen that particular example, thanks for providing the citation.

Perhaps I'm just being stubborn here, but allow me a moment. Isn't the core issue there that the executor requires the future to have a lifetime of 'static? Aaron explains it:

> ...because they are tossed onto executors like thread pools and hence not tied to any particular stack frame.

It's surely a huge undertaking, but wouldn't a more nuanced lifetime analysis solve the underlying problem here? I don't think the issue is that any other way to write this example is unsafe, it's that it's running into a limitation of the borrow checker.

What's required of an asynchronous borrow? The same thing required of any borrow: exclusivity. Sure, the async/await lets you write code that "looks synchronous", and gives the lexical cues the compiler needs to enforce the borrow. But does that mean it's the only way? The counterexample in Aaron's article even relies on async/await! That makes me highly suspicious that the need for it was taken as a given and never sufficiently challenged.

The core issue in that example is that the borrow needs to be held until some point in the future that can't truly be determined lexically. That says to me that it's not a borrow in the traditional Rust model. It is absolutely a useful borrow to check for, but why go about it backwards by only allowing such chronological borrows in such narrow circumstances?

Moreover, the example isn't realistic. The model that should be used for reading from a socket is a stream.

> The return type of an `async fn` is an `impl Future`.

Well, it is and it isn't. For instance, it appears that one can't await on a plain function that returns an `impl Future`...

Re:Rust async-await status report 2 by steveklabnik on Thursday July 11, 2019 @12:49PM (#20413111)
> Isn't the core issue there that the executor requires the future to have a lifetime of 'static?

Yes, and no. That is one of the factors. Another, and IMHO, bigger factor is that the lifetime needs to be 'static only as the task starts to execute. This is why Pin, etc, came into existence. This is due to self-referentiality.

> It's surely a huge undertaking, but wouldn't a more nuanced lifetime analysis solve the underlying problem here?

Yes, we're back in the realm of "open research question."

> What's required of an asynchronous borrow? The same thing required of any borrow: exclusivity.

Most borrows are not exclusive.

> That makes me highly suspicious that the need for it was taken as a given and never sufficiently challenged.

With respect, it is just as easy for me to say "This makes me highly suspicious that you have not investigated how all of this works and that's why you think it's easy."

> The core issue in that example is that the borrow needs to be held until some point in the future that can't truly be determined lexically.

Rust's borrows have not been lexical since last year.

That's fine, of course! It's not like getting into this stuff is easy. But the team working on this has a ton of experience; multiple PhDs, etc, etc. This is the most investigated feature of Rust ever. Again, yes, maybe it is possible, but it's not within the realm of a "just".

Additionally, none of any of the stuff in this comment this solves the original issues with do notation.

> it appears that one can't await on a plain function that returns an `impl Future`...

Sure you can: https://play.rust-lang.org/?version=nightly&mode=debug&editi...

Re:Rust async-await status report 2 by couchand on Thursday July 11, 2019 @01:17PM (#20413387)
> the lifetime needs to be 'static only as the task starts to execute.

It would seem the lifetime needs to be coerceable to the lifetime of the executor, but as far as I can tell never really should need to be 'static.

> Most borrows are not exclusive.

Sorry, I meant, exclusive mutability.

> Rust's borrows have not been lexical since last year.

As I understand it, the borrows are not lexical in the classic sense of lasting until the end of the function, but they are still determined completely lexically, correct? Here the async/await syntax is provide lexical cues regarding the sequencing of asynchronous code, which is how it helps the borrow checker, right?

> But the team working on this has a ton of experience; multiple PhDs, etc, etc.

Of course. You've all done a great job producing a solid language! I'm not questioning the technical chops. And I don't think I said "just", I appreciate the scale of the problem. I'm just arguing about a tradeoff that I have not been able to find much discussion of.

It's great to improve the ergonomics of the language. We need more people to want to use it! Is it possible that this is going too far? It sounds like it introduces an awful lot of magic.

Re:Rust async-await status report 2 by steveklabnik on Thursday July 11, 2019 @01:37PM (#20413571)
> I'm not questioning the technical chops.

No worries then; a lot of people have made very similar statements in the past, and that's the place they're coming from.

> I'm just arguing about a tradeoff that I have not been able to find much discussion of.

I am still not 100% sure what exactly it is you're talking about, at this point, to be honest. I think it's "why do we need async/await and not just write manual futures". I've pointed out several things. Is there something else?

> It would seem the lifetime needs to be coerceable to the lifetime of the executor, but as far as I can tell never really should need to be 'static.

Some executors, like Tokio, are mult-threaded with work-stealing. A task (chain of futures) may start executing on one thread, be waiting, and then get moved to another thread before continuing execution.

> the borrows are not lexical in the classic sense of lasting until the end of the function, but they are still determined completely lexically, correct?

I am not totally sure what you mean by "completely lexically" here. It is true that NLL means that borrows can exist for only part of a lexical scope, but not more than a lexical scope. Or rather, the "more than" part is already taken care of by lifetimes anyway.

> Is it possible that this is going too far? It sounds like it introduces an awful lot of magic.

Most programmers don't really mind magic as much as you'd think. I mean, sure, in some sense, it's a lot of magic, but the result is that instead of writing

    use std::future::Future;
    use std::task::{Context, Poll};
    use std::pin::Pin;
    
    struct Five;
    
    impl Future for Five {
        type Output = i32;
        
        fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Self::Output> {
            Poll::Ready(5)        
        }
    }
    
    fn returns_five() -> impl Future<Output=i32> {
        Five 
    }
I write

    async fn returns_five() -> i32 {
        5
    }
And, the intuition that "async fn returns an impl Future that I don't have to implement" is simple enough and accurate enough that it feels like even less magic.

In practice, the people who have been writing tons of networking code don't see it as too much magic; they see it as a significantly nicer way to write the code they already have to write. The ergonomic difference is so large, that the delays in shipping have caused fairly significant community strife and pressure. It is possibly too hyped. Writing manual futures is so, so, so painful.

Re:Rust async-await status report 2 by atombender on Wednesday July 10, 2019 @02:51PM (#20405124)
I'm curious if the team ever considered an inverted approach. I've read through many of the discussions, and don't think I ever saw this suggested. I'm sure there must be a reason not to do it, but my Rust isn't advanced enough to tell.

It seems to me that you'll generally await futures in high-level code. Keeping values around as futures is the minority case. So instead of asking to await one could automatically insert awaiting code whenever a future needs to be converted to a real value. Instead of this:

  async fn process() -> Image {
    await process_image(await fetch("http://example.com/foo.jpg"))
  }
...you could simply have this:

  async fn process() -> Image {
    process_image(fetch("http://example.com/foo.jpg"))
  }
...which would await both the inner and outer futures: process_image() expects an Image, and fetch() returns a Future<Image>, so it can be automatically awaited and coerced.

Sometimes you do want a future when you don't want to wait right away, e.g. when storing a bunch of pending operations or something. For that, one could tell Rust not to automatically coerce futures:

  let pending: HashMap<string, Future<Image>>
  // ...
  async fn process() {
    pending[key] = defer process_image(fetch("http://example.com/foo.jpg"))
  }
...although this, too, could presumably be done transparently for you, since Rust knows you're assigning to a future.
Re:Rust async-await status report 2 by steveklabnik on Wednesday July 10, 2019 @04:53PM (#20406440)
Yes, this was suggested. I don’t remember the exact reason it was rejected, other than maybe being a more unusual choice with no significantly clear benefit.

I think part of it too was that this works better if your futures were to execute immediately like promises; this would make a lot of code serial by default.

Re:Rust async-await status report 2 by Buttons840 on Wednesday July 10, 2019 @03:33PM (#20405603)
Those hardly look different. From the context of the parent comments I thought you were going to suggest something completely different, but it looks like it's just a small syntax change.
Re:Rust async-await status report 2 by atombender on Wednesday July 10, 2019 @03:37PM (#20405652)
The semantics are clear and have already stabilized. The discussions the last year have almost entirely been about syntax.