4 ms·
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;
by steveklabnik 7y ago
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.
- spinningslate 7y agoThanks. 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.
- steveklabnik 7y agoWhile 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=ph&test=plaintext 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.
- di4na 7y agoAs 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.
- steveklabnik 7y agoYep, 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.
- adwn 7y ago> 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.
- spinningslate 7y ago> 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-are-processes-in-erlang-more-efficient-than-os-threads https://stackoverflow.com/questions/2708033/technically-why-...
- Manishearth 7y agoWe 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.
- dboreham 7y ago
- couchand 7y agoI'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.
- steveklabnik 7y agodo 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!
- couchand 7y agoMy 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?
- atombender 7y agoI'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.
- Buttons840 7y agoThose 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.
- atombender 7y agoThe semantics are clear and have already stabilized. The discussions the last year have almost entirely been about syntax.
- steveklabnik 7y agoYes, 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.