20 ms·
Zero-cost futures in Rust
- jaytaylor 10y agoTLDR; I’ve claimed a few times that our futures library provides a zero-cost abstraction, in that it compiles to something very close to the state machine code you’d write by hand. To make that a bit more concrete: - None of the future combinators impose any allocation. When we do things like chain uses of and_then, not only are we not allocating, we are in fact building up a big enum that represents the state machine. (There is one allocation needed per “task”, which usually works out to one per connection.) - When an event arrives, only one dynamic dispatch is required. - There are essentially no imposed synchronization costs; if you want to associate data that lives on your event loop and access it in a single-threaded way from futures, we give you the tools to do so. This sounds quite badass and awesome. I'm not sure what other language implementations take this approach, but this is clearly an extremely beautiful, powerful, and novel (to me at least!) concept. Before reading this, I thought rust was great. This takes it to the next level, though.
- crudbug 10y agoGenerating state machine out of async fluent APIs was enlightening. How are are handling errors ? - One Error state with the context ?
- hawkice 10y agoThis is probably better answered by how Rust handles errors generally: https://doc.rust-lang.org/book/error-handling.html https://doc.rust-lang.org/book/error-handling.html Suffice it to say, similar magic tricks have already been at work to make handling errors not soak up a lot of space, and it'd work well with something like this, I think.
- crudbug 10y agoGenerating state machine out of async fluent APIs was enlightening. How are are handling errors ? - One Error state with the context ?
- merb 10y agothat sounds great but I guess that will make the compilation time bigger.
- pcwalton 10y agoCompared to what?
- merb 10y agocompared to a non free rust future
- deleted 10y ago[deleted]
- Manishearth 10y agoYeah, using closures in zero-cost abstractions (like iterator or now future adaptors) does make compilation slow. LLVM isn't used to this kind of code, and takes time optimizing it. However, now that we have MIR we can optimize this on the Rust side first, before the type info has been lost. This was one of the reasons MIR was added :)
- deleted 10y ago[deleted]
- dbaupp 10y agoI'm... curious about why you think LLVM isn't used to the sort of code that results from these sort of zero-cost abstractions: C++ code is also very aggressive about templates that inline away to nothing, and modern C++ especially has many similar abstractions to Rust (cf. closures, and the range proposal). Rust isn't special in that particular regard. Maybe you mean LLVM is very low-level and thus it sees everything in the abstractions, which obviously takes time to sort through (and is especially wasteful to do multiple times for different monomorphisations). Thus, using MIR to optimise at a higher level can simplify code more efficiently before it hits LLVM.
- rcthompson 10y agoAnd in practice: > ...we’ve added a comparison of minihttp against a directly-coded state machine version in Rust. The two are within 0.3% of each other.
- bascule 10y agoWhile the benchmarks are looking a lot better than many other similar Rust libraries in this space, I'm not sure the code is in a state where they're actually meaningful yet: https://github.com/alexcrichton/futures-rs/blob/master/futures-minihttp/src/response.rs#L30-L35 https://github.com/alexcrichton/futures-rs/blob/master/futur...
- steveklabnik 10y agoI agree that good benchmarks are important. This is the Techempower benchmark, which only requires this, and is how it's implemented in most languages for Techempower. See https://github.com/TechEmpower/FrameworkBenchmarks/blob/180d44e1064abc6fe8c703b05e065c0564e6ee05/frameworks/Java/rapidoid/src/main/java/hello/SimpleHttpProtocol.java#L37-L77 https://github.com/TechEmpower/FrameworkBenchmarks/blob/180d... for example. Extra tests for more complex stuff would be great.
- soulbadguy 10y agoFinally a nice async/io interface for rust, always felt that it was a big missing piece, couple of questions for peps familiar with async in other languages : 1 - Isn't the state machine approach the same as C#/.net async/await is using ? But the with the added convenience of the syntactic sugar ? 2 - no allocation : , does'nt the lambda closure need to be allocated somewhere ? 3 - I would have love some comparison (booth performance wise and on theory) with C++ up comming coroutine work, from my understand the C++ approach is even more efficient in term of context switching and have the advantage of even less allocation.
- steveklabnik 10y ago1. I am not sure exactly how async/await is implemented, but I believe it is very similar. Some people are also working on implementing similar sugar in Rust, but it's not done yet. 2. Closures are on the stack, not the heap, by default, in Rust. If you don't see a Box, they're not heap allocated. 3. I agree!
- moosingin3space 10y agoStack-allocated closures are one of my favorite features of Rust. They make it easy to write functional-style code that performs well.
- indeyets 10y ago(1) here's how it's done in JS: https://tc39.github.io/ecmascript-asyncawait/#desugaring https://tc39.github.io/ecmascript-asyncawait/#desugaring
- pcwalton 10y ago> Isn't the state machine approach the same as C#/.net async/await is using ? But the with the added convenience of the syntactic sugar ? Similar in principle, but the implementation is different. Tasks in C# are more of an OO style instead of a FP style where they turn into an enum (sum type, if you want to get theoretical). > 2 - no allocation : , does'nt the lambda closure need to be allocated somewhere ? No, not in Rust (or in C++). > from my understand the C++ approach is even more efficient in term of context switching and have the advantage of even less allocation. No, you're asserting this without understanding Rust. It's not possible to get less than zero allocation.
- vvanders 10y agoNot sure if I missed this in the post, does this depend on any unstabilized features or can we use this today on 1.10.0 stable? Awesome stuff btw, love the iterator inspiration.
- pcwalton 10y agoI think "impl Trait" is unstable.
- steveklabnik 10y agoIt is, however, it's not currently actually using that syntax in the code: http://alexcrichton.com/futures-rs/futures/trait.Future.html http://alexcrichton.com/futures-rs/futures/trait.Future.html So, this will work on stable. It was presented that way to not get into the whole details about the whole impl trait shenanigans in general.
- pcwalton 10y agoAh, great news!
- eddyb 10y agoNot even in nightly yet - follow https://github.com/rust-lang/rust/pull/35091 https://github.com/rust-lang/rust/pull/35091 for updates (we're trying to land it this week).
- alexcrichton 10y agoYou can indeed use this on stable Rust today! Right now 1.9.0 is the minimum supported version due to the usage of `catch_panic` in a few places. I'd recommend a beta compiler for now though to compile some of the examples. There's a bug in the stable compiler which causes them to take up to 8x longer to compile, but beta/nightly are both speedy!
- vvanders 10y agoThat's so awesome. I was hoping that was the case. Serious Kudos, it feels like a lot of the promises of Rust are really paying off here.
- skybrian 10y agoIs there any special handling for Futures that complete with an error? Also, how do you debug code that's hung or taking too long? It might be useful to get a list of all the jobs (incomplete Futures) that are currently running, much like running 'ps'.
- Rusky 10y agoBecause Rust doesn't do exceptions the way other languages with futures do, there's no special handling. Just use a future representing a Result, like you would for synchronous errors.
- aturon 10y agoYes -- the blog post didn't go into details about this, but Futures in general have an error type as well, and all the combinators know how to propagate errors correctly. (There's also a notion of "cancellation" for a future -- we'll get into this with later posts). In terms of debugging, there's not infrastructure currently, but the kind of thing you're talking about should be easy to add!
- KirinDave 10y agoI've used async tooling extensively on several platforms now, including Go, C#, Java, Clojure's core async, the new async methods in JS. The challenge of these state machine codegen abstractions is not performance. It's making source-facing debuggers do the right thing and handle errors in a way that doesn't break the illusion that the code running is the code in source. If you can match the degree of support C# has for error handling, you'll be in an amazing place.
- skybrian 10y agoIt would be useful to give a Future a name (for debugging) in the code that creates it, so a list of "pending jobs" is more readable. I don't think any other language is doing that. Like other kinds of debugging info, you might also want a way to strip out the job names in a production build.
- shmerl 10y agoSo will this become the official part of the language / standard library?
- steveklabnik 10y agoThat, if it happens, is still fairly far in the future. This isn't the only work on this space in Rust (see https://medium.com/@carllerche/announcing-tokio-df6bb4ddb34 https://medium.com/@carllerche/announcing-tokio-df6bb4ddb34 for example, which also uses this library), so it's still in an early phase. Once everyone has actually used things and found it satisfactory, then such things can be discussed.
- AndyKelley 10y agoThis is huge. This allows one to express concurrency in a natural way not prone to the typical errors of problems of this nature, with no runtime overhead, while competing with C in terms of the constraints of the runtime. Big kudos to Aaron Turon and Alex Crichton. You guys knocked it out of the park.
- tracker1 10y agoAgreed, and very cool... I do think that async/await syntax is very nice for reducing a certain amount of nesting bloat. Even with chaining, it doesn't look as nice imho, though better than deeply nesting callbacks.
- alexnewman 10y agoCheckout https://github.com/tokio-rs/tokio https://github.com/tokio-rs/tokio. It builds on top of future-rs
- losvedir 10y agoI dabbled with rust in the past and was really fascinated with it, but haven't played around lately. One thing caught my eye in the post: fn get_row(id: i32) -> impl Future<Item = Row>; That return type looks odd to me. What does it mean to return an "impl", and is that a new feature in rust, or just something advanced that I missed in my exploration before?
- steveklabnik 10y agoThat is new syntax which has been accepted as a language change, but has not actually landed in the compiler yet. It was shown this way because it's much easier to understand, but is the same thing as it would be in the real code. https://github.com/rust-lang/rfcs/blob/master/text/1522-conservative-impl-trait.md https://github.com/rust-lang/rfcs/blob/master/text/1522-cons... is an explanation of the feature in detail, but it boils down to "I will return you some kind of thing that implements the Future trait, but I'm not telling you exactly what it is."
- michaelsbradley 10y agoDoes the need for this arise, in some sense, because Rust doesn't offer classical inheritance and so no mechanism to indicate co/contra variance?
- aturon 10y agoNo, it's more related to the fact that Rust allows you to use traits in both a statically- and dynamically-dispatched way. You can read some more on the topic here: http://aturon.github.io/blog/2015/09/28/impl-trait/ http://aturon.github.io/blog/2015/09/28/impl-trait/
- Gankro 10y agoRust does let you communicate variance (it's very important for lifetimes). Variance is determined based on a type's composition, and PhantomData can be used to specify variance that doesn't immediately follow by providing an "example" of what it should behave like. The only limitation of this approach is that you can't override the compiler's reasoning -- if it sees evidence for covariance, and you provide more evidence for contravariance it will just give up and pick invariance (which is of course strictly correct if both pieces of evidence are accurate). Also I guess you can't have bivariance but like... bivariance is so bad. All of Rust's standard collection and smaht pointer types are written in pure Rust and have relatively complex variance behaviour.
- tomdale 10y agoThe recent flurry of activity around async IO in Rust has been really exciting; to me, it indicates that the core team's decision to stabilize the language was a smart bet that is paying off in rapid ecosystem growth. One quibble I have with this post is that it talks about futures as a zero-cost abstraction. That might be true (or close to true) from a performance perspective, but in my (admittedly inexperienced) opinion, it seems to have a significant ergonomic cost that is not accounted for. While futures help us deal with multi-threaded coordination of data from multiple sources, that overhead isn't necessary for situations where you're running in a single thread dedicated to doing IO operations. Dealing with futures in your code is not non-trivial. Browsing through the futures version of the HTTP server, I had a hard time following along: https://github.com/alexcrichton/futures-rs/blob/master/futures-minihttp/src/lib.rs#L92-L133 https://github.com/alexcrichton/futures-rs/blob/master/futur... And it requires a bunch of helper code to go with it: https://github.com/alexcrichton/futures-rs/blob/master/futures-minihttp/src/io2.rs https://github.com/alexcrichton/futures-rs/blob/master/futur... The blog post mentions Tokio, another high-level abstraction on top of mio (by the same author). Because it doesn't require the futures abstraction from top-to-bottom, it offers similar (maybe even a little better) performance with what, to my eyes, is far simpler code: https://github.com/tokio-rs/tokio-minihttp/blob/master/src/lib.rs#L21-L44 https://github.com/tokio-rs/tokio-minihttp/blob/master/src/l... I'm still learning Rust and spend most of my time in JavaScript. The analogy I'd use is: imagine if in the Node programming model, every API required you to use JS Promises, even at the very lowest level. Even if you could reduce the cost of creating new Promise objects, interacting with them over simple values could make the code you write more verbose. In Rust, that problem is exacerbated by the much stricter type system and the fact that you have to do cross-thread coordination. I'm a total beginner to systems programming, and a lot of this stuff is above my pay grade. However this shakes out in the community, I'm very happy to see Rust on the way to becoming the fastest, most productive way to write high-performance web services.
- aturon 10y agoThanks for the thoughtful reply! I'm a little confused about the snippet you're pointing out. It's not actually using futures at all! In fact, that code is just part of setting up threads for the server. The reason it's more complicated than the version in Tokio is that minihttp supports multiple event loop threads (which gives some performance benefits), and this code is handling that setup. At a broader level, Tokio's services -- the main thing users write -- are based on futures, in exactly the same way as minihttp. So for people writing actual servers, the ergonomics should be the same. Now, stepping back, it's definitely true that ergonomics are a cost to be aware of, and it's one we've thought carefully about in the design of futures. We've had a lot of experience and success in Rust with iterators, which share a lot of the same API design philosophy. That said, I do anticipate that over time we'll want to layer sugar on top of futures. As I mention a couple times in the blog post, async/await (or something like it) is the obvious way to do it, and there's plenty of prior art. But I think we should walk before we run -- let's make sure we've got the core abstraction right, and then we can sprinkle some sugar where it's needed.
- the_mitsuhiko 10y agoWohoo. I was waiting for this. I hope that at a later point this will also mean that we get some sort of syntax support for it once it's stable and entered std.
- kibwen 10y agoWhat sort of syntax support do you have in mind? (Personally I think any language-level support is unlikely, given that Rust doesn't make a point of privileging any particular approach to concurrency.)
- pcwalton 10y ago> Personally I think any language-level support is unlikely I don't. :) Futures are a very generic abstraction that doesn't really favor any approach any more than, say, iterators do.
- the_mitsuhiko 10y agoSee Python 3.5, ES6 and C# and their await stuff. They generally use a form of future internally to abstract this.
- kibwen 10y agoAh yes, I wasn't thinking of async/await. I suppose there's enough precedence from other languages to warrant that, though this is probably far-future work (I don't even expect futures to land in the stdlib for quite a while, though I would like them to eventually).
- natrius 10y agoWhat would a rough sketch of async/await syntax sugar look like implemented with Rust macros?
- steveklabnik 10y agoOne vision of that https://github.com/erickt/stateful/blob/master/examples/generator.rs https://github.com/erickt/stateful/blob/master/examples/gene...
- Manishearth 10y agoI'm rather surprised by the benchmark; I would expect the Go benchmark to be faster than Java (and the fact that it isn't may indicate some improvements that can be done to fasthttp by learning from rapidoid or minihttp). Then again, the difference isn't that much, so it just could be implementation details that would require a total refactor to fix.
- trishume 10y agoWhy would you expect that? AFAIK the Go compiler has a worse optimizer, a worse GC (at least throughput-wise) and possibly also a worse threading runtime. Since the benchmark measures steady-state performance and not memory or latency, and with the best Java server (probably optimized to almost never allocate) then Java having better code generation and threading could give it that much of an edge.
- Manishearth 10y agoGo GCs fewer things, though. At least in my experience -- this isn't a hard fact :) To me, Go feels like C-but-some-things-are-GCd, which Java is not. The explicit sharing annotations in Go means that it's super easy for it to not put most of the things on the GC heap, and it's super easy for me, the programmer, to see what's not being put on the GC heap. Whenever I've debugged Go code this been validated. But yes, it's not as clear cut. I ignored the threading runtime being a factor, and ignored that Java has a better GC (though it GCs more things). I also ignored JIT as steve pointed out to me in irc. So yeah, there could be language reasons behind the benchmark order too. Worth investigating though, I'm sure the folks using fasthttp would love a perf boost :)
- zzzcpan 10y agoActually, optimizer, GC and a threading runtime have almost nothing to do with Go's net/http performing that badly. The reasons are mostly in very poor design choices in the library itself that forced them to do a lot of implicit synchronization, unnecessary memory copying, unnecessary system calls, etc. It's just a huge mess, avoid it whenever possible.
- michaelmior 10y agoCurious if someone has tried this and Eventual[0] with any thoughts on how they compare. https://github.com/carllerche/eventual https://github.com/carllerche/eventual
- alexcrichton 10y agoThe futures crate is intended to be the successor to eventual, the author of which, Carl, helped us with some key insights in the futures crate as well.
- michaelmior 10y agoCool! Thanks for the reply :) It's nice to see some collaboration as opposed to trying to decide between competing crates.
- ridiculous_fish 10y agoHow does the zero-cost abstraction work? Say we make a Future<Int> and then chain `.map(|x| x+1)` on a dynamic number of times (N). Presumably this requires storing at least N function pointers. How can we store these N function pointers with zero cost? If it only takes one allocation, where does the N-1 future store its function pointers?
- Gankro 10y agoEach chain produces a different static type. If you want to do a dynamic amount of chains (which seems strange to me? Got an example?) you would need to allocate and use dynamic dispatch, yes. Basically `MyFuture.map(x)` => `Map<MyFuture, X>` `MyFuture.map(x).map(y)` => `Map<Map<MyFuture, X>, Y>` This is obviously disgusting to expose to users, which is one of the reasons this post uses the `impl Trait` syntax to cover it up and say "well it's something with the right interface, the compiler knows it (so no need to do dynamic dispatch), don't worry about it".
- ridiculous_fish 10y agoThanks for your answer. Here's a more realistic example. Say we have a file.close() function that returns a Future<Void>, indicating when the close is complete. Now we want to make a Future for closing a list of files: let fut = Future<void>::new(); let v = vec![file, file2, file3]; for file in v.into_iter() { fut = fut.and_then(file.close()); } Is this possible with this API, or would the assignment to `fut` break because we now have a different type? With traditional Futures I'd expect this to look like a sort of linked-list of closures (definitely lots of allocation). What does it end up looking like under the hood with zero-cost Rust futures?
- tyoverby 10y agoYou are correct in assuming that the assignment would be a type error. Similar to other languages, you'd have to allocate. In rust, it would probably look like: let mut fut: Box<Future<()>> = Box::new(Future<()>::new()); and then fut = Box::new(fut.and_then(|| file.close()));
- rdtsc 10y agoAs mentioned in the post, given Rust wants to operate in the same space as C, this approach makes sense. However from a higher level, building more complex concurrent systems, dealing with futures/deferred-s/promises and/or a central select/epoll/kqueue reactor loop gets daunting and doesn't mix with complex business rules. Deferred based approach has been a round for many years. I experienced it by using Twisted (Python framework) for 5 or so years. And early on it was great However when we switched to using green threads, the logic and amount of code was greatly simplified. So wondering if Rust provides any ability to add that kind of an N:M threading approach. Perhaps via an extension, macro or some other mechanism. Note that in C, it being C such things can be done with some low level trickery. Here is a library that attempt that: http://libmill.org/ http://libmill.org/ And there were a few others before, but none have taken off enough to become mainstream.
- steveklabnik 10y agoYes, this is the lowest layer of the story. For actually writing an app, writing something higher-level makes sense: consider the relationship between raw futures and Finagle, for example. You have to build out the lower layers before you can add the upper ones, though. Rust doesn't natively supply anything like green threads, as they have too heavy of requirements. But there are librarys that can implement them, since Rust is so low-level: https://crates.io/crates/mioco https://crates.io/crates/mioco as an example.
- davidgrenier 10y agoIn other words futures are not as composable as one would hope. John Reppy's CML seems like a much better toolbox which gets composability without pretension. Vesa Karvonen (whom I understand has worked on the MLTon compiler) has offered an excellent delivery in Hopac for C# and F# complete with a slew of combinators: https://github.com/Hopac/Hopac https://github.com/Hopac/Hopac I'm not aware of anyone offering an alternative superior to an informal CSP yet which seems to be the reason why Go and Clojure have picked it as well for their concurrency model. See David Nolen's excellent blog posts on the matter: http://swannodette.github.io/2013/07/12/communicating-sequential-processes http://swannodette.github.io/2013/07/12/communicating-sequen... http://swannodette.github.io/2013/08/17/comparative http://swannodette.github.io/2013/08/17/comparative
- ufo 10y agoUnfortunately, it seems that you still need to use callbacks and lots of and_thens to write this async code. Wouldn't it be possible to add coroutines to Rust instead?
- pbarnes_1 10y agoThis is awesome, but I have an off-topic rust question: Why can't we have some syntactic sugar to get rid of .unwrap()?
- steveklabnik 10y agoFor Result, in nightly, there is sugar, though it's for try!, not unwrap(), which serve very similar purposes. It's not yet clear if said sugar will support Option; that's one of the reasons it's not stable yet. Feel free to post off-topic questions on https://users.rust-lang.org/ https://users.rust-lang.org/ any time!
- jdub 10y agoCheck out the try! macro. https://doc.rust-lang.org/book/error-handling.html#the-try-macro https://doc.rust-lang.org/book/error-handling.html#the-try-m...
- whateveracct 10y agoInstead of looking for "syntactic sugar to get rid of unwrap()" I think the better option would be to prove that the Option isn't None. In my experience in other functional languages with Options, if you can't prove it, you're usually making a mistake somewhere upstream in your code.
- bvinc 10y agoThe try! macro should be used much more often than .unwrap(). And there is a feature that has been implemented to make a ? operator that is roughly equivalent to the try macro. It's currently hidden behind a feature gate named question_mark and it's unclear if or when it will be part of the language.
- hackaflocka 10y agoWhat's the meaning of "zero cost future" in this context? I googled the phrase and got a bunch of irrelevant material.
- fbernier 10y agoI think it means that it has no performance hit versus handwriting your own threading.
- steveklabnik 10y ago> C++ implementations obey the zero-overhead principle: What > you don’t use, you don’t pay for [Stroustrup, 1994]. And > further: What you do use, you couldn’t hand code any better. > > – Stroustrup So, in this context, the idea is that if you hand-rolled your own state machine, you should see no difference than using this library. And, we measured: the overhead in a benchmark comparing the two was 0.3%, that's three tenths of one percent.
- jholman 10y agoThe other concept that GP needs to understand the title is https://en.wikipedia.org/wiki/Futures_and_promises https://en.wikipedia.org/wiki/Futures_and_promises So the title/TFA is about "futures" (in the sense of my link) that have "zero cost" (in the sense of steveklabnik's comment).
- bfrog 10y agoI love the direction this is going, and the performance it achieves. Debugging promises/deferreds in other languages has given me nightmares, compare with erlang/golang debugging where you get a simple stacktrace. Does this provide some nice way of debugging complex future chains? Are there plans towards making it super easy to debug? Cheers!
- inglor 10y agoNative promises in JavaScript suffered from debugging issues - but there are hooks that can help you with those - like unhandledRejecton and the like. Debugging promises in JavaScript is very easy at the moment.
- kbenson 10y ago> a simple TCP echo server; How convenient. I've been exploring/learning Rust, and writing a simple echo server and comparing it to a reference version I've written in Perl is my first semi-trivial program I wanted to do to compare.
- saynsedit 10y agoBig downside is now you will have a dichotomy of functions that block using futures and functions that block at the OS level and no sane way to intermix them. Rust essentially becomes two languages. Async/await sugar doesn't fix this. Would be great if functions could be written in a general way for both IO models and users could select the implementation at their convenience.
- steveklabnik 10y ago> no sane way to intermix them. My understanding is, the idea is to put the blocking stuff in a threadpool with https://github.com/alexcrichton/futures-rs/tree/master/futures-cpupool https://github.com/alexcrichton/futures-rs/tree/master/futur...
- saynsedit 10y agoRight, there may be a lot of back and forth marshaling between using thread pools and not depending on whether the library you're using is futures based or not. Maybe you use one library that is futures-based and one that isn't. Maybe the library you use is mostly non-blocking except for one use of sleep() or another esotorically blocking call. It's just annoying and prone to error. Most people may not even be aware of the subtly blocking nature of the code they use in their futures-based project. This is what I mean by having two different languages. Libraries written for one aren't always/simply compatible with the other. You'll have a growing community of nominally "futures-based" rust libraries too. This is why people use Go or Erlang. It just removes the need to have to think about this. Not saying they are generally better than Rust, and some Rust people may even like having a futures-based sublanguage, but I suspect most programmers will be loathe to have to deal with the extra mental tax.
- steveklabnik 10y agoThat's true. But caring about the details is the bread and butter of the kinds of programs that Rust is targetted at. If you can get away with the reduced performance, abstracting away those details is a totally reasonable choice. Rust is not and never can be a programming language for all programmers, and that's super okay.
- crudbug 10y agoOne thing I have not seen in discussion is - Work vs. Worker abstraction. Your application work - computation logic/business rules, should be decoupled from the type of worker. The worker can be - blocking or non-blocking - Futures/Continuations/Co-routines.
- thomasahle 10y agoI'm confused by .map(|row| { json::encode(row) }) .map(|val| some_new_value(val)) Over .map(json::encode) .map(some_new_value) Is the explicit extra layer of lambda generally prefered in Rust over just passing the functions?
- steveklabnik 10y agoIt's just a style thing, some people prefer one way, some another. I personally prefer the latter. They compile to the exact same thing.
- thomasahle 10y agoThe makes sense. Do you know if Rust already has an idiomatic correct style?
- steveklabnik 10y agoI don't think that there's ever been an explicit discussion about it. I'm not even sure how many people know the latter is possible, to be honest, it's a bit harder to learn about.
- digi_owl 10y agoI wonder if it is information used by the compiler to do error checks. Keep in mind that one of the goals of Rust is to catch common errors in C like languages at compile time rather than run time.
- tatterdemalion 10y agoNo. If there were a type error with either form, the compiler will catch it.
- inglor 10y agoIt makes the blog post more approachable for users. I have seen the exlicit style being used over the point-free style many times before since it seems like it's easier for newbies to grasp - I haven't seen any evidence to support it though.
- leovonl 10y agoIn my opinion - as someone with some background in CS - the name "future" is a little too overloaded here. It is not only used for the deferred computation of a value so much as it also means the composition of computations. This is not wrong per se, but calling the result a "future" alone oversimplifies what's happening below and hides some properties about the combinations. The first observation one can make - which is not mentioned anywhere in the article - is that the composition of futures here can be understood as a monadic composition. This by itself gives a big hint why this interface is so powerful. Second is that this library could be understood as an implementation of process and process combination from pi-calculus [1] - sequential combination, joining, selection, etc - so it could be formalized using its process algebra. From the practical side, one example of a mature library that implements similar concepts is the LWT [2] library for OCaml, which has the same idea of deferred computation, joining and sequencing, but calls the computations "lightweight threads". One could also argue about naming in this case, but it seem to reflect a better the idea of independent "processes" that are combined on the same address space. Finally, as much as these concepts of futures and processes look similar on the surface, they each have their own properties - so it's always good to consider what better fits the model. By looking at the research and at other similar solutions, one can make more informed choices and have a better idea of what to expect from the implementation. [1] http://www.cs.cmu.edu/~wing/publications/Wing02a.pdf http://www.cs.cmu.edu/~wing/publications/Wing02a.pdf [2] http://ocsigen.org/lwt/manual/ http://ocsigen.org/lwt/manual/
- mike_hock 10y agoI suppose you could say, this way of programming is the future.
- infogulch 10y agoget off my lawn this isn't reddit grumble grumble doesn't add to the conversation blah blah
- hinkley 10y agoBack when futures and promises were a new concept to most people, if someone asked me to explain why you would want to do such a thing, my favorite example was loading images in a web browser. You wouldn't want to load the same image four times just because it appears in four places on a page, would you? Yada yada promises etc etc. Seeing articles like this makes me feel like a circle has finally been closed.
- matthewaveryusa 10y agoI'm genuinely interested in knowing what the problem is with an event loop using epoll and a threadpool for IO that blocks but epoll can't poll. I've used proprietary event loops at 2 giant companies, libuv with C, asio with cpp and nodes async, and the async IO was never the problem in terms of performance or complexity. What is the problem that's trying to be solved?
- sanxiyn 10y agoThe problem being solved is better UX (in this case, developer experience). It is an event loop using epoll and a threadpool, just in a more palatable (and composable) interface, without much overhead.
- Animats 10y agoThis is cute. This is clever. Whether or not it's too clever time will tell. A year ago, I noted that Rust was starting out at roughly the cruft level C++ took 20 years to reach. Rust is now well beyond that. All this "futures" stuff strongly favors the main path over any other paths. You can't loop, retry, or easily branch on an error, other than bailing out. It's really a weird syntax for describing a limited type of state machine. I'm not saying it's good or bad, but it seems a bit tortured.
- steveklabnik 10y agoWhat's your definition of "cruft," and where does it appear in Rust? I will be the first to admit that Rust is not perfect, but that adjective does not come to mind. I am of course incredibly biased.
- vvanders 10y agoCruft? Coming from C++ I find it quite the opposite.
- pcwalton 10y ago> All this "futures" stuff strongly favors the main path over any other paths. You can't loop, retry, or easily branch on an error, other than bailing out. You can do all of that.
- Animats 10y agoOK, make an HTTP request, and if it fails, wait 2 seconds and retry. After 10 times, give up.
- pcwalton 10y agoTimeouts are covered here. http://alexcrichton.com/futures-rs/futures/index.html#examples http://alexcrichton.com/futures-rs/futures/index.html#exampl... As for repeating something multiple times, you would either repeat the chain 10 times or write your own future combinator. Probably writing a custom future combinator would make the most sense here; actually, such a thing ought to be built-in to futures.rs.
- meneses 10y agoAawesome. So to cancel a future, I just drop it! Awesome.
- lossolo 10y agoWhy you didn't compare it to C++ or C ? If you want to compete with C/C++ it would be natural to compare those in benchmarks. Java and Go have GC. It's like comparing super car with street cars when you should compare it to other super cars.
- Manishearth 10y agoBecause they're benchmarking the implementation of async IO, and showing the results against libraries that folks use. This is not a common pattern in C/C++, so the libraries there are fewer and less people have used them. But, if you want C++, check the data from the original benchmark which was linked: https://www.techempower.com/benchmarks/#section=data-r12&hw=peak&test=plaintext https://www.techempower.com/benchmarks/#section=data-r12&hw=... . The Java one is slightly faster than the C++ one (more like "just as fast", since the difference is tiny). And there are 7 more java libs (half of them with recognizable names) before the next one (which I've never heard of), which underlines my point about this being more common in Java than C/++. The benchmark wasn't shown to prove that Rust is the fastest language in the universe. It was shown to prove that the Rust futures implementation is competitive with the others in use today, ones which people would recognize.
- eggnet 10y agoHow are futures handled for open() and disk i/o?
- steveklabnik 10y agoNothing special yet; those are still implemented in a blocking way, and so should be put into a threadpool.
- eggnet 10y agoA threadpool with futures support, I gather. Very nice.
- steveklabnik 10y agohttps://github.com/alexcrichton/futures-rs/tree/master/futures-cpupool https://github.com/alexcrichton/futures-rs/tree/master/futur...
- plesner 10y agoThis looks really impressive. I'm curious what the story is around propagating errors through chains of futures. Traditionally future libraries don't pay much attention to that which can make debugging excruciating, which it doesn't have to be. But then rust does errors differently so maybe it's less of an issue there? About the naming though, I was a little disappointed. Out of future, deferred, and promise, "promise" is the better term. The two others imply that something will happen later which is misleading because it's fine to have promises stick around long after they're fulfilled.
- nv-vn 10y agoAnyone else find the f.select(g)/f.join(g) syntax unintuitive/awkward? I'm confused as to why they wouldn't go with the (IMO) more logical select(f, g) and join(f, g) in this case (since neither Future is really the "subject" in these cases). Not that this is a major concern (it would take only a few lines of code to change within your own program using an alias for the functions), just interested in knowing the rationale behind the choice.
- samnardoni 10y agoYou can write it both ways. t.method() is the same as T::method(t).
- ben0x539 10y agoI guess it's cool that Rust is getting zero-cost futures, but they have a long way to go to catch up to C++'s negative-overhead coroutines!
- deleted 10y ago[deleted]
- deleted 10y ago[deleted]
- cyber1 10y agoLittle benchmark rs-futures vs lwan (https://lwan.ws https://lwan.ws) on my machine Core i5 futures-minihttp(singlethread): $ wrk -c 100 -t 2 -d 20 http://127.0.0.1:8080/plaintext Running 20s test @ http://127.0.0.1:8080/plaintext 2 threads and 100 connections Thread Stats Avg Stdev Max +/- Stdev Latency 823.09us 449.37us 20.98ms 98.69% Req/Sec 62.15k 10.51k 105.24k 48.63% 2479035 requests in 20.10s, 340.44MB read Requests/sec: 123335.77 Transfer/sec: 16.94MB lwan(singlethread): $ wrk -c 100 -t 2 -d 20 http://127.0.0.1:8080/ Running 20s test @ http://127.0.0.1:8080/ 2 threads and 100 connections Thread Stats Avg Stdev Max +/- Stdev Latency 596.45us 573.31us 24.46ms 99.33% Req/Sec 86.17k 13.15k 119.71k 76.00% 3429720 requests in 20.01s, 624.73MB read Requests/sec: 171404.15 Transfer/sec: 31.22MB For lwan i use http server example from lwan.ws main page. As you can see in this example C http server much faster than simple http Rust server. * futures-minihttp release build * lwan -O3
- cyber1 10y agoBut for me, this difference is nothing, because on Rust we can write safe and fast close to metal code. I think Rust is the greatest modern system programming language.
- killercup 10y agoOn Reddit [1], Alex mentioned that the single-thread case was not optimized (yet). What do the numbers for multi-threading look like? [1]: http://reddit.com/r/rust/comments/4x8jqt/zerocost_futures_in_rust/d6dm0f5 http://reddit.com/r/rust/comments/4x8jqt/zerocost_futures_in...
- cyber1 10y agofutures-minihttp(2 threads): $ wrk -c 100 -t 2 -d 20 http://127.0.0.1:8080/plaintext Running 20s test @ http://127.0.0.1:8080/plaintext 2 threads and 100 connections Thread Stats Avg Stdev Max +/- Stdev Latency 633.87us 808.22us 24.66ms 98.41% Req/Sec 86.72k 4.84k 94.90k 85.75% 3452383 requests in 20.01s, 424.73MB read Requests/sec: 172546.21 Transfer/sec: 21.23MB lwan(2 threads): $ wrk -c 100 -t 2 -d 20 http://127.0.0.1:8080/ Running 20s test @ http://127.0.0.1:8080/ 2 threads and 100 connections Thread Stats Avg Stdev Max +/- Stdev Latency 375.26us 618.54us 20.60ms 98.21% Req/Sec 116.82k 5.56k 124.07k 92.25% 4647906 requests in 20.00s, 846.62MB read Requests/sec: 232339.90 Transfer/sec: 42.32MB
- cm3 10y agoThis is cool and validates Rust, but I just want to add that even 2kb stacks as mentioned in sibling comments is bigger than Erlang's process stacks. In Erlang 19.0.3, even with dirty-schedulers enabled, a process's default size is 338 words.
- b34r 10y agoselect is an odd term choice for what is essentially a race condition. What's the thought behind the naming of that method?
- steveklabnik 10y agohttp://man7.org/linux/man-pages/man2/select.2.html http://man7.org/linux/man-pages/man2/select.2.html
- soulbadguy 10y agoFor those who are curious about how does that fair against a coroutine based approach : https://www.youtube.com/watch?v=_fu0gx-xseY https://www.youtube.com/watch?v=_fu0gx-xseY