Skip to content

Commit

Permalink
Improve StringComparer.OrdinalIgnoreCase.Compare perf (dotnet#80813)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored and mdh1418 committed Jan 24, 2023
1 parent a431246 commit 388c83b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/StringComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public override int Compare(string? x, string? y)

if (_ignoreCase)
{
return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
return System.Globalization.Ordinal.CompareStringIgnoreCase(ref x.GetRawStringData(), x.Length, ref y.GetRawStringData(), y.Length);
}

return string.CompareOrdinal(x, y);
Expand Down Expand Up @@ -418,7 +418,25 @@ private OrdinalIgnoreCaseComparer() : base(true)
{
}

public override int Compare(string? x, string? y) => string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
public override int Compare(string? x, string? y)
{
if (ReferenceEquals(x, y))
{
return 0;
}

if (x == null)
{
return -1;
}

if (y == null)
{
return 1;
}

return System.Globalization.Ordinal.CompareStringIgnoreCase(ref x.GetRawStringData(), x.Length, ref y.GetRawStringData(), y.Length);
}

public override bool Equals(string? x, string? y)
{
Expand Down

0 comments on commit 388c83b

Please sign in to comment.