The JIT can be improved when a static variable is declared with a constant value, ideally by treating it like a constant.
Declared as constant
static class Bla
{
public const double Value1 = 2d;
public const double Value2 = 5d;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double BlaMethod(double val)
{
return val / Value1 / Value2;
}
}
---------------------------------------------------------
vzeroupper
xchg ax,ax
vdivsd xmm0,xmm0,[rel 7FFA`42B2`F0A8h]
vdivsd xmm0,xmm0,[rel 7FFA`42B2`F0B0h]
ret
Declared as static
static class Bla
{
public static double Value1 = 2d;
public static double Value2 = 5d;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double BlaMethod(double val)
{
return val / Value1 / Value2;
}
}
---------------------------------------------------------
sub rsp,28h
vzeroupper
vmovsd [rsp+30h],xmm0
mov rcx,7FFA`42B9`6A88h
mov edx,2
call 0000`7FFA`A260`70B0h
vmovsd xmm0,[rsp+30h]
vdivsd xmm0,xmm0,[rel 7FFA`42B9`6AC0h]
vdivsd xmm0,xmm0,[rel 7FFA`42B9`6AC8h]
add rsp,28h
ret
The JIT can be improved when a static variable is declared with a constant value, ideally by treating it like a constant.
Declared as constant
Declared as static