F# automatically inlines some functions even thought they are not marked with `inline`, is this intended?

天大地大妈咪最大 提交于 2019-12-10 17:37:51

问题


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

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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/17904557/f-automatically-inlines-some-functions-even-thought-they-are-not-marked-with-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!