Fix ordering of adding in Regex's BitVector.GetHashCode (#71913)
authorStephen Toub <stoub@microsoft.com>
Mon, 11 Jul 2022 04:59:22 +0000 (00:59 -0400)
committerGitHub <noreply@github.com>
Mon, 11 Jul 2022 04:59:22 +0000 (22:59 -0600)
It's slightly cheaper to call AddBytes before Add(int).

src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/Symbolic/BitVector.cs

index edfeac5..4eda223 100644 (file)
@@ -175,12 +175,8 @@ namespace System.Text.RegularExpressions.Symbolic
             if (_hashcode == null)
             {
                 HashCode hc = default;
+                hc.AddBytes(MemoryMarshal.AsBytes<ulong>(_blocks));
                 hc.Add(Length);
-                if (_blocks is ulong[] blocks) // may be null in case of a default struct
-                {
-                    hc.AddBytes(MemoryMarshal.AsBytes<ulong>(blocks));
-                }
-
                 _hashcode = hc.ToHashCode();
             }