Hashcode calculation update (dotnet/corefx#37002)
authorbuyaa-n <buyankhishig.namnan@microsoft.com>
Thu, 18 Apr 2019 19:46:20 +0000 (12:46 -0700)
committerGitHub <noreply@github.com>
Thu, 18 Apr 2019 19:46:20 +0000 (12:46 -0700)
Seems packaging failure is not related, merging, thank you all for feedback

Commit migrated from https://github.com/dotnet/corefx/commit/e816593d49d87ec2a5815e34b82c5c8ba86aa85f

src/libraries/System.Private.Xml.Linq/src/System.Private.Xml.Linq.csproj
src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XHashtable.cs
src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlName.cs

index 701858e..667b1c5 100644 (file)
@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <ProjectGuid>{BAC347A3-9841-44FC-B1E3-2344D1152C23}</ProjectGuid>
     <AssemblyName>System.Private.Xml.Linq</AssemblyName>
@@ -63,5 +63,6 @@
     <Reference Include="System.Threading" />
     <Reference Include="System.Threading.Tasks" />
     <Reference Include="System.Threading.Thread" />
+    <Reference Include="System.Memory" />
   </ItemGroup>
 </Project>
\ No newline at end of file
index caeeac8..ff637f4 100644 (file)
@@ -63,8 +63,6 @@ namespace System.Xml.Linq
     {
         private XHashtableState _state;                          // SHARED STATE: Contains all XHashtable state, so it can be atomically swapped when resizes occur
 
-        private const int StartingHash = (5381 << 16) + 5381;   // Starting hash code value for string keys to be hashed
-
         /// <summary>
         /// Prototype of function which is called to extract a string key value from a hashed value.
         /// Returns null if the hashed value is invalid (e.g. value has been released due to a WeakReference TValue being cleaned up).
@@ -397,22 +395,8 @@ namespace System.Xml.Linq
             /// </summary>
             private static int ComputeHashCode(string key, int index, int count)
             {
-                int hashCode = StartingHash;
-                int end = index + count;
                 Debug.Assert(key != null, "key should have been checked previously for null");
-
-                // Hash the key
-                for (int i = index; i < end; i++)
-                    unchecked
-                    {
-                        hashCode += (hashCode << 7) ^ key[i];
-                    }
-
-                // Mix up hash code a bit more and clear the sign bit.  This code was taken from NameTable.cs in System.Xml.
-                hashCode -= hashCode >> 17;
-                hashCode -= hashCode >> 11;
-                hashCode -= hashCode >> 5;
-                return hashCode & 0x7FFFFFFF;
+                return string.GetHashCode(key.AsSpan(index, count));
             }
 
             /// <summary>
index f9a206f..75c36ea 100644 (file)
@@ -182,18 +182,7 @@ namespace System.Xml
             int hashCode = 0;
             if (name != null)
             {
-                unchecked
-                {
-                    for (int i = name.Length - 1; i >= 0; i--)
-                    {
-                        char ch = name[i];
-                        if (ch == ':') break;
-                        hashCode += (hashCode << 7) ^ ch;
-                    }
-                    hashCode -= hashCode >> 17;
-                    hashCode -= hashCode >> 11;
-                    hashCode -= hashCode >> 5;
-                }
+                return string.GetHashCode(name.AsSpan(name.LastIndexOf(':') + 1));
             }
             return hashCode;
         }