Hash quality and stability of String.GetHashCode() in .NET?

前端 未结 5 421
日久生厌
日久生厌 2020-12-31 04:03

I am wondering about the hash quality and the hash stability produced by the String.GetHashCode() implementation in .NET?

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 04:37

    This is an old question, but I'd like to contribute by mentionning this microsoft bug about hash quality.

    Summary: On 64b, hash quality is very low when your string contains '\0' bytes. Basically, only the start of the string will be hashed.

    If like me, you have to use .Net strings to represent binary data as key for high-performance dictionaries, you need to be aware of this bug.

    Too bad, it's a WONTFIX... As a sidenote, I don't understand how they could say that modifying the hashcode being a breaking change, when the code includes

    // We want to ensure we can change our hash function daily.
    // This is perfectly fine as long as you don't persist the
    // value from GetHashCode to disk or count on String A
    // hashing before string B. Those are bugs in your code.
    hash1 ^= ThisAssembly.DailyBuildNumber;
    

    and the hashcode is already different in x86/64b anyway.

提交回复
热议问题