在 Rust 中使用局部函数是否会对性能产生负面影响?

Is there any negative performance implication to using local functions in Rust?

我最近意识到我可以在 Rust 中创建本地函数(函数中的函数)。似乎是清理我的代码而不污染文件的函数 space 的好方法。我下面所说的局部函数与 'external' 函数的意思的小样本:

fn main() {
    fn local_plus(x: i64, y: i64) -> i64 {
        x + y
    }
    let x = 2i64;
    let y = 5i64;

    let local_res = local_plus(x, y);
    let external_res = external_plus(x,y);
    assert_eq!(local_res, external_res);
}

fn external_plus(x: i64, y: i64)  -> i64 {
    x + y
}

我想知道这样做是否会对性能产生负面影响?就像每次包含函数运行时 Rust 重新声明函数或占用一些不需要的函数量 space 一样?或者它实际上没有性能影响?

顺便说一句,欢迎任何关于我如何为自己找到答案的提示(通过阅读任何特定的文档集或我可以使用的工具)。

没有影响;我检查了为两个变体生成的程序集,它是相同的。

我比较的两个版本:

"external":

fn main() {
    let x = 2i64;
    let y = 5i64;

    let external_res = external_plus(x,y);
}

fn external_plus(x: i64, y: i64)  -> i64 {
    x + y
}

"local":

fn main() {
    fn local_plus(x: i64, y: i64) -> i64 {
        x + y
    }
    let x = 2i64;
    let y = 5i64;

    let local_res = local_plus(x, y);
}

并且两者都产生相同的 asm 结果(今天夜间的发布模式):

    .text
    .file   "rust_out.cgu-0.rs"
    .section    .text._ZN8rust_out4main17hb497928495d48c40E,"ax",@progbits
    .p2align    4, 0x90
    .type   _ZN8rust_out4main17hb497928495d48c40E,@function
_ZN8rust_out4main17hb497928495d48c40E:
    .cfi_startproc
    retq
.Lfunc_end0:
    .size   _ZN8rust_out4main17hb497928495d48c40E, .Lfunc_end0-_ZN8rust_out4main17hb497928495d48c40E
    .cfi_endproc

    .section    .text.main,"ax",@progbits
    .globl  main
    .p2align    4, 0x90
    .type   main,@function
main:
    .cfi_startproc
    movq    %rsi, %rax
    movq    %rdi, %rcx
    leaq    _ZN8rust_out4main17hb497928495d48c40E(%rip), %rdi
    movq    %rcx, %rsi
    movq    %rax, %rdx
    jmp _ZN3std2rt10lang_start17h14cbded5fe3cd915E@PLT
.Lfunc_end1:
    .size   main, .Lfunc_end1-main
    .cfi_endproc


    .section    ".note.GNU-stack","",@progbits

这意味着生成的二进制文件将存在零差异(不仅在性能方面)。

更何况,你用个函数也没关系;以下方法:

fn main() {
    let x = 2i64;
    let y = 5i64;

    let res = x + y;
}

也产生相同的程序集。

底线是,一般来说,无论您是在 main() 中还是在其外部声明函数,函数都会被内联。

编辑:正如 Shepmaster 指出的那样,在这个程序中没有副作用,因此两种变体生成的程序集实际上与以下程序相同:

fn main() {}

然而,两者的 MIR 输出也是相同的(并且不同于空白 main()),因此即使副作用是现在。

As a bit of an aside, any tips on how I could have found out the answer for myself (either through reading any specific set of documents, or tooling I could use) would be welcome.

你知道the Rust playground吗?

输入您的代码,点击 "LLVM IR"、"Assembly" 或 "MIR" 而不是 "Run",您将看到为什么发出的低级表示说代码。

我个人更喜欢LLVM IR(我习惯从C++读它),它仍然比汇编高得多,但仍然是post语言。

I was wondering if there is any negative performance implication of doing this?

这是一个非常复杂的问题;其实

在 Rust 中本地或外部声明函数之间的唯一区别 是范围之一。在本地声明它只会缩小它的范围。 没有别的

但是...范围和用法会对编译产生巨大影响。

例如,仅使用一次的函数比使用 10 次的函数更有可能被内联。编译器无法轻易估计 pub 函数(无界)的使用次数,但对局部或非 pub 函数具有完善的知识。并且函数是否内联会极大地影响性能配置文件(或好或坏)。

因此,通过缩小范围从而限制使用,您鼓励编译器考虑内联您的函数(除非您将其标记为 "cold")。

另一方面,由于范围缩小,无法共享(显然)。

So... what?

遵循用法:在尽可能紧凑的范围内定义一个项目。

这是封装:现在,下次需要修改这块时,您就知道受影响的范围了。

对 Rust 有一些信任,如果可以避免,它不会引入开销。