Skip to content

Commit 2e4e73a

Browse files
TheTonttutig
authored andcommitted
Rewrite TextFormatter.RemoveHotKeySpecifier
Uses stackalloc char buffer with fallback to rented array.
1 parent 5ab51fc commit 2e4e73a

File tree

2 files changed

+128
-11
lines changed

2 files changed

+128
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System.Text;
2+
using BenchmarkDotNet.Attributes;
3+
using Tui = Terminal.Gui;
4+
5+
namespace Terminal.Gui.Benchmarks.Text.TextFormatter;
6+
7+
/// <summary>
8+
/// Benchmarks for <see cref="Tui.TextFormatter.RemoveHotKeySpecifier"/> performance fine-tuning.
9+
/// </summary>
10+
[MemoryDiagnoser]
11+
[BenchmarkCategory (nameof(Tui.TextFormatter))]
12+
public class RemoveHotKeySpecifier
13+
{
14+
// Omit from summary table.
15+
private static readonly Rune HotkeySpecifier = (Rune)'_';
16+
17+
/// <summary>
18+
/// Benchmark for previous implementation.
19+
/// </summary>
20+
[Benchmark]
21+
[ArgumentsSource (nameof (DataSource))]
22+
public string Previous (string text, int hotPos)
23+
{
24+
return StringConcatLoop (text, hotPos, HotkeySpecifier);
25+
}
26+
27+
/// <summary>
28+
/// Benchmark for current implementation with stackalloc char buffer and fallback to rented array.
29+
/// </summary>
30+
[Benchmark (Baseline = true)]
31+
[ArgumentsSource (nameof (DataSource))]
32+
public string Current (string text, int hotPos)
33+
{
34+
return Tui.TextFormatter.RemoveHotKeySpecifier (text, hotPos, HotkeySpecifier);
35+
}
36+
37+
/// <summary>
38+
/// Previous implementation with string concatenation in a loop.
39+
/// </summary>
40+
public static string StringConcatLoop (string text, int hotPos, Rune hotKeySpecifier)
41+
{
42+
if (string.IsNullOrEmpty (text))
43+
{
44+
return text;
45+
}
46+
47+
// Scan
48+
var start = string.Empty;
49+
var i = 0;
50+
51+
foreach (Rune c in text.EnumerateRunes ())
52+
{
53+
if (c == hotKeySpecifier && i == hotPos)
54+
{
55+
i++;
56+
57+
continue;
58+
}
59+
60+
start += c;
61+
i++;
62+
}
63+
64+
return start;
65+
}
66+
67+
public IEnumerable<object []> DataSource ()
68+
{
69+
string[] texts = [
70+
"",
71+
// Typical scenario.
72+
"_Save file (Ctrl+S)",
73+
// Medium text, hotkey specifier somewhere in the middle.
74+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sed euismod metus. _Phasellus lectus metus, ultricies a commodo quis, facilisis vitae nulla.",
75+
// Long text, hotkey specifier almost at the beginning.
76+
"Ĺόŕéḿ íṕśúḿ d́όĺόŕ śít́ áḿét́, ćόńśéćt́ét́úŕ ád́íṕíśćíńǵ éĺít́. _Ṕŕáéśéńt́ q́úíś ĺúćt́úś éĺít́. Íńt́éǵéŕ út́ áŕćú éǵét́ d́όĺόŕ śćéĺéŕíśq́úé ḿát́t́íś áć ét́ d́íáḿ. " +
77+
"Ṕéĺĺéńt́éśq́úé śéd́ d́áṕíb́úś ḿáśśá, v́éĺ t́ŕíśt́íq́úé d́úí. Śéd́ v́ít́áé ńéq́úé éú v́éĺít́ όŕńáŕé áĺíq́úét́. Út́ q́úíś όŕćí t́éḿṕόŕ, t́éḿṕόŕ t́úŕṕíś íd́, t́éḿṕúś ńéq́úé. " +
78+
"Ṕŕáéśéńt́ śáṕíéń t́úŕṕíś, όŕńáŕé v́éĺ ḿáúŕíś át́, v́áŕíúś śúśćíṕít́ áńt́é. Út́ ṕúĺv́íńáŕ t́úŕṕíś ḿáśśá, q́úíś ćúŕśúś áŕćú f́áúćíb́úś íń.",
79+
// Long text, hotkey specifier almost at the end.
80+
"Ĺόŕéḿ íṕśúḿ d́όĺόŕ śít́ áḿét́, ćόńśéćt́ét́úŕ ád́íṕíśćíńǵ éĺít́. Ṕŕáéśéńt́ q́úíś ĺúćt́úś éĺít́. Íńt́éǵéŕ út́ áŕćú éǵét́ d́όĺόŕ śćéĺéŕíśq́úé ḿát́t́íś áć ét́ d́íáḿ. " +
81+
"Ṕéĺĺéńt́éśq́úé śéd́ d́áṕíb́úś ḿáśśá, v́éĺ t́ŕíśt́íq́úé d́úí. Śéd́ v́ít́áé ńéq́úé éú v́éĺít́ όŕńáŕé áĺíq́úét́. Út́ q́úíś όŕćí t́éḿṕόŕ, t́éḿṕόŕ t́úŕṕíś íd́, t́éḿṕúś ńéq́úé. " +
82+
"Ṕŕáéśéńt́ śáṕíéń t́úŕṕíś, όŕńáŕé v́éĺ ḿáúŕíś át́, v́áŕíúś śúśćíṕít́ áńt́é. _Út́ ṕúĺv́íńáŕ t́úŕṕíś ḿáśśá, q́úíś ćúŕśúś áŕćú f́áúćíb́úś íń.",
83+
];
84+
85+
foreach (string text in texts)
86+
{
87+
int hotPos = text.EnumerateRunes()
88+
.Select((r, i) => r == HotkeySpecifier ? i : -1)
89+
.FirstOrDefault(i => i > -1, -1);
90+
91+
yield return [text, hotPos];
92+
}
93+
94+
// Typical scenario but without hotkey and with misleading position.
95+
yield return ["Save file (Ctrl+S)", 3];
96+
}
97+
}

Terminal.Gui/Text/TextFormatter.cs

+31-11
Original file line numberDiff line numberDiff line change
@@ -2444,24 +2444,44 @@ public static string RemoveHotKeySpecifier (string text, int hotPos, Rune hotKey
24442444
return text;
24452445
}
24462446

2447-
// Scan
2448-
var start = string.Empty;
2449-
var i = 0;
2450-
2451-
foreach (Rune c in text.EnumerateRunes ())
2447+
const int maxStackallocCharBufferSize = 512; // ~1 kiB
2448+
char[]? rentedBufferArray = null;
2449+
try
24522450
{
2453-
if (c == hotKeySpecifier && i == hotPos)
2451+
Span<char> buffer = text.Length <= maxStackallocCharBufferSize
2452+
? stackalloc char[text.Length]
2453+
: (rentedBufferArray = ArrayPool<char>.Shared.Rent(text.Length));
2454+
2455+
int i = 0;
2456+
var remainingBuffer = buffer;
2457+
foreach (Rune c in text.EnumerateRunes ())
24542458
{
2459+
if (c == hotKeySpecifier && i == hotPos)
2460+
{
2461+
i++;
2462+
continue;
2463+
}
2464+
int charsWritten = c.EncodeToUtf16 (remainingBuffer);
2465+
remainingBuffer = remainingBuffer [charsWritten..];
24552466
i++;
2467+
}
24562468

2457-
continue;
2469+
ReadOnlySpan<char> newText = buffer [..^remainingBuffer.Length];
2470+
// If the resulting string would be the same as original then just return the original.
2471+
if (newText.Equals(text, StringComparison.Ordinal))
2472+
{
2473+
return text;
24582474
}
24592475

2460-
start += c;
2461-
i++;
2476+
return new string (newText);
2477+
}
2478+
finally
2479+
{
2480+
if (rentedBufferArray != null)
2481+
{
2482+
ArrayPool<char>.Shared.Return (rentedBufferArray);
2483+
}
24622484
}
2463-
2464-
return start;
24652485
}
24662486

24672487
#endregion // Static Members

0 commit comments

Comments
 (0)