From ce0f0b8bc3b39b8aa4b4aa6639fe505679f37c05 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sat, 6 Feb 2021 07:27:37 -0500 Subject: [PATCH] Remove use of string.GetHashCode(StringComparison) (#47883) All of our call sites to it pass in a const StringComparison; GetHashCode will then turn around and decide which property on StringComparer to use, and then call GetHashCode on the relevant instance... we may as well just pick the instance directly, which not only saves the lookup and enables devirtualization, it also enables better trimming, as string.GetHashCode(StringComparison) ends up rooting all of the StringComparer properties in case you pass in the appropriate StringComparison. --- src/libraries/System.Net.Mail/src/System/Net/Mail/MailAddress.cs | 2 +- src/libraries/System.Private.Uri/src/System/Uri.cs | 2 +- .../src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.Mail/src/System/Net/Mail/MailAddress.cs b/src/libraries/System.Net.Mail/src/System/Net/Mail/MailAddress.cs index 9b4a06d..dd19ec1 100644 --- a/src/libraries/System.Net.Mail/src/System/Net/Mail/MailAddress.cs +++ b/src/libraries/System.Net.Mail/src/System/Net/Mail/MailAddress.cs @@ -272,7 +272,7 @@ namespace System.Net.Mail public override int GetHashCode() { - return ToString().GetHashCode(StringComparison.InvariantCultureIgnoreCase); + return StringComparer.InvariantCultureIgnoreCase.GetHashCode(ToString()); } private static readonly EncodedStreamFactory s_encoderFactory = new EncodedStreamFactory(); diff --git a/src/libraries/System.Private.Uri/src/System/Uri.cs b/src/libraries/System.Private.Uri/src/System/Uri.cs index dbb89a3..db7d057 100644 --- a/src/libraries/System.Private.Uri/src/System/Uri.cs +++ b/src/libraries/System.Private.Uri/src/System/Uri.cs @@ -1542,7 +1542,7 @@ namespace System if (IsUncOrDosPath) { - return remoteUrl.GetHashCode(StringComparison.OrdinalIgnoreCase); + return StringComparer.OrdinalIgnoreCase.GetHashCode(remoteUrl); } else { diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs index ff36163..3416867 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs @@ -51,7 +51,7 @@ namespace System.Runtime.InteropServices public override int GetHashCode() { - return Name == null ? 0 : Name.GetHashCode(StringComparison.OrdinalIgnoreCase); + return Name == null ? 0 : StringComparer.OrdinalIgnoreCase.GetHashCode(Name); } public override string ToString() -- 2.7.4