colinfang
colinfang

Reputation: 21727

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

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?

Upvotes: 4

Views: 251

Answers (1)

N_A
N_A

Reputation: 19897

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.

Upvotes: 2

Related Questions