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

Trivial wrapper Generators waste space relative to wrapped Generators #114064

Closed
anderspapitto opened this issue Jul 25, 2023 · 3 comments
Closed

Comments

@anderspapitto
Copy link
Contributor

I'm unclear if this is properly a bug or feature request - it's a missed optimization opportunity that IMO should be guaranteed.

I tried this code: (playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021)

#![feature(generators)]
#![feature(generator_trait)]

use core::pin::Pin;
use std::ops::Generator;
use std::ops::GeneratorState;
use std::mem;


pub fn mk_foo(x: bool) -> impl Generator {
    move || {
        if x {
            yield;
        }
    }
}

pub fn mk_bar(x: bool) -> impl Generator {
    let mut f = mk_foo(x);
    static move || {
        loop {
            match unsafe { Pin::new_unchecked(&mut f) }.resume(()) {
                GeneratorState::Yielded(_) => yield,
                GeneratorState::Complete(_) => return,
            }
        }
    }
}

pub fn mk_baz(x: bool) -> impl Generator {
    static move || {
        let mut f = mk_foo(x);
        loop {
            match unsafe { Pin::new_unchecked(&mut f) }.resume(()) {
                GeneratorState::Yielded(_) => yield,
                GeneratorState::Complete(_) => return,
            }
        }
    }
}

fn main() {
    let foo = mk_foo(true);
    let bar = mk_bar(true);
    let baz = mk_baz(true);
    println!(
        "sizes {} {} {}",
        mem::size_of_val(&foo),
        mem::size_of_val(&bar),
        mem::size_of_val(&baz),
    );
}

I expected to see this happen: sizes of foo, bar, baz should all be identical, because foo and baz simply forward to the underlying foo - they don't have any additional states. This makes the generator abstraction less zero-cost than it should be.

Instead, this happened: sizes are 2, 3 and 4 respectively.

Meta

rustc 1.73.0-nightly (8771282 2023-07-23)
binary: rustc
commit-hash: 8771282
commit-date: 2023-07-23
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5

@anderspapitto anderspapitto added the C-bug Category: This is a bug. label Jul 25, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 25, 2023
@saethlin
Copy link
Member

saethlin commented Jul 25, 2023

I think you are looking for #108590 and the issue linked in it

@saethlin saethlin removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 26, 2023
@anderspapitto
Copy link
Contributor Author

anderspapitto commented Jul 26, 2023

yeah looks like the same issue

@saethlin
Copy link
Member

Thanks for confirming :)
I'll close this as a duplicate then

@saethlin saethlin closed this as not planned Won't fix, can't repro, duplicate, stale Jul 26, 2023
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

3 participants