F#会自动内联某些函数,甚至以为它们未标记为"inline",这是故意的吗? [英] F# automatically inlines some functions even thought they are not marked with `inline`, is this intended?

查看:113
本文介绍了F#会自动内联某些函数,甚至以为它们未标记为"inline",这是故意的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使没有用"inline"标记,F#似乎也会自动内联某些功能.

It appears that F# automatically inlines some functions, even though they are not marked with "inline".

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

By inspecting IL, I see the compiler has aggressively inlined 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关键字的情况下才内联.有提到它的参考文献吗?

The behaviour looks strange to me. I had thought that the compiler should only inline if there is inline keyword. Are there any reference which mentioned it?

推荐答案

inline关键字是强制编译器内联函数的一种方法,因此允许函数采用类型作为参数并提高性能.编译器没有理由不内联函数,因为它认为它适合发行版本.

The inline keyword is a way of forcing the compiler to inline a function and as a result allows a function to take a type as a parameter and increases performance. There is no reason for the compiler to not inline functions as it sees fit for a release build.

这篇关于F#会自动内联某些函数,甚至以为它们未标记为"inline",这是故意的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆