Generators are too big · Issue #52924 · rust-lang/rust (original) (raw)
Currently, generators won't ever reuse storage slots, causing them to take up more space than is necessary:
#![feature(generators)]
fn main() {
let a = || {
{
let x: i32 = 5;
yield;
println!("{:?}", x);
}
{
let x: i32 = 5;
yield;
println!("{:?}", x);
}
};
println!("{}", std::mem::size_of_val(&a)); // prints "12", could print "8" (4 for state, 4 for x
)
}
Finding optimal solutions seems like a difficult packing problem, but it should be easy to do quite a bit better.
Also see the example case in #59087.