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

Possibly optimize simple function body? #240

Open
novacrazy opened this issue Mar 6, 2023 · 0 comments
Open

Possibly optimize simple function body? #240

novacrazy opened this issue Mar 6, 2023 · 0 comments

Comments

@novacrazy
Copy link

I'm working on an autogenerated EventHandler trait with many async fn on_some_event() methods. There is also one async fn fallback() method for which all default implementations of on_some_event call, giving:

#[async_trait::async_trait]
trait EventHandler {
    async fn fallback(self, args: SomeType) -> U;

    // This 30 times or so for default implementations
    async fn on_some_event(self, args: SomeType) -> U {
        self.fallback(args).await
    }
}

This leads to double-boxing the fallback future, which is likely executing more often than not if the library users don't handle all events.

Would it be possible to flag an async method as "transparent" perhaps? Strip the .await and return the result directly.

// ...
#[async_trait(transparent)]
async fn on_some_event(self, args: SomeType) -> U {
    self.fallback(args).await
}
// would desugar to
fn on_some_event<'async_trait>(self, args: SomeType) 
    -> std::pin::Pin<Box<dyn std::future::Future<Output = U> + Send + 'async_trait>> 
where
    Self: Send + 'async_trait
{
    self.fallback(args)
}

However, if the async_trait codegen is stable enough, I could also just desugar it manually for default implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant