Do moves in rust always copy the stack allocation?

75 Views Asked by At

Lets say I have a large, non-Copy, stack allocated data structure, and then I move it a bunch of times:

struct Foo(u8);

fn main() {
    // A big (8MiB) stack allocation
    let big_stack_data: [Foo; 0x800000] = gen_data();

    let a = big_stack_data; // moves into a
    let b = a; // moves into b
    let c = b; // moves into c
    let d = c; // moves into d
    let e = d; // moves into e
    let f = e; // moves into f
    let g = f; // moves into g
    let h = g; // moves into h
    let i = h; // moves into i
}

Will this actually copy the 8MiB of data each time the array value is moved? Will the compiler actually try to allocate 80 MiB for main's stack?

It seems to me that it doesn't have to. It seems to me that since each move invalidates the source variable, all this memory allocation and copying cost is not necessary and the compiler could simply use the same allocation for each new variable and not actually copy anything. Is this not correct?

0

There are 0 best solutions below