actix-web v2.0 released
Features use of std futures and async-await across the actix ecosystem.
I spent hours replacing complicated chained futures with simple, intuitive async-await and can testify that this new paradigm is a game changer. No more Either. No more folding. Far fewer clones. Lots of borrowing.
Much, much better experience and hours of experience using combinators that I may never touch again.
Posted Dec 25, 2019, 6:55 PM UTC
I understand only actix-web moved to async-await? Any plans to move actix itself to it?
I'm really not sure how actix is supposed to move over until we have async fn (and therefore GATs) in traits.
The boxing cost of the async-trait workaround crate would most likely be way too high, an Actor model framework would cross trait boundaries way too often for that to reasonably work.
If you want to use futures in actix you already need to box them.
impl Traitdoesn't work for methods for the same reason async doesn't.Can you explain what you think is too high, and why it is the case? Actor systems in general are far from fast. They often involve heap allocation and boxing of message objects, enqueuing them into a remote actors mailbox, scheduling the actor, executing the code there (with dynamic/table driven dispatch on a message type) and then going all the way back to deliver a response if necessary. Actix might or might not need all of those steps (I'm not familiar with it). But even if it would be super efficient for an actor framework, it would be doubtful that the cost of one or a couple more trait object calls is important. Keep in mind that trait objects and dynamic dispatch in general are still super efficient on modern CPUs. The main cost that is sometimes referred to is that trait objects prevent certain optimizations (e.g. inlining) - which may or may not be important in a given piece of code.
So how to deal with databases in actix-web? Wait until all is async is not feasible for me.
Use
web::block. I think this example should show it.I’ve been using that for my current project. I am about to try https://github.com/mehcode/actix-diesel?files=1 this to see if it makes it any better.
Slower than a sync method?
That's hard to answer, because everything is a sync method - There is no such thing as a natural async method 😀 Async methods work by calling multiple synchronous methods.
Based on the assumption that you mean thread-blocking synchronous methods the performance will still depend on the actual use-case. It might or might not be faster than a e.g. using an asynchronous message passing system.
executing a sync function is just as fast as executing an async function. The problem async solves is that it allows us to do other things while waiting for certain I/O syscalls to complete - in the slow portions, we're not really running the function, we're waiting for the operating system to wake up our thread.
The added overhead of Box<dyn Future> is in the creation and disposal of the future, not the execution.
I wasn't trying to imply that async was somehow a 'faster' method. I'm also aware of what Box<dyn Future> implies performance-wise; the RFC covers it well https://github.com/rust-lang/rfcs/pull/1603
I mean, for Actix, a web service library, I expect async to be a significant speedup. For a method that would be async (because it makes sense) but the choice is dynamic dispatch or sync, I expect dynamic dispatch to be the right call (again, with the assumption being that the underlying method is capable of async, especially for a web framework).
Of course it's use case dependent.
Yep. "most likely be way too high" is a strong claim.
IIUC, the actix 0.9 is on futures 0.3, which makes it compatible.
I don't think that's enough though, is it? The traits you have to implement for Actors use regular sync functions, because async fn isn't supported yet in traits.
So I was specifically thinking of the future returned by sending a message to an actor. And that's an await compatible 0.3 future now.
So it works for my use case, which is having an actor be in charge of a data structure, and communicating with it via messages. I was abke to upgrade to actix web 2, and use tbe new await syntax when interacting with actors (I have no need for it within actor implementations)
Regular futures have a limitation - they don't allow you to reborrow the actor. actix worked around this by introducing
ActorFuture, which have not moved to async-await yet.async-await has the same problem of keeping the actor borrowed across
.awaitpoints. Solving this - whatever the solution would be - is going to require some design. Have the maintainers decided already on how they want to do this?