I would like to initialize a large object with a function. Currently I have:
fn initialize(mydata: &mut Vec<Vec<MyStruct>>) { /* ... */ }
I would prefer to have:
fn initialize() -> Vec<Vec<MyStruct>> { /* ... */ }
I've heard that C++ often implements return value optimization (RVO), if you are lucky and have a good compiler. Can we disable copying here and have it returned by a hidden pointer that is passed into the function? Is RVO part of the language or an optional optimization?
Yes, by all means, you should write
(By the way, a
Vec
is not that large - it's only 3 pointer-sized integers)Rust has RVO, and this is advertised in guides. You can see it yourself with this code:
If you compile this program in release mode on the playground, outputting assembly, among everything else you will see this:
Vec::new()
was inlined, but you can see the idea - the address for the freshVec
instance is passed into the function in%rdi
, and the function storesVec
fields directly into this memory, avoiding unnecessary copying through the stack. This is how it is called:You can see that eventually the
Vec
instance will be put directly into the stack memory.