From ec88045fee57028cfabd3877bfb588f46b063c1a Mon Sep 17 00:00:00 2001 From: Staffan Gustafsson Date: Thu, 7 Feb 2019 05:23:26 +0100 Subject: [PATCH] Reduce string allocations when formatting file system objects. (#8831) * Reduce string allocations when formatting file system objects by caching FullName. * Clear cache FullName when the module changes. --- .../engine/DataStoreAdapterProvider.cs | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/System.Management.Automation/engine/DataStoreAdapterProvider.cs b/src/System.Management.Automation/engine/DataStoreAdapterProvider.cs index 441054bc89..7c17a961b7 100644 --- a/src/System.Management.Automation/engine/DataStoreAdapterProvider.cs +++ b/src/System.Management.Automation/engine/DataStoreAdapterProvider.cs @@ -35,6 +35,8 @@ namespace System.Management.Automation /// private SessionState _sessionState; + private string _fullName; + /// /// Gets the name of the provider. /// @@ -47,29 +49,34 @@ namespace System.Management.Automation { get { - string result = this.Name; - if (!string.IsNullOrEmpty(this.PSSnapInName)) + string GetFullName(string name, string psSnapInName, string moduleName) { - result = - string.Format( - System.Globalization.CultureInfo.InvariantCulture, - "{0}\\{1}", - this.PSSnapInName, - this.Name); + string result = name; + if (!string.IsNullOrEmpty(psSnapInName)) + { + result = + string.Format( + System.Globalization.CultureInfo.InvariantCulture, + "{0}\\{1}", + psSnapInName, + name); + } + + // After converting core snapins to load as modules, the providers will have Module property populated + else if (!string.IsNullOrEmpty(moduleName)) + { + result = + string.Format( + System.Globalization.CultureInfo.InvariantCulture, + "{0}\\{1}", + moduleName, + name); + } + + return result; } - // After converting core snapins to load as modules, the providers will have Module property populated - else if (!string.IsNullOrEmpty(this.ModuleName)) - { - result = - string.Format( - System.Globalization.CultureInfo.InvariantCulture, - "{0}\\{1}", - this.ModuleName, - this.Name); - } - - return result; + return _fullName ?? (_fullName = GetFullName(Name, PSSnapInName, ModuleName)); } } @@ -136,6 +143,7 @@ namespace System.Management.Automation internal void SetModule(PSModuleInfo module) { Module = module; + _fullName = null; } ///