我可以在 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>>) { /* ... */ }
我更喜欢:
fn initialize() -> Vec<Vec<MyStruct>> { /* ... */ }
我听说 C++ 经常实现返回值优化 (RVO),如果你很幸运并且有一个好的编译器.我们可以在这里禁用复制并让它由传递给函数的隐藏指针返回吗?RVO 是语言的一部分还是可选的优化?
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
fn initialize() -> Vec<Vec<MyStruct>> { ... }
(顺便说一句,Vec
并没有那么大——它只有 3 个指针大小的整数)
(By the way, a Vec
is not that large - it's only 3 pointer-sized integers)
Rust 有 RVO,这个 在指南中做了广告.您可以使用以下代码自行查看:
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()
被内联了,但你可以看到这个想法 - 新的 Vec
实例的地址被传递到 % 的函数中rdi
,并且该函数将Vec
字段直接存储到此内存中,避免通过堆栈进行不必要的复制.它是这样叫的:
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
可以看到最终Vec
实例会被直接放入栈内存中.
You can see that eventually the Vec
instance will be put directly into the stack memory.
这篇关于我可以在 Rust 中按值有效地返回一个对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!