JIT optimization in script combining inline property and non-mutable boolean module

In the next program

module Program

let condition = System.DateTime.Now.Millisecond % 2 = 0

let inline reliesOnCondition (x:int) =
    if condition then
        printfn "%i" x

[<EntryPoint>]
let main args =
    reliesOnConditional System.DateTime.Now.Second
    0

Will JIT optimize expression reliesOnCondition System.DateTime.Now.Secondif it conditionturns out to be false when loading a module?

+5
source share
2 answers

, , JIT . , . , , . , jitted , , , . , (#if #). .

+2

*. F # IL , condition . - JIT reliesOnCondition.

, . ( .)

F # "SOQ"

F #. false.

module Program

let condition = false

let inline reliesOnCondition (x:int) =
    if condition then
        printfn "%i" x

[<EntryPoint>]
let main args =
    printfn "(attach a debugger and press any key)"
    System.Console.ReadKey(true) |> ignore

    reliesOnCondition System.DateTime.Now.Second
    0

IL PDB

ildasm, IL, /SOURCE. IL , .

ildasm SOQ.exe /OUT=SOQ-annotated.exe.il /SOURCE

IL

ilasm, IL, /DEBUG, PDB. . -, F # , IL.

ilasm SOQ-annotated.exe.il /DEBUG

Visual Studio

. , JIT-ted, . Visual Studio .

IL VS. " " " ". x86-.

opp x86. F # (ildasm /SOURCE), IL (ilasm /DEBUG) x86 ( Visual Studio).

//000014:     reliesOnCondition System.DateTime.Now.Second
    IL_0026:  call       valuetype [mscorlib]System.DateTime [mscorlib]System.DateTime::get_Now()
000000db  lea         ecx,[ebp-58h] 
000000de  call        595E8C00 
    IL_002b:  stloc.3
000000e3  lea         edi,[ebp-30h] 
000000e6  lea         esi,[ebp-58h] 
000000e9  movq        xmm0,mmword ptr [esi] 
000000ed  movq        mmword ptr [edi],xmm0 
    IL_002c:  ldloca.s   V_3
000000f1  lea         eax,[ebp-30h] 
000000f4  mov         dword ptr [ebp-74h],eax 
    IL_002e:  call       instance int32 [mscorlib]System.DateTime::get_Second()
000000f7  mov         ecx,dword ptr [ebp-74h] 
000000fa  call        5960A670 
000000ff  mov         dword ptr [ebp-5Ch],eax 
    IL_0033:  stloc.2
00000102  mov         eax,dword ptr [ebp-5Ch] 
00000105  mov         dword ptr [ebp-28h],eax 
    IL_0034:  call       bool Program::get_condition()
00000108  call        dword ptr ds:[004232D8h] 
0000010e  mov         dword ptr [ebp-60h],eax 
    IL_0039:  brfalse.s  IL_003d
00000111  cmp         dword ptr [ebp-60h],0 
00000115  je          0000011A 
... snip ...

, IL- 34 Program::get_condition(), JIT no-op. ( , , .)

* (x64 Win7). JIT x86 x64, , NGEN . .

+4

All Articles