简体   繁体   English

我可以在Rust中按值有效地返回对象吗?

[英]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. 我听说C ++通常会实现返回值优化(RVO),如果您幸运的话,并且具有良好的编译器。 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? RVO是语言的一部分还是可选的优化?

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) (顺便说一下, Vec并不是那么大-它只有3个指针大小的整数)

Rust has RVO, and this is advertised in guides . Rust具有RVO,这已在guide中公布 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. 内联了Vec::new() ,但是您可以看到这个想法-新鲜的Vec实例的地址传递到%rdi的函数中,并且该函数将Vec字段直接存储到此内存中,从而避免了通过堆栈进行不必要的复制。 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. 您可以看到最终Vec实例将直接放入堆栈内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM