Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not do heap allocation on asyncronous trait methods #7

Closed
TTWNO opened this issue Jul 27, 2022 · 2 comments · Fixed by #82
Closed

Do not do heap allocation on asyncronous trait methods #7

TTWNO opened this issue Jul 27, 2022 · 2 comments · Fixed by #82
Labels
good first issue Good for newcomers

Comments

@TTWNO
Copy link
Member

TTWNO commented Jul 27, 2022

As far as I understand, currently, async traits require heap allocation and dynamic dispatch on every invocation. The reason why this isn't supported seems to be linked in a blog post.

Eventually, I would like to find a way to have non-heap-allocated asynchronous trait methods. For now, the solution is ok, as our only goal is to be reasonably performant, not to be portable assembler.

"Micro-optimizations are the root of all evil"
—Some Guy Smarter Way Than Me

@mcb2003
Copy link
Contributor

mcb2003 commented Aug 23, 2022

async fn is just syntax sugar, and this could be done by transforming an async function like this:

async fn do_something(x: u32, y: u32) -> u32 {
    let res = return_number().await;
    x + y - res
}

Into this:

use std::future::Future;

fn do_something(x: u32, y: u32) -> impl Future<Output = u32> {
async move {
    let res = return_number().await;
    x + y - res
}
}

However, impl Future<Output = u32> isn't a concrete type, so you can't name it, making this impossible in some cases. For most use cases though, this should work.

Whether this is worth it is up for discussion. As a point of reference, C++ 20 coroutines are always heap-allocated from what I can tell.

@mcb2003 mcb2003 added the good first issue Good for newcomers label Aug 23, 2022
@TTWNO
Copy link
Member Author

TTWNO commented Nov 17, 2022

Should be native to Rust in 1.66/67. See:

https://blog.rust-lang.org/inside-rust/2022/11/17/async-fn-in-trait-nightly.html

@TTWNO TTWNO transferred this issue from odilia-app/odilia Nov 28, 2022
@TTWNO TTWNO linked a pull request Feb 8, 2023 that will close this issue
@TTWNO TTWNO linked a pull request May 14, 2023 that will close this issue
5 tasks
@TTWNO TTWNO closed this as completed in #82 Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
2 participants