Formatting: Use cache for dash padding strings (#6625)

This commit is contained in:
Ilya
2018-04-14 16:49:01 -07:00
committed by Dongbo Wang
parent feb38cff70
commit 2457113c80
2 changed files with 19 additions and 1 deletions
@@ -180,7 +180,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
// 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);
}
@@ -81,6 +81,24 @@ namespace System.Management.Automation.Internal
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