影響はありません。私は両方のバリアントについて生成されたアセンブリをチェックし、それは同一です。
私が比較される2つのバージョン:
"外部":
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
}
"ローカル":
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 [email protected]
.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が指摘したように、このプログラムではそこには副作用ではないので、両方の変形のために生成されたアセンブリは、実際に1つのと同じです:
fn main() {}
しかし、MIR出力両方とも同じであり(ブランクの場合とは異なります)、副作用があっても関数の場所からの違いはありません。
ベンチマークを実行したり、MIR /アセンブリ出力を確認したりして、自分で見つけることができます。 – ljedrz
@ljedrz:ベンチマークは気まぐれな獣です。最適化されていない最適化されたMIR/LLVM IR /アセンブリを最もよく確認してください。 @MatthieuM。 –
。ベンチマークは、他の方法がもはや実用的ではないより複雑なケースに適しています。 – ljedrz