F# 似乎自动内联了一些函数,即使它们没有标记为“内联”。
let a x= x + 3
let b x= x * x
let funB x y =
if x > y then 3
else 1
let funC x =
let s = a x
let c = funB s (b x)
c + 1
通过检查 IL,我看到编译器已积极内联funB
&a,b
funC:
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldc.i4.3
IL_0003: add
IL_0004: stloc.0 // s
IL_0005: ldarg.0
IL_0006: ldarg.0
IL_0007: mul
IL_0008: stloc.1
IL_0009: ldloc.0 // s
IL_000A: ldloc.1
IL_000B: ble.s IL_0011
IL_000D: ldc.i4.3
IL_000E: nop
IL_000F: br.s IL_0013
IL_0011: ldc.i4.1
IL_0012: nop
IL_0013: ldc.i4.1
IL_0014: add
IL_0015: ret
这种行为对我来说看起来很奇怪。我曾认为编译器应该只在有inline
关键字的情况下内联。有没有提到它的参考?