Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

optimization - Can I efficiently return an object by value in Rust?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, by all means, you should write

fn initialize() -> Vec<Vec<MyStruct>> { ... }

(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:

#[inline(never)]
fn initialize() -> Vec<i32> {
    Vec::new()
}

fn main() {
    let v = initialize();
}

If you compile this program in release mode on the playground, outputting assembly, among everything else you will see this:

playground::initialize:
    movq    $4, (%rdi)
    xorps   %xmm0, %xmm0
    movups  %xmm0, 8(%rdi)
    retq

Vec::new() was inlined, but you can see the idea - the address for the fresh Vec instance is passed into the function in %rdi, and the function stores Vec fields directly into this memory, avoiding unnecessary copying through the stack. This is how it is called:

playground::main:
    subq    $24, %rsp
    movq    %rsp, %rdi
    callq   playground::initialize

You can see that eventually the Vec instance will be put directly into the stack memory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...