Can Rust optimise away the bit-wise copy during move of an object someday?

Given that in Rust (unlike C or C++) the address of a value is not considered to matter, there is nothing in terms of language that prevents the elision of the copy.

However, today rustc does not optimize anything: all optimizations are delegated to LLVM, and it seems you have hit a limitation of the LLVM optimizer here (it’s unclear whether this limitation is due to LLVM being close to C’s semantics or is just an omission).

So, there are two avenues of improving code generation for this:

  • teaching LLVM to perform this optimization (if possible)
  • teaching rustc to perform this optimization (optimization passes are coming to rustc now that it has MIR)

but for now you might simply want to avoid such large objects from being allocated on the stack, you can Box it for example.

Leave a Comment