我不知道为什么,但我看了标准 C# 编译器 (VS2015) 生成的 IL,它在发布模式下明显没有优化。
我测试的代码非常简单:
static void Main(string[] args)
{
int count = 25 + 7/3;
count += 100;
Console.WriteLine("{0}", count);
}
调试模式下的IL输出为:
// [12 9 - 12 10]
IL_0000: nop
// [34 13 - 34 34]
IL_0001: ldc.i4.s 27 // 0x1b
IL_0003: stloc.0 // count
// [35 13 - 35 26]
IL_0004: ldloc.0 // count
IL_0005: ldc.i4.s 100 // 0x64
IL_0007: add
IL_0008: stloc.0 // count
// [36 13 - 36 45]
IL_0009: ldstr "{0}"
IL_000e: ldloc.0 // count
IL_000f: box [mscorlib]System.Int32
IL_0014: call void [mscorlib]System.Console::WriteLine(string, object)
IL_0019: nop
// [37 9 - 37 10]
IL_001a: ret
Release模式下的代码是:
IL_0000: ldc.i4.s 27 // 0x1b
IL_0002: stloc.0 // V_0
IL_0003: ldloc.0 // V_0
IL_0004: ldc.i4.s 100 // 0x64
IL_0006: add
IL_0007: stloc.0 // V_0
IL_0008: ldstr "{0}"
IL_000d: ldloc.0 // V_0
IL_000e: box [mscorlib]System.Int32
IL_0013: call void [mscorlib]System.Console::WriteLine(string, object)
IL_0018: ret
现在,为什么编译器不执行 sum (27 + 100) 并直接用 127 调用 WriteLine ?
我在 C++ 中尝试了相同的示例,它按预期工作。
有一些特殊的标志来执行这种优化?
更新: 我在 MONO 4.6.20 上尝试相同的代码,发布模式下的结果如下
// method line 2
.method private static hidebysig
default void Main (string[] args) cil managed
{
// Method begins at RVA 0x2058
.entrypoint
// Code size 18 (0x12)
.maxstack 8
IL_0000: ldstr "{0}"
IL_0005: ldc.i4.s 0x7f
IL_0007: box [mscorlib]System.Int32
IL_000c: call void class [mscorlib]System.Console::WriteLine(string, ob ject)
IL_0011: ret
} // end of method Program::Main
2024 年更新
我用新的编译器.net core 8重试,最终代码直接在IL中优化
.custom instance void [System.Runtime]System.Runtime.CompilerServices.NullableContextAttribute::.ctor(uint8) = (
01 00 01 00 00
)
// Method begins at RVA 0x2050
// Header size: 12
// Code size: 25 (0x19)
.maxstack 2
.entrypoint
.locals init (
[0] int32 count
)
IL_0000: ldc.i4.s 27
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: ldc.i4.s 100
IL_0006: add
IL_0007: stloc.0
IL_0008: ldstr "{0}"
IL_000d: ldloc.0
IL_000e: box [System.Runtime]System.Int32
IL_0013: call void [System.Console]System.Console::WriteLine(string, object)
IL_0018: ret
您不能依赖编译器的 IL 输出来准确评估代码的优化程度,因为 JIT 将在运行时控制 IL 来生成要运行的实际代码。在这种情况下,JIT 发出的实际 x64(在任何不首选 32 位的 CPU 的发布模式下)如下所示:
sub rsp,28h
mov rcx,7FFF85323E98h
call 00007FFF91C72530 ; I'm not sure what this call does, I assume it's allocating memory for the boxed int
mov rcx,20CA5CB3648h
mov rcx,qword ptr [rcx] ; After this rcx is actually pointing to the string "{0}"
mov dword ptr [rax+8],7Fh ; Box the value 127 into the object that rax points at
mov rdx,rax
call 00007FFF85160070 ; Call Console.WriteLine with its arguments in rcx and rdx
nop
add rsp,28h
ret
所以额外的版本被省略了。
如果我打开“首选 32 位”,发出的 x86 看起来像这样:
mov ecx,72041638h
call 011630F4 ; presumably allocating memory for the boxed int
mov edx,eax
mov eax,dword ptr ds:[40E232Ch] ; loads a pointer to "{0}" into eax
mov dword ptr [edx+4],7Fh ; boxes 127 into object pointed at by edx
mov ecx,eax
call 71F373F4 ; calls Console.WriteLine with arguments in ecx and edx
ret
在这两种情况下,JIT 都优化了局部变量以及额外的加法操作。由于 JIT 执行了如此多的优化,您会发现 C# 编译器本身并没有竭尽全力来优化任何内容。
tl;dr C# 编译器发出的 IL 不是机器运行的,因此通常不代表将应用的优化类型。