From e63c988504c9b3a210aa3111e2454a7766e03226 Mon Sep 17 00:00:00 2001 From: Radek Zikmund Date: Wed, 19 Jun 2024 18:53:20 +0200 Subject: [PATCH 1/2] Improve perf of CredentialCache.GetCredential --- .../src/System/Net/CredentialCacheKey.cs | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/libraries/Common/src/System/Net/CredentialCacheKey.cs b/src/libraries/Common/src/System/Net/CredentialCacheKey.cs index 0d59c10149315..2e0158a803ed5 100644 --- a/src/libraries/Common/src/System/Net/CredentialCacheKey.cs +++ b/src/libraries/Common/src/System/Net/CredentialCacheKey.cs @@ -25,7 +25,7 @@ internal CredentialCacheKey(Uri uriPrefix, string authenticationType) AuthenticationType = authenticationType; } - internal bool Match(Uri uri, string authenticationType) + internal bool Match(Uri uri, int prefixLen, string authenticationType) { if (uri == null || authenticationType == null) { @@ -40,12 +40,12 @@ internal bool Match(Uri uri, string authenticationType) if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"Match({UriPrefix} & {uri})"); - return IsPrefix(uri, UriPrefix); + return IsPrefix(uri, prefixLen); } // IsPrefix (Uri) // - // Determines whether is a prefix of this URI. A prefix + // Determines whether is a prefix of this URI. A prefix // match is defined as: // // scheme match @@ -55,23 +55,21 @@ internal bool Match(Uri uri, string authenticationType) // // Returns: // True if is a prefix of this URI - private static bool IsPrefix(Uri uri, Uri prefixUri) + private bool IsPrefix(Uri uri, int prefixLen) { Debug.Assert(uri != null); - Debug.Assert(prefixUri != null); - if (prefixUri.Scheme != uri.Scheme || prefixUri.Host != uri.Host || prefixUri.Port != uri.Port) + if (UriPrefix.Scheme != uri.Scheme || UriPrefix.Host != uri.Host || UriPrefix.Port != uri.Port) { return false; } - int prefixLen = prefixUri.AbsolutePath.LastIndexOf('/'); - if (prefixLen > uri.AbsolutePath.LastIndexOf('/')) + if (UriPrefixLength > prefixLen) { return false; } - return string.Compare(uri.AbsolutePath, 0, prefixUri.AbsolutePath, 0, prefixLen, StringComparison.OrdinalIgnoreCase) == 0; + return string.Compare(uri.AbsolutePath, 0, UriPrefix.AbsolutePath, 0, UriPrefixLength, StringComparison.OrdinalIgnoreCase) == 0; } public override int GetHashCode() => @@ -108,21 +106,31 @@ public static bool TryGetCredential(Dictionary longestMatchPrefix) + if (uriPrefixLength == prefixLen) { - // Yes: update the information about currently preferred match - longestMatchPrefix = prefixLen; - mostSpecificMatch = value; - mostSpecificMatchUri = key.UriPrefix; + // we can't get any better than this + break; } } } From 5ce45d44cf487c07cde0b9bdc2f27dda92461d89 Mon Sep 17 00:00:00 2001 From: Radek Zikmund Date: Mon, 24 Jun 2024 13:45:15 +0200 Subject: [PATCH 2/2] Minor changes --- .../Common/src/System/Net/CredentialCacheKey.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/libraries/Common/src/System/Net/CredentialCacheKey.cs b/src/libraries/Common/src/System/Net/CredentialCacheKey.cs index 2e0158a803ed5..b84ec32774c61 100644 --- a/src/libraries/Common/src/System/Net/CredentialCacheKey.cs +++ b/src/libraries/Common/src/System/Net/CredentialCacheKey.cs @@ -58,8 +58,9 @@ internal bool Match(Uri uri, int prefixLen, string authenticationType) private bool IsPrefix(Uri uri, int prefixLen) { Debug.Assert(uri != null); + Uri uriPrefix = UriPrefix; - if (UriPrefix.Scheme != uri.Scheme || UriPrefix.Host != uri.Host || UriPrefix.Port != uri.Port) + if (uriPrefix.Scheme != uri.Scheme || uriPrefix.Host != uri.Host || uriPrefix.Port != uri.Port) { return false; } @@ -69,7 +70,7 @@ private bool IsPrefix(Uri uri, int prefixLen) return false; } - return string.Compare(uri.AbsolutePath, 0, UriPrefix.AbsolutePath, 0, UriPrefixLength, StringComparison.OrdinalIgnoreCase) == 0; + return string.Compare(uri.AbsolutePath, 0, uriPrefix.AbsolutePath, 0, UriPrefixLength, StringComparison.OrdinalIgnoreCase) == 0; } public override int GetHashCode() => @@ -106,6 +107,12 @@ public static bool TryGetCredential(Dictionary