diff --git a/Benchmarks/Text/TextFormatter/RemoveHotKeySpecifier.cs b/Benchmarks/Text/TextFormatter/RemoveHotKeySpecifier.cs new file mode 100644 index 000000000..e72dc0e1e --- /dev/null +++ b/Benchmarks/Text/TextFormatter/RemoveHotKeySpecifier.cs @@ -0,0 +1,97 @@ +using System.Text; +using BenchmarkDotNet.Attributes; +using Tui = Terminal.Gui; + +namespace Terminal.Gui.Benchmarks.Text.TextFormatter; + +/// +/// Benchmarks for performance fine-tuning. +/// +[MemoryDiagnoser] +[BenchmarkCategory (nameof(Tui.TextFormatter))] +public class RemoveHotKeySpecifier +{ + // Omit from summary table. + private static readonly Rune HotkeySpecifier = (Rune)'_'; + + /// + /// Benchmark for previous implementation. + /// + [Benchmark] + [ArgumentsSource (nameof (DataSource))] + public string Previous (string text, int hotPos) + { + return StringConcatLoop (text, hotPos, HotkeySpecifier); + } + + /// + /// Benchmark for current implementation with stackalloc char buffer and fallback to rented array. + /// + [Benchmark (Baseline = true)] + [ArgumentsSource (nameof (DataSource))] + public string Current (string text, int hotPos) + { + return Tui.TextFormatter.RemoveHotKeySpecifier (text, hotPos, HotkeySpecifier); + } + + /// + /// Previous implementation with string concatenation in a loop. + /// + public static string StringConcatLoop (string text, int hotPos, Rune hotKeySpecifier) + { + if (string.IsNullOrEmpty (text)) + { + return text; + } + + // Scan + var start = string.Empty; + var i = 0; + + foreach (Rune c in text.EnumerateRunes ()) + { + if (c == hotKeySpecifier && i == hotPos) + { + i++; + + continue; + } + + start += c; + i++; + } + + return start; + } + + public IEnumerable DataSource () + { + string[] texts = [ + "", + // Typical scenario. + "_Save file (Ctrl+S)", + // Medium text, hotkey specifier somewhere in the middle. + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sed euismod metus. _Phasellus lectus metus, ultricies a commodo quis, facilisis vitae nulla.", + // Long text, hotkey specifier almost at the beginning. + "Ĺόŕéḿ íṕśúḿ d́όĺόŕ śít́ áḿét́, ćόńśéćt́ét́úŕ ád́íṕíśćíńǵ éĺít́. _Ṕŕáéśéńt́ q́úíś ĺúćt́úś éĺít́. Íńt́éǵéŕ út́ áŕćú éǵét́ d́όĺόŕ śćéĺéŕíśq́úé ḿát́t́íś áć ét́ d́íáḿ. " + + "Ṕéĺĺéń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́úé. " + + "Ṕŕáéśéńt́ śáṕíéń t́úŕṕíś, όŕńáŕé v́éĺ ḿáúŕíś át́, v́áŕíúś śúśćíṕít́ áńt́é. Út́ ṕúĺv́íńáŕ t́úŕṕíś ḿáśśá, q́úíś ćúŕśúś áŕćú f́áúćíb́úś íń.", + // Long text, hotkey specifier almost at the end. + "Ĺόŕéḿ íṕśúḿ d́όĺόŕ śít́ áḿét́, ćόńśéćt́ét́úŕ ád́íṕíśćíńǵ éĺít́. Ṕŕáéśéńt́ q́úíś ĺúćt́úś éĺít́. Íńt́éǵéŕ út́ áŕćú éǵét́ d́όĺόŕ śćéĺéŕíśq́úé ḿát́t́íś áć ét́ d́íáḿ. " + + "Ṕéĺĺéń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́úé. " + + "Ṕŕáéśéńt́ śáṕíéń t́úŕṕíś, όŕńáŕé v́éĺ ḿáúŕíś át́, v́áŕíúś śúśćíṕít́ áńt́é. _Út́ ṕúĺv́íńáŕ t́úŕṕíś ḿáśśá, q́úíś ćúŕśúś áŕćú f́áúćíb́úś íń.", + ]; + + foreach (string text in texts) + { + int hotPos = text.EnumerateRunes() + .Select((r, i) => r == HotkeySpecifier ? i : -1) + .FirstOrDefault(i => i > -1, -1); + + yield return [text, hotPos]; + } + + // Typical scenario but without hotkey and with misleading position. + yield return ["Save file (Ctrl+S)", 3]; + } +} diff --git a/Terminal.Gui/Text/TextFormatter.cs b/Terminal.Gui/Text/TextFormatter.cs index 418808094..ed49590e1 100644 --- a/Terminal.Gui/Text/TextFormatter.cs +++ b/Terminal.Gui/Text/TextFormatter.cs @@ -2444,24 +2444,44 @@ public class TextFormatter return text; } - // Scan - var start = string.Empty; - var i = 0; - - foreach (Rune c in text.EnumerateRunes ()) + const int maxStackallocCharBufferSize = 512; // ~1 kiB + char[]? rentedBufferArray = null; + try { - if (c == hotKeySpecifier && i == hotPos) - { - i++; + Span buffer = text.Length <= maxStackallocCharBufferSize + ? stackalloc char[text.Length] + : (rentedBufferArray = ArrayPool.Shared.Rent(text.Length)); - continue; + int i = 0; + var remainingBuffer = buffer; + foreach (Rune c in text.EnumerateRunes ()) + { + if (c == hotKeySpecifier && i == hotPos) + { + i++; + continue; + } + int charsWritten = c.EncodeToUtf16 (remainingBuffer); + remainingBuffer = remainingBuffer [charsWritten..]; + i++; } - start += c; - i++; - } + ReadOnlySpan newText = buffer [..^remainingBuffer.Length]; + // If the resulting string would be the same as original then just return the original. + if (newText.Equals(text, StringComparison.Ordinal)) + { + return text; + } - return start; + return new string (newText); + } + finally + { + if (rentedBufferArray != null) + { + ArrayPool.Shared.Return (rentedBufferArray); + } + } } #endregion // Static Members