为什么86 JIT比64更聪明? [英] Why x86 JIT is smarter than x64?
问题描述
我运行一个非常简单的程序
I'm running a very simple program
static void Main(string[] args)
{
Console.WriteLine(Get4S());
Console.WriteLine(Get4());
}
private static int Get4S()
{
return 4;
}
private static int Get4()
{
int res = 0;
for (int i = 0; i < 4; i++)
{
res++;
}
return res;
}
当它工作在 86
它内联 Get4S
方法和 Get4
ASM code是:
when it works under x86
it inlines Get4S
method and Get4
asm code is:
00000000 push ebp
00000001 mov ebp,esp
00000003 xor eax,eax
00000005 inc eax
00000006 inc eax
00000007 inc eax
00000008 inc eax
00000009 pop ebp
0000000a ret
但在x64上运行时,我们得到 Get4S
方法,但 Get4
ASM完全不优化相同的ASM
BUT when running under x64 we get same asm for Get4S
method, but Get4
asm is not optimized at all:
00000000 xor eax,eax
00000002 xor edx,edx
00000004 inc eax
00000006 inc edx
00000008 cmp edx,4
0000000b jl 0000000000000004
0000000d ret
我假定 64
JIT展开循环,然后看到结果可以编译时进行计算,并与编译时的结果功能将被内联。但它没有happend。
I supposed that x64
JIT unroll the loop, then see that result can be computed in compile-time, and function with compile-time result will be inlined. But nothing from it happend.
为什么 64
在这种情况下如此愚蠢?..
Why x64
is so stupid in this case?..
推荐答案
我得到了这一点。这是因为当选择 64
创建被用于RyuJIT,即使的.Net 4.5.2
目标平台选择。所以我通过添加app.config文件本节固定它:
I got the point. It's because RyuJIT is used when x64
build is selected, even if .Net 4.5.2
target platform is selected. So i fixed it by adding this section in App.Config file:
<configuration>
<runtime>
<useLegacyJit enabled="1" />
</runtime>
</configuration>
此标记使传统的x64 JIT(加引号,是因为我觉得他比闪亮RyuJIT好得多),并在main方法的结果ASM是:
This markup enables "legacy" x64 JIT (in quotes, because I think he's much better than "shiny" RyuJIT), and result ASM in main method is:
00000000 sub rsp,28h
00000004 mov ecx,4
00000009 call 000000005EE75870
0000000e mov ecx,4
00000013 call 000000005EE75870
00000018 nop
00000019 add rsp,28h
0000001d ret
这两种方法是在编译时计算和他们的价值观内联。
both methods was calculated in compile time and inlined by theirs values.
结论:当净4.6
安装,旧的x64抖动是由 RyuJIT
替换下的所有解决方案 CLR4.0
。因此,只有将其关闭的方法是 useLegacyJit
开关或 COMPLUS_AltJit
环境变量
Conclusion: When .Net 4.6
is installed, old x64 jitter is replaced by RyuJIT
for all solutions under CLR4.0
. So only way to turn it off is useLegacyJit
switch or COMPLUS_AltJit
environment variable
这篇关于为什么86 JIT比64更聪明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!