为什么String.GetHashCode()来实现不同的CLR的32位和64位版本? [英] Why is String.GetHashCode() implemented differently in 32-bit and 64-bit versions of the CLR?

查看:615
本文介绍了为什么String.GetHashCode()来实现不同的CLR的32位和64位版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是string.GetHashCode()的32位和64位版本之间的差异背后的技术原因是什么?

What are the technical reasons behind the difference between the 32-bit and 64-bit versions of string.GetHashCode()?

更​​重要的是,为什么64比特版本似乎在遇到NUL字符终止其算法?例如,当64位CLR下运行下面的表达式都返回true。

More importantly, why does the 64-bit version seem to terminate its algorithm when it encounters the NUL character? For example, the following expressions all return true when run under the 64-bit CLR.

"\0123456789".GetHashCode() == "\0987654321".GetHashCode()
"\0AAAAAAAAA".GetHashCode() == "\0BBBBBBBBB".GetHashCode()
"\0The".GetHashCode() == "\0Game".GetHashCode()

这行为(错误?)表现为一个性能问题当我们用这样的字符串作为字典键。

This behavior (bug?) manifested as a performance issue when we used such strings as keys in a Dictionary.

推荐答案

这看起来像一个已知的问题,微软将无法修复

This looks like a known issue which Microsoft would not fix:

正如你所提到的这将是一些程序(即使他们真的不应该依托重大更改关于这一点),这样做的风险被认为过高在当前版本中解决此问题。

As you have mentioned this would be a breaking change for some programs (even though they shouldn't really be relying on this), the risk of this was deemed too high to fix this in the current release.

我同意碰撞的速度,这将导致在默认的字典< ;弦乐,对象>将这个被夸大。如果这是你的应用程序的性能产生不利影响,我会建议试图解决它通过使用词典构造函数接受一个的IEqualityComparer之一,这样可以提供更合适的GetHashCode的实现。我知道这是不理想,并希望得到这个固定在.NET Framework的未来版本

I agree that the rate of collisions that this will cause in the default Dictionary<String, Object> will be inflated by this. If this is adversely effecting your applications performance, I would suggest trying to work around it by using one of the Dictionary constructors that takes an IEqualityComparer so you can provide a more appropriate GetHashCode implementation. I know this isn't ideal and would like to get this fixed in a future version of the .NET Framework.

来源:< A HREF =htt​​p://connect.microsoft.com/VisualStudio/feedback/details/519104/string-gethashcode-ignores-any-characters-in-the-string-beyond-the-first-null-byte-in- x64的运行> Microsoft连接 - String.GetHashCode忽略超出64运行的第一个空字节字符串中的任何字符

这篇关于为什么String.GetHashCode()来实现不同的CLR的32位和64位版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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