在EqualsHelper方法里面一看,在.NET框架 [英] A look inside the EqualsHelper method in the .NET framework

查看:254
本文介绍了在EqualsHelper方法里面一看,在.NET框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看等于法实施字符串类的.NET Framework,发现它使用了 EqualsHelper 方法。我发现,这真是一个非常酷的和有效的方法,但有一些非常weired我发现,为什么他们递增指针(或进行补偿),通过除法运算,如:

I was having a look at the Equals method implementation of the String class of the .NET framework and found that it uses the EqualsHelper method. I found that it's really a very cool and efficient method, but there's something very weired I found, why do they increment the pointers (or making offsets) via a division operation, like:

*(长*)(PTR +(IntPtr的)8/2),PTR + =(IntPtr的)4/2; 等。

谢谢!

推荐答案

嗯,我没有看到你在谈论的原因是,你正在寻找64位的来源,而不是32位消息人士透露,因为我本来。

Ah, the reason I didn't see what you were talking about is that you're looking at the 64-bit sources, rather than the 32-bit sources, as I was originally.

事实证明,原始来源$ C ​​$ C文件有一个#如果指令在里面。它做不同的事情取决于 AMD64 符号是否被定义在编译的时候。

It turns out that the original source code file has an #if directive in it. It does different things depending on whether or not the AMD64 symbol is defined at compile time.

在原来的code的意见是非常有益的。基本上,编制为64位平台架构的时候,他们选择了12展开循环,并检查3的四字中的时间。这是成为可能,不同的系统架构中的性能优化。

The comments in the original code are quite instructive. Basically, when compiling the framework for 64-bit platforms, they've chosen to unroll the loop by 12 and check 3 quadwords at a time. This is a performance optimization that is made possible by the 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

如果你有兴趣在.NET Framework的内部结构,确保从下载完整版共享源代码的这网站。你错过了很多有趣的事情,当你试图只使用.NET反射来做到这一点。不是其中最重要的是注释。想想看,我见过的人认为在这里评论是没有必要写得很好code!

If you're interested in the internals of the .NET Framework, make sure to download the full version of the shared source from this site. You miss a lot of interesting things when you're trying to do it using only .NET Reflector. Not the least of which is the comments. And to think, I've seen people argue on here that comments aren't necessary in well-written code!

这篇关于在EqualsHelper方法里面一看,在.NET框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆