8000 [Feature] Use cache for dash padding · PowerShell/PowerShell@7cc7d18 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7cc7d18

Browse files
committed
[Feature] Use cache for dash padding
1 parent 6bd70bc commit 7cc7d18

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ internal void GenerateHeader(string[] values, LineOutput lo)
180180
// NOTE: we can do this because "-" is a single cell character
181181
// on all devices. If changed to some other character, this assumption
182182
// would be invalidated
183-
breakLine[k] = new string('-', count);
183+
breakLine[k] = StringUtil.DashPadding(count);
184184
}
185185
GenerateRow(breakLine, lo, false, null, lo.DisplayCells);
186186
}

src/System.Management.Automation/utils/StringUtil.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ internal static string Padding(int countOfSpaces)
8181

8282
return result;
8383
}
84+
85+
private const int DashCacheMax = 120;
86+
private static readonly string[] DashCache = new string[DashCacheMax];
87+
internal static string DashPadding(int count)
88+
{
89+
if (count >= DashCacheMax)
90+
return new string('-', count);
91+
92+
var result = DashCache[count];
93+
94+
if (result == null)
95+
{
96+
Interlocked.CompareExchange(ref DashCache[count], new string('-', count), null);
97+
result = DashCache[count];
98+
}
99+
100+
return result;
101+
}
84102
}
85103
} // namespace
86104

0 commit comments

Comments
 (0)
0