From 328d9f13c03888dc8f359c34dd9871b023769c57 Mon Sep 17 00:00:00 2001 From: Andrew J Said Date: Thu, 26 Oct 2023 23:11:18 +0100 Subject: [PATCH] When hashing the entire string, case sensitivity of hash and equals should be the same --- .../src/System.Collections.Immutable.csproj | 3 -- .../Collections/Frozen/FrozenDictionary.cs | 5 ++-- .../System/Collections/Frozen/FrozenSet.cs | 5 ++-- ...lCaseSensitiveHashCaseInsensitiveEquals.cs | 28 ------------------- ...lCaseSensitiveHashCaseInsensitiveEquals.cs | 27 ------------------ 5 files changed, 4 insertions(+), 64 deletions(-) delete mode 100644 src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/String/OrdinalStringFrozenDictionary_FullCaseSensitiveHashCaseInsensitiveEquals.cs delete mode 100644 src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/String/OrdinalStringFrozenSet_FullCaseSensitiveHashCaseInsensitiveEquals.cs diff --git a/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj b/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj index 33eb902cddff6..273897b01524a 100644 --- a/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj +++ b/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj @@ -66,13 +66,10 @@ The System.Collections.Immutable library is built-in as part of the shared frame - - - diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenDictionary.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenDictionary.cs index b0fad1cd1115c..3dddb87219090 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenDictionary.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenDictionary.cs @@ -238,9 +238,8 @@ private static FrozenDictionary CreateFromDictionary } else { - frozenDictionary = analysis.IgnoreCaseForEquals - ? new OrdinalStringFrozenDictionary_FullCaseSensitiveHashCaseInsensitiveEquals(keys, values, stringComparer, analysis.MinimumLength, analysis.MaximumLengthDiff) - : new OrdinalStringFrozenDictionary_Full(keys, values, stringComparer, analysis.MinimumLength, analysis.MaximumLengthDiff); + // if (IgnoreCaseForEquals) => Can only be true if there are no letters, thus case sensitive comparison still works here. + frozenDictionary = new OrdinalStringFrozenDictionary_Full(keys, values, stringComparer, analysis.MinimumLength, analysis.MaximumLengthDiff); } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSet.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSet.cs index 63c2be5dbec1b..608668351188c 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSet.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/FrozenSet.cs @@ -186,9 +186,8 @@ private static FrozenSet CreateFromSet(HashSet source) } else { - frozenSet = analysis.IgnoreCaseForEquals - ? new OrdinalStringFrozenSet_FullCaseSensitiveHashCaseInsensitiveEquals(entries, stringComparer, analysis.MinimumLength, analysis.MaximumLengthDiff) - : new OrdinalStringFrozenSet_Full(entries, stringComparer, analysis.MinimumLength, analysis.MaximumLengthDiff); + // if (IgnoreCaseForEquals) => Can only be true if there are no letters, thus case sensitive comparison still works here. + frozenSet = new OrdinalStringFrozenSet_Full(entries, stringComparer, analysis.MinimumLength, analysis.MaximumLengthDiff); } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/String/OrdinalStringFrozenDictionary_FullCaseSensitiveHashCaseInsensitiveEquals.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/String/OrdinalStringFrozenDictionary_FullCaseSensitiveHashCaseInsensitiveEquals.cs deleted file mode 100644 index f3c805e416054..0000000000000 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/String/OrdinalStringFrozenDictionary_FullCaseSensitiveHashCaseInsensitiveEquals.cs +++ /dev/null @@ -1,28 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections.Generic; - -namespace System.Collections.Frozen -{ - internal sealed class OrdinalStringFrozenDictionary_FullCaseSensitiveHashCaseInsensitiveEquals : OrdinalStringFrozenDictionary - { - internal OrdinalStringFrozenDictionary_FullCaseSensitiveHashCaseInsensitiveEquals( - string[] keys, - TValue[] values, - IEqualityComparer comparer, - int minimumLength, - int maximumLengthDiff) - : base(keys, values, comparer, minimumLength, maximumLengthDiff) - { - } - - // This override is necessary to force the jit to emit the code in such a way that it - // avoids virtual dispatch overhead when calling the Equals/GetHashCode methods. Don't - // remove this, or you'll tank performance. - private protected override ref readonly TValue GetValueRefOrNullRefCore(string key) => ref base.GetValueRefOrNullRefCore(key); - - private protected override bool Equals(string? x, string? y) => StringComparer.OrdinalIgnoreCase.Equals(x, y); - private protected override int GetHashCode(string s) => Hashing.GetHashCodeOrdinal(s.AsSpan()); - } -} diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/String/OrdinalStringFrozenSet_FullCaseSensitiveHashCaseInsensitiveEquals.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/String/OrdinalStringFrozenSet_FullCaseSensitiveHashCaseInsensitiveEquals.cs deleted file mode 100644 index ebcd7b14c6b17..0000000000000 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/String/OrdinalStringFrozenSet_FullCaseSensitiveHashCaseInsensitiveEquals.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections.Generic; - -namespace System.Collections.Frozen -{ - internal sealed class OrdinalStringFrozenSet_FullCaseSensitiveHashCaseInsensitiveEquals : OrdinalStringFrozenSet - { - internal OrdinalStringFrozenSet_FullCaseSensitiveHashCaseInsensitiveEquals( - string[] entries, - IEqualityComparer comparer, - int minimumLength, - int maximumLengthDiff) - : base(entries, comparer, minimumLength, maximumLengthDiff) - { - } - - // This override is necessary to force the jit to emit the code in such a way that it - // avoids virtual dispatch overhead when calling the Equals/GetHashCode methods. Don't - // remove this, or you'll tank performance. - private protected override int FindItemIndex(string item) => base.FindItemIndex(item); - - private protected override bool Equals(string? x, string? y) => StringComparer.OrdinalIgnoreCase.Equals(x, y); - private protected override int GetHashCode(string s) => Hashing.GetHashCodeOrdinal(s.AsSpan()); - } -}