Why is move operation invoking memcpy here?

When I tried this code at https://rust.godbolt.org/z/J4r4xQ, it shows that the compiler inserts a call to memcpy here. Why doesn't it reuse the memory, since the incoming struct is going to be dropped anyway?

pub struct Foo {
    pub hello: [usize; 1024],
}

pub struct Bar {
    pub hello: [usize; 1024],
}

pub fn foobar(f: Foo) -> Bar {
    Bar {
        hello: f.hello,
    }
}

If you look at test, you will see a memset, and no copies. The memcpy in foobar is because it need to do some copying around to maintain it's ABI, moving stuff from the inputs to the output slot, but this can be trivially optimized away as seen in test.

4 Likes

Note that (essentially) all moves are emitted as memcpys. It's up to LLVM to remove them.

3 Likes