View inside EqualsHelper method in .NET framework

I looked at the implementation of the Equals method of the .NET class String class and found that it uses the EqualsHelper method. I found this to be a really cool and efficient method, but I found something very solid why they increment pointers (or do offsets) using a division operation, like this:

*(long*)(ptr + (IntPtr)8 / 2), ptr += (IntPtr)4 / 2; etc.

Thanks!

+6
source share
1 answer

And, the reason why I did not see what you were talking about is that you are looking at 64-bit sources, and not at 32-bit sources, as I was originally.

It turns out that the source code file has the #if directive in it. It performs different actions depending on whether the AMD64 symbol is defined at compile time.

Comments in the source code are very instructive. In principle, when compiling the framework for 64-bit platforms, they decided to deploy a loop of 12 and simultaneously check 3 quadwords . This is a performance optimization that is made possible by a different system architecture.

  // unroll the loop #if AMD64 // for AMD64 bit platform we unroll by 12 and // check 3 qword at a time. This is less code // than the 32 bit case and is shorter pathlength while (length >= 12) { if (*(long*)a != *(long*)b) break; if (*(long*)(a+4) != *(long*)(b+4)) break; if (*(long*)(a+8) != *(long*)(b+8)) break; a += 12; b += 12; length -= 12; } #else while (length >= 10) { if (*(int*)a != *(int*)b) break; if (*(int*)(a+2) != *(int*)(b+2)) break; if (*(int*)(a+4) != *(int*)(b+4)) break; if (*(int*)(a+6) != *(int*)(b+6)) break; if (*(int*)(a+8) != *(int*)(b+8)) break; a += 10; b += 10; length -= 10; } #endif 

If you are interested in the internal components of the .NET Framework, be sure to download the full version of the shared source from this site . You miss a lot of interesting things when you try to do this using only the .NET Reflector. Not the last of these are comments. And to think, I saw people arguing here that comments are not needed in well-written code!

+10
source

Source: https://habr.com/ru/post/887553/


All Articles