Skip to content

Trivial wrapper Generators waste space relative to wrapped Generators #114064

Not planned
@anderspapitto

Description

@anderspapitto
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

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jul 25, 2023
saethlin

saethlin commented on Jul 25, 2023

@saethlin
Member

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

removed
C-bugCategory: This is a bug.
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jul 26, 2023
anderspapitto

anderspapitto commented on Jul 26, 2023

@anderspapitto
ContributorAuthor

yeah looks like the same issue

saethlin

saethlin commented on Jul 26, 2023

@saethlin
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @anderspapitto@saethlin@rustbot

        Issue actions

          Trivial wrapper Generators waste space relative to wrapped Generators · Issue #114064 · rust-lang/rust