From 7cc7d18ce0679e73571b08c769e4522f74af7166 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 11 Apr 2018 16:29:38 +0500 Subject: [PATCH] [Feature] Use cache for dash padding --- .../FormatAndOutput/common/TableWriter.cs | 2 +- .../utils/StringUtil.cs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs index 37374ea07ef..a6cbf31d0f6 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs @@ -180,7 +180,7 @@ internal void GenerateHeader(string[] values, LineOutput lo) // NOTE: we can do this because "-" is a single cell character // on all devices. If changed to some other character, this assumption // would be invalidated - breakLine[k] = new string('-', count); + breakLine[k] = StringUtil.DashPadding(count); } GenerateRow(breakLine, lo, false, null, lo.DisplayCells); } diff --git a/src/System.Management.Automation/utils/StringUtil.cs b/src/System.Management.Automation/utils/StringUtil.cs index 7af181038f0..8b4b3ddd96c 100644 --- a/src/System.Management.Automation/utils/StringUtil.cs +++ b/src/System.Management.Automation/utils/StringUtil.cs @@ -81,6 +81,24 @@ internal static string Padding(int countOfSpaces) return result; } + + private const int DashCacheMax = 120; + private static readonly string[] DashCache = new string[DashCacheMax]; + internal static string DashPadding(int count) + { + if (count >= DashCacheMax) + return new string('-', count); + + var result = DashCache[count]; + + if (result == null) + { + Interlocked.CompareExchange(ref DashCache[count], new string('-', count), null); + result = DashCache[count]; + } + + return result; + } } } // namespace