消除边界检查的 foreach 循环有什么特殊情况?

发布于 2024-07-15 00:23:59 字数 51 浏览 13 评论 0原文

消除边界检查的 foreach/for 循环有什么特殊情况? 另外,它是哪个边界检查?

What is the special case with the foreach/for loop that eliminates bounds checking? Also which bounds checking is it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痞味浪人 2024-07-22 00:23:59

标准

for(int i = 0; i < array.Length; i++) {
    ...
}

循环允许 JIT 安全地删除数组边界检查(索引是否在 [0..length-1] 范围内)。

数组上的 foreach 循环相当于标准 for 遍历数组。

编辑:
正如罗伯特·杰普森指出的:

如果数组是这样的话,这将被优化
当地的。 如果可以从以下位置访问该数组
其他位置,边界检查将
仍须执行。 参考: CLR 中的数组边界检查消除

谢谢! 我自己也不知道。

The standard

for(int i = 0; i < array.Length; i++) {
    ...
}

loop is the one that allows the JIT to safely remove array bounds checks (whether the index is within [0..length-1])

The foreach loop over arrays is equivalent to that standard for loop over arrays.

EDIT:
As Robert Jeppesen points out:

This will be optimized if the array is
local. If the array is accessible from
other locations, bounds checking will
still be performed. Reference: Array Bounds Check Elimination in the CLR

Thanks! Didn't know that myself.

半葬歌 2024-07-22 00:23:59

SealedSun 是对的。 不要像 C++ 中那样进行优化。 JIT 非常聪明,可以为您做正确的事情。 您始终可以用不同的方式对循环进行编码,然后检查 IL 代码。

        static void Main(string[] args)
        {
            int[] array = new int[100];
00000000  push        edi  
00000001  push        esi  
00000002  push        eax  
00000003  xor         eax,eax 
00000005  mov         dword ptr [esp],eax 
00000008  mov         edx,64h 
0000000d  mov         ecx,79174292h 
00000012  call        49E73198 
00000017  mov         esi,eax 
            int sum = 0;
00000019  xor         edx,edx 
0000001b  mov         dword ptr [esp],edx 
            for(int index = 0; index < array.Length; index++)
0000001e  mov         edi,dword ptr [esi+4] 
00000021  test        edi,edi 
00000023  jle         00000033 
            {
                sum += array[index];
00000025  mov         eax,dword ptr [esi+edx*4+8] 
00000029  add         dword ptr [esp],eax 
            for(int index = 0; index < array.Length; index++)
0000002c  add         edx,1 
0000002f  cmp         edi,edx 
00000031  jg          00000025 
            }

            Console.WriteLine(sum.ToString());
00000033  mov         esi,dword ptr [esp] 
00000036  call        493765F8 
0000003b  push        eax  
0000003c  mov         ecx,esi 
0000003e  xor         edx,edx 
00000040  call        49E83A8B 
00000045  mov         edi,eax 
00000047  mov         edx,88h 
0000004c  mov         ecx,1 
00000051  call        49E731B0 
00000056  mov         esi,eax 
00000058  cmp         dword ptr [esi+70h],0 
0000005c  jne         00000068 
0000005e  mov         ecx,1 
00000063  call        4936344C 
00000068  mov         ecx,dword ptr [esi+70h] 
0000006b  mov         edx,edi 
0000006d  mov         eax,dword ptr [ecx] 
0000006f  call        dword ptr [eax+000000D8h] 
00000075  pop         ecx  
        }
00000076  pop         esi  
00000077  pop         edi  
00000078  ret              

现在,如果按照 C++ 中的方式优化代码,您将得到以下结果:

        static void Main(string[] args)
        {
            int[] array = new int[100];
00000000  push        edi  
00000001  push        esi  
00000002  push        ebx  
00000003  push        eax  
00000004  xor         eax,eax 
00000006  mov         dword ptr [esp],eax 
00000009  mov         edx,64h 
0000000e  mov         ecx,79174292h 
00000013  call        49E73198 
00000018  mov         esi,eax 
            int sum = 0;
0000001a  xor         edx,edx 
0000001c  mov         dword ptr [esp],edx 

            int length = array.Length;
0000001f  mov         ebx,dword ptr [esi+4] 
            for (int index = 0; index < length; index++)
00000022  test        ebx,ebx 
00000024  jle         0000003B 
00000026  mov         edi,dword ptr [esi+4] 
            {
                sum += array[index];
00000029  cmp         edx,edi                  <-- HERE
0000002b  jae         00000082                 <-- HERE
0000002d  mov         eax,dword ptr [esi+edx*4+8] 
00000031  add         dword ptr [esp],eax 
            for (int index = 0; index < length; index++)
00000034  add         edx,1 
00000037  cmp         edx,ebx 
00000039  jl          00000029 
            }

            Console.WriteLine(sum.ToString());
0000003b  mov         esi,dword ptr [esp] 
0000003e  call        493765F8 
00000043  push        eax  
00000044  mov         ecx,esi 
00000046  xor         edx,edx 
00000048  call        49E83A8B 
0000004d  mov         edi,eax 
0000004f  mov         edx,88h 
00000054  mov         ecx,1 
00000059  call        49E731B0 
0000005e  mov         esi,eax 
00000060  cmp         dword ptr [esi+70h],0 
00000064  jne         00000070 
00000066  mov         ecx,1 
0000006b  call        4936344C 
00000070  mov         ecx,dword ptr [esi+70h] 
00000073  mov         edx,edi 
00000075  mov         eax,dword ptr [ecx] 
00000077  call        dword ptr [eax+000000D8h] 
0000007d  pop         ecx  
        }
0000007e  pop         ebx  
0000007f  pop         esi  
00000080  pop         edi  
00000081  ret              
00000082  call        4A12746B 
00000087  int         3    

顺便说一句 - 这与 foreach 语句相同:

        static void Main(string[] args)
        {
            int[] array = new int[100];
00000000  push        edi  
00000001  push        esi  
00000002  push        eax  
00000003  xor         eax,eax 
00000005  mov         dword ptr [esp],eax 
00000008  mov         edx,64h 
0000000d  mov         ecx,79174292h 
00000012  call        49E73198 
00000017  mov         esi,eax 
            int sum = 0;
00000019  xor         edx,edx 
0000001b  mov         dword ptr [esp],edx 
            for(int index = 0; index < array.Length; index++)
0000001e  mov         edi,dword ptr [esi+4] 
00000021  test        edi,edi 
00000023  jle         00000033 
            {
                sum += array[index];
00000025  mov         eax,dword ptr [esi+edx*4+8] 
00000029  add         dword ptr [esp],eax 
            for(int index = 0; index < array.Length; index++)
0000002c  add         edx,1 
0000002f  cmp         edi,edx 
00000031  jg          00000025 
            }

            Console.WriteLine(sum.ToString());
00000033  mov         esi,dword ptr [esp] 
00000036  call        493765F8 
0000003b  push        eax  
0000003c  mov         ecx,esi 
0000003e  xor         edx,edx 
00000040  call        49E83A8B 
00000045  mov         edi,eax 
00000047  mov         edx,88h 
0000004c  mov         ecx,1 
00000051  call        49E731B0 
00000056  mov         esi,eax 
00000058  cmp         dword ptr [esi+70h],0 
0000005c  jne         00000068 
0000005e  mov         ecx,1 
00000063  call        4936344C 
00000068  mov         ecx,dword ptr [esi+70h] 
0000006b  mov         edx,edi 
0000006d  mov         eax,dword ptr [ecx] 
0000006f  call        dword ptr [eax+000000D8h] 
00000075  pop         ecx  
        }
00000076  pop         esi  
00000077  pop         edi  
00000078  ret              

不要尝试在没有数字的情况下优化代码。 正如您所看到的,如果您不妨碍 JIT,它将为您做很多事情。 在优化之前使用分析器。 总是。

SealedSun is right. Don't optimize the way you would in C++. JIT is quite smart to do the right thing for you. You can always code the loop in different ways and then inspect the IL code.

        static void Main(string[] args)
        {
            int[] array = new int[100];
00000000  push        edi  
00000001  push        esi  
00000002  push        eax  
00000003  xor         eax,eax 
00000005  mov         dword ptr [esp],eax 
00000008  mov         edx,64h 
0000000d  mov         ecx,79174292h 
00000012  call        49E73198 
00000017  mov         esi,eax 
            int sum = 0;
00000019  xor         edx,edx 
0000001b  mov         dword ptr [esp],edx 
            for(int index = 0; index < array.Length; index++)
0000001e  mov         edi,dword ptr [esi+4] 
00000021  test        edi,edi 
00000023  jle         00000033 
            {
                sum += array[index];
00000025  mov         eax,dword ptr [esi+edx*4+8] 
00000029  add         dword ptr [esp],eax 
            for(int index = 0; index < array.Length; index++)
0000002c  add         edx,1 
0000002f  cmp         edi,edx 
00000031  jg          00000025 
            }

            Console.WriteLine(sum.ToString());
00000033  mov         esi,dword ptr [esp] 
00000036  call        493765F8 
0000003b  push        eax  
0000003c  mov         ecx,esi 
0000003e  xor         edx,edx 
00000040  call        49E83A8B 
00000045  mov         edi,eax 
00000047  mov         edx,88h 
0000004c  mov         ecx,1 
00000051  call        49E731B0 
00000056  mov         esi,eax 
00000058  cmp         dword ptr [esi+70h],0 
0000005c  jne         00000068 
0000005e  mov         ecx,1 
00000063  call        4936344C 
00000068  mov         ecx,dword ptr [esi+70h] 
0000006b  mov         edx,edi 
0000006d  mov         eax,dword ptr [ecx] 
0000006f  call        dword ptr [eax+000000D8h] 
00000075  pop         ecx  
        }
00000076  pop         esi  
00000077  pop         edi  
00000078  ret              

Now if optimize the code the way you would in C++ you get the following:

        static void Main(string[] args)
        {
            int[] array = new int[100];
00000000  push        edi  
00000001  push        esi  
00000002  push        ebx  
00000003  push        eax  
00000004  xor         eax,eax 
00000006  mov         dword ptr [esp],eax 
00000009  mov         edx,64h 
0000000e  mov         ecx,79174292h 
00000013  call        49E73198 
00000018  mov         esi,eax 
            int sum = 0;
0000001a  xor         edx,edx 
0000001c  mov         dword ptr [esp],edx 

            int length = array.Length;
0000001f  mov         ebx,dword ptr [esi+4] 
            for (int index = 0; index < length; index++)
00000022  test        ebx,ebx 
00000024  jle         0000003B 
00000026  mov         edi,dword ptr [esi+4] 
            {
                sum += array[index];
00000029  cmp         edx,edi                  <-- HERE
0000002b  jae         00000082                 <-- HERE
0000002d  mov         eax,dword ptr [esi+edx*4+8] 
00000031  add         dword ptr [esp],eax 
            for (int index = 0; index < length; index++)
00000034  add         edx,1 
00000037  cmp         edx,ebx 
00000039  jl          00000029 
            }

            Console.WriteLine(sum.ToString());
0000003b  mov         esi,dword ptr [esp] 
0000003e  call        493765F8 
00000043  push        eax  
00000044  mov         ecx,esi 
00000046  xor         edx,edx 
00000048  call        49E83A8B 
0000004d  mov         edi,eax 
0000004f  mov         edx,88h 
00000054  mov         ecx,1 
00000059  call        49E731B0 
0000005e  mov         esi,eax 
00000060  cmp         dword ptr [esi+70h],0 
00000064  jne         00000070 
00000066  mov         ecx,1 
0000006b  call        4936344C 
00000070  mov         ecx,dword ptr [esi+70h] 
00000073  mov         edx,edi 
00000075  mov         eax,dword ptr [ecx] 
00000077  call        dword ptr [eax+000000D8h] 
0000007d  pop         ecx  
        }
0000007e  pop         ebx  
0000007f  pop         esi  
00000080  pop         edi  
00000081  ret              
00000082  call        4A12746B 
00000087  int         3    

By the way - here is the same with foreach statement:

        static void Main(string[] args)
        {
            int[] array = new int[100];
00000000  push        edi  
00000001  push        esi  
00000002  push        eax  
00000003  xor         eax,eax 
00000005  mov         dword ptr [esp],eax 
00000008  mov         edx,64h 
0000000d  mov         ecx,79174292h 
00000012  call        49E73198 
00000017  mov         esi,eax 
            int sum = 0;
00000019  xor         edx,edx 
0000001b  mov         dword ptr [esp],edx 
            for(int index = 0; index < array.Length; index++)
0000001e  mov         edi,dword ptr [esi+4] 
00000021  test        edi,edi 
00000023  jle         00000033 
            {
                sum += array[index];
00000025  mov         eax,dword ptr [esi+edx*4+8] 
00000029  add         dword ptr [esp],eax 
            for(int index = 0; index < array.Length; index++)
0000002c  add         edx,1 
0000002f  cmp         edi,edx 
00000031  jg          00000025 
            }

            Console.WriteLine(sum.ToString());
00000033  mov         esi,dword ptr [esp] 
00000036  call        493765F8 
0000003b  push        eax  
0000003c  mov         ecx,esi 
0000003e  xor         edx,edx 
00000040  call        49E83A8B 
00000045  mov         edi,eax 
00000047  mov         edx,88h 
0000004c  mov         ecx,1 
00000051  call        49E731B0 
00000056  mov         esi,eax 
00000058  cmp         dword ptr [esi+70h],0 
0000005c  jne         00000068 
0000005e  mov         ecx,1 
00000063  call        4936344C 
00000068  mov         ecx,dword ptr [esi+70h] 
0000006b  mov         edx,edi 
0000006d  mov         eax,dword ptr [ecx] 
0000006f  call        dword ptr [eax+000000D8h] 
00000075  pop         ecx  
        }
00000076  pop         esi  
00000077  pop         edi  
00000078  ret              

Don't try to optimize your code without numbers. As you can see JIT will do a lot for your if you don't stand in its way. Use profiler before you optimize. ALWAYS.

り繁华旳梦境 2024-07-22 00:23:59

有关详细信息,请参阅:

http://codebetter.com /blogs/david.hayden/archive/2005/02/27/56104.aspx

基本上,如果你有一个 for 循环,并且你显式引用 IList.Count 或 Array.Length,JIT 会捕捉到这一点,并跳过边界检查。 它比预先计算列表长度更快。

我相信,列表或数组上的 foreach 会在内部执行相同的操作。

See this for details:

http://codebetter.com/blogs/david.hayden/archive/2005/02/27/56104.aspx

Basically, if you have a for loop, and you explicitly refer to IList.Count or Array.Length, the JIT will catch that, and skip the bounds checking. It makes it faster than precomputing the list length.

foreach on a list or array will do the same thing internally, I believe.

萌酱 2024-07-22 00:23:59

foreach 循环使用枚举器,它是处理循环的类或结构。 枚举器有一个 Current 属性,用于返回集合中的当前项。 这消除了使用索引来访问集合中的项目,因此不需要获取项目的额外步骤,包括边界检查。

A foreach loop uses an enumerator, which is a class or structure that handles the looping. The enumerator has a Current property that returns the current item from the collection. That elliminates the use of an index to access the item in the collection, so the extra step to get the item, including bounds checking, is not needed.

盛夏已如深秋| 2024-07-22 00:23:59

什么? 我不确定是否有可能消除 C# 中的边界检查。 如果您想要非托管代码,请使用:

int[] array;
fixed (int * i = array)
{
 while (i++)
  Console.WriteLine("{0}", *i);
}

例如 - 它不检查边界,并且会严重死掉。 :-)

What? I'm not sure if it is even possible to eliminate bounds checking in c#. If you want unmanaged code, then use:

int[] array;
fixed (int * i = array)
{
 while (i++)
  Console.WriteLine("{0}", *i);
}

for example - it doesn't check bounds, and dies terribly. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文