From d8623ca09d4d5eee7c7278de7b3e74ea1d061206 Mon Sep 17 00:00:00 2001 From: Ilya Date: Mon, 1 Jul 2019 14:28:31 +0500 Subject: [PATCH 1/3] Reduce allocations in NormalizePath() --- .../namespaces/NavigationProviderBase.cs | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/System.Management.Automation/namespaces/NavigationProviderBase.cs b/src/System.Management.Automation/namespaces/NavigationProviderBase.cs index 5458e54b1ea..348dec2ebb6 100644 --- a/src/System.Management.Automation/namespaces/NavigationProviderBase.cs +++ b/src/System.Management.Automation/namespaces/NavigationProviderBase.cs @@ -916,27 +916,43 @@ private string NormalizePath(string path) // For example: path HKCU:\Test\/ is pointing to a subkey '/' of 'HKCU:\Test', if we // normalize it, then we will get a wrong path. bool pathHasForwardSlash = path.IndexOf(StringLiterals.AlternatePathSeparator) != -1; - bool pathHasBackSlash = path.IndexOf(StringLiterals.DefaultPathSeparator) != -1; - bool pathHasMixedSlashes = pathHasForwardSlash && pathHasBackSlash; - bool shouldNormalizePath = true; - string normalizedPath = path.Replace(StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator); + if (!pathHasForwardSlash) + { + return path; + } + + bool pathHasBackSlash = path.IndexOf(StringLiterals.DefaultPathSeparator) != -1; + string normalizedPath; // There is a mix of slashes & the path is rooted & the path exists without normalization. // In this case, we might want to skip the normalization to the path. - if (pathHasMixedSlashes && IsAbsolutePath(path) && ItemExists(path)) + if (pathHasBackSlash && IsAbsolutePath(path) && ItemExists(path)) { // 1. The path exists and ends with a forward slash, in this case, it's very possible the ending forward slash // make sense to the underlying provider, so we skip normalization // 2. The path exists, but not anymore after normalization, then we skip normalization bool parentEndsWithForwardSlash = path.EndsWith(StringLiterals.AlternatePathSeparatorString, StringComparison.Ordinal); - if (parentEndsWithForwardSlash || !ItemExists(normalizedPath)) + if (parentEndsWithForwardSlash) { - shouldNormalizePath = false; + return path; + } + + normalizedPath = path.Replace(StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator); + + if (!ItemExists(normalizedPath)) + { + return path; + } + else + { + return normalizedPath; } } - return shouldNormalizePath ? normalizedPath : path; + normalizedPath = path.Replace(StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator); + + return normalizedPath; } /// From bc9c48f5754879ba437ab6952d301a41ddde0a00 Mon Sep 17 00:00:00 2001 From: Ilya Date: Mon, 1 Jul 2019 16:36:22 +0500 Subject: [PATCH 2/3] Remove extra var and use char in EndsWith() --- .../namespaces/NavigationProviderBase.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/System.Management.Automation/namespaces/NavigationProviderBase.cs b/src/System.Management.Automation/namespaces/NavigationProviderBase.cs index 348dec2ebb6..0ee6bd1595f 100644 --- a/src/System.Management.Automation/namespaces/NavigationProviderBase.cs +++ b/src/System.Management.Automation/namespaces/NavigationProviderBase.cs @@ -905,19 +905,19 @@ protected virtual object MoveItemDynamicParameters( /// those errors. /// /// - /// The path to normalize + /// The path to normalize. /// /// - /// Normalized path or the original path + /// Normalized path or the original path. /// private string NormalizePath(string path) { // If we have a mix of slashes, then we may introduce an error by normalizing the path. // For example: path HKCU:\Test\/ is pointing to a subkey '/' of 'HKCU:\Test', if we // normalize it, then we will get a wrong path. - bool pathHasForwardSlash = path.IndexOf(StringLiterals.AlternatePathSeparator) != -1; - - if (!pathHasForwardSlash) + // + // Fast return if nothing to normalize. + if (path.IndexOf(StringLiterals.AlternatePathSeparator) == -1) { return path; } @@ -932,7 +932,7 @@ private string NormalizePath(string path) // 1. The path exists and ends with a forward slash, in this case, it's very possible the ending forward slash // make sense to the underlying provider, so we skip normalization // 2. The path exists, but not anymore after normalization, then we skip normalization - bool parentEndsWithForwardSlash = path.EndsWith(StringLiterals.AlternatePathSeparatorString, StringComparison.Ordinal); + bool parentEndsWithForwardSlash = path.EndsWith(StringLiterals.AlternatePathSeparator); if (parentEndsWithForwardSlash) { return path; From 3dc9b20a8e62ff0746bfb51f54c970720e8ca8a1 Mon Sep 17 00:00:00 2001 From: Ilya Date: Mon, 1 Jul 2019 17:04:55 +0500 Subject: [PATCH 3/3] Remove extra var parentEndsWithForwardSlash --- .../namespaces/NavigationProviderBase.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/System.Management.Automation/namespaces/NavigationProviderBase.cs b/src/System.Management.Automation/namespaces/NavigationProviderBase.cs index 0ee6bd1595f..8dfd5af83ea 100644 --- a/src/System.Management.Automation/namespaces/NavigationProviderBase.cs +++ b/src/System.Management.Automation/namespaces/NavigationProviderBase.cs @@ -932,8 +932,7 @@ private string NormalizePath(string path) // 1. The path exists and ends with a forward slash, in this case, it's very possible the ending forward slash // make sense to the underlying provider, so we skip normalization // 2. The path exists, but not anymore after normalization, then we skip normalization - bool parentEndsWithForwardSlash = path.EndsWith(StringLiterals.AlternatePathSeparator); - if (parentEndsWithForwardSlash) + if (path.EndsWith(StringLiterals.AlternatePathSeparator)) { return path; }