Skip to content

Inline generators before state machine conversion in order to reduce branching #83101

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

Open
pcwalton opened this issue Mar 14, 2021 · 0 comments
Labels
A-coroutines Area: Coroutines A-mir-opt Area: MIR optimizations C-enhancement Category: An issue proposing an enhancement or a PR with one. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-slow Issue: Problems and improvements with respect to performance of generated code.

Comments

@pcwalton
Copy link
Contributor

As of 6c51ec9, which is the fix for #76181, generators no longer inline in MIR. This results in missing the following optimization opportunity:

pub async fn f() {
    foo().await;
    g().await;
}

async fn g() {
    bar().await;
    h().await;
}

async fn h() {
    baz();
}

should become:

async fn f() {
    foo().await;
    bar().await;
    baz().await;
}

I checked a simple example and LLVM didn't seem to do the inlining, which doesn't surprise me as after the state machine transformation it's going to be quite hard to perform it.

This could be an important optimization because recreating the nested call stack when a generator is resumed can be O(n) in bad cases like this one. (This was brought up as a potential problem when comparing C++ coroutines with Rust generators.)

@pcwalton pcwalton added I-slow Issue: Problems and improvements with respect to performance of generated code. C-enhancement Category: An issue proposing an enhancement or a PR with one. A-coroutines Area: Coroutines A-mir-opt Area: MIR optimizations labels Mar 14, 2021
@workingjubilee workingjubilee added the C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such label Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coroutines Area: Coroutines A-mir-opt Area: MIR optimizations C-enhancement Category: An issue proposing an enhancement or a PR with one. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-slow Issue: Problems and improvements with respect to performance of generated code.
Projects
None yet
Development

No branches or pull requests

2 participants