Skip to content

Commit 02b1214

Browse files
authored
Optimize Dictionary.FindValue for constant keys (no constant folding) (#117427)
1 parent 39fc72d commit 02b1214

File tree

4 files changed

+4
-23
lines changed

4 files changed

+4
-23
lines changed

src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ internal ref TValue FindValue(TKey key)
409409
if (typeof(TKey).IsValueType && // comparer can only be null for value types; enable JIT to eliminate entire if block for ref types
410410
comparer == null)
411411
{
412-
uint hashCode = (uint)key.GetHashCode();
412+
// TODO: Replace with just key.GetHashCode once https://github.com/dotnet/runtime/issues/117521 is resolved.
413+
uint hashCode = (uint)EqualityComparer<TKey>.Default.GetHashCode(key);
413414
int i = GetBucket(hashCode);
414415
Entry[]? entries = _entries;
415416
uint collisionCount = 0;

src/libraries/System.Private.CoreLib/src/System/Collections/Generic/NonRandomizedStringEqualityComparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ internal OrdinalIgnoreCaseComparer(IEqualityComparer<string?> wrappedComparer) :
112112
{
113113
}
114114

115-
public override bool Equals(string? x, string? y) => string.EqualsOrdinalIgnoreCase(x, y);
115+
public override bool Equals(string? x, string? y) => string.Equals(x, y, StringComparison.OrdinalIgnoreCase);
116116

117117
public override int GetHashCode(string? obj)
118118
{

src/libraries/System.Private.CoreLib/src/System/Collections/Generic/RandomizedStringEqualityComparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ internal OrdinalIgnoreCaseComparer(IEqualityComparer<string?> wrappedComparer)
102102
string IAlternateEqualityComparer<ReadOnlySpan<char>, string?>.Create(ReadOnlySpan<char> span) =>
103103
span.ToString();
104104

105-
public override bool Equals(string? x, string? y) => string.EqualsOrdinalIgnoreCase(x, y);
105+
public override bool Equals(string? x, string? y) => string.Equals(x, y, StringComparison.OrdinalIgnoreCase);
106106

107107
bool IAlternateEqualityComparer<ReadOnlySpan<char>, string?>.Equals(ReadOnlySpan<char> alternate, string? other)
108108
{

src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,6 @@ ref Unsafe.Add(ref strA.GetRawStringData(), (nint)(uint)indexA /* force zero-ext
4646
ref Unsafe.Add(ref strB.GetRawStringData(), (nint)(uint)indexB /* force zero-extension */), countB);
4747
}
4848

49-
internal static bool EqualsOrdinalIgnoreCase(string? strA, string? strB)
50-
{
51-
if (ReferenceEquals(strA, strB))
52-
{
53-
return true;
54-
}
55-
56-
if (strA is null || strB is null)
57-
{
58-
return false;
59-
}
60-
61-
if (strA.Length != strB.Length)
62-
{
63-
return false;
64-
}
65-
66-
return EqualsOrdinalIgnoreCaseNoLengthCheck(strA, strB);
67-
}
68-
6949
private static bool EqualsOrdinalIgnoreCaseNoLengthCheck(string strA, string strB)
7050
{
7151
Debug.Assert(strA.Length == strB.Length);

0 commit comments

Comments
 (0)