在CLR数组边界检查消除? [英] Array Bounds Check Elimination in the CLR?
问题描述
我最近读<一个href=\"http://blogs.msdn.com/b/clr$c$cgeneration/archive/2009/08/13/array-bounds-check-elimination-in-the-clr.aspx\"相对=nofollow>本文由Dave Detlefs 他在presents少数情况下CLR执行数组边界检查消除。我决定把这个测试自己,所以我做了以下内容:
I was recently reading this article by Dave Detlefs in which he presents a few cases where the CLR performs array bounds check elimination. I decided to test this myself, so I did the following:
- 开业的Visual Studio 2010 SP1旗舰版
- 创建类型控制台应用程序的一个新的C#项目(针对默认.NET 4客户端配置文件)
-
增加了以下code(所有的子方法是直接从文章所):
- Opened Visual Studio 2010 Ultimate SP1
- Created a new C# project of type Console Application (targeting .NET 4 Client Profile by default)
Added the following code (all sub-methods are taken directly from the article):
class Program {
static void Main(string[] args) {
int[] array = new int[30];
Test_SimpleAscend(array);
Test_SimpleRedundant(array, 3);
foreach (int i in array) {
Console.WriteLine(i);
}
}
static void Test_SimpleAscend(int[] a) {
for (int i = 0; i < a.Length; i++)
a[i] = i;
}
static void Test_SimpleRedundant(int[] a, int i) {
int k = a[i];
k = k + a[i];
}
}
切换到发布模式;验证了优化code,在构建选项被选中
Switched to Release mode; verified that "Optimize Code" is checked in the Build options
因此,这里是一个为在dissassembly [我] =我;在Test_SimpleAscend:
So here's the dissassembly for a[i] = i; in Test_SimpleAscend:
a[i] = i;
00000024 mov eax,dword ptr [ebp-4]
00000027 mov edx,dword ptr [ebp-8]
0000002a cmp eax,dword ptr [edx+4]
0000002d jb 00000034
0000002f call 64FD6E08
00000034 mov ecx,dword ptr [ebp-4]
00000037 mov dword ptr [edx+eax*4+8],ecx
在CMP / JB /呼叫边界检查,竟迫使通话将被执行,则会引发IndexOutOfRangeException。
The cmp/jb/call is bounds checking, actually forcing the call to be executed throws an IndexOutOfRangeException.
对于所有阵列同样的事情的访问,包括Test_SimpleRedundant冗余访问。那么,有什么错我的测试方法,或者CLR实际上并没有消除边界检查?我希望我是错的,如果是这样,我想知道我怎么才能真正得到数组边界检查消除。
Same thing for all array accesses, including the redundant access in Test_SimpleRedundant. So is there something wrong with my testing methodology, or the CLR doesn't actually eliminate bounds checking? I hope I'm wrong and if so I'd like to know how I can really get array bounds checking elimination.
推荐答案
多亏了科迪灰色评论,我已经成功地回答我的问题:
Thanks to a comment by Cody Gray, I've managed to answer my own question:
在默认情况下,JIT优化调试时被禁用。为了解决这个问题,可以到调试 - >选项和设置 - >调试 - >常规,并取消这两个启用仅我的code和燮$ P模块负载$ PSS JIT优化。
By default, JIT Optimizations are disabled when debugging. To fix this, one can go to Debug -> Options and Settings -> Debugging -> General, and uncheck both "Enable Just My Code" and "Suppress JIT Optimization on module load".
另请参见 http://msdn.microsoft.com/en-us/库/ ms241594.aspx
启用优化,边界检查被移除像广告。
With optimization enabled, the bounds check are removed as advertised.
在这里我要离开这个以作记录。
I'll leave this here for documentation purposes.
这篇关于在CLR数组边界检查消除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!